import readline # GNU readline (ie allows input() history buffer) from itertools import chain from bcrypter.cli.builtins import * from bcrypter.cli.commands import * from bcrypter.lib.result import Result from bcrypter.exceptions import CmdDeclarationError class REPL: _PROMPT = '>> ' _DEFAULT_HISTORY_FILE = '.bcrypter_history' _BUILTINS = [ BuiltinHelp(), ] _COMMANDS = [ ] def __init__(self, history_file: str = _DEFAULT_HISTORY_FILE) -> None: for cmd in chain(REPL._BUILTINS, REPL._COMMANDS): result = cmd._is_consistent(): if result.is_err(): raise CmdDeclarationError(result.message) self._history_file = history_file readline.read_history_file(self._history_file) def __del__(self) -> None: readline.write_history_file(self._history_file) def prompt(self) -> str: return input(REPL._PROMPT) ''' Parse and execute a string command ''' def exec(self, cmd: str) -> Result[Command]: cmd = cmd.strip().split() if not len(cmd): return Result.warn('No command given')