From 35aed1029158fa210cfc1d75c00eeaaa21fe52af Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Sun, 10 Nov 2019 11:19:58 +0000 Subject: [PATCH] Add python-console command to the hint-tester This enables interactive state exploration of britney's internals for a given data set. Signed-off-by: Niels Thykier --- britney.py | 15 +++++++++++++++ britney2/completer.py | 2 +- britney2/console.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 britney2/console.py diff --git a/britney.py b/britney.py index d2cb7e8..a6b473b 100755 --- a/britney.py +++ b/britney.py @@ -1306,6 +1306,13 @@ class Britney(object): known_hints = self._hint_parser.registered_hints + print("Britney hint tester") + print() + print("Besides inputting known britney hints, the follow commands are also available") + print(" * quit/exit - terminates the shell") + print(" * python-console - jump into an interactive python shell (with the current loaded dataset)") + print() + while True: # read the command from the command line try: @@ -1319,6 +1326,14 @@ class Britney(object): # quit the hint tester if user_input and user_input[0] in ('quit', 'exit'): break + elif user_input and user_input[0] == 'python-console': + try: + import britney2.console + except ImportError as e: + print("Failed to import britney.console module: %s" % repr(e)) + continue + britney2.console.run_python_console(self) + print("Returning to the britney hint-tester console") # run a hint elif user_input and user_input[0] in ('easy', 'hint', 'force-hint'): mi_factory = self._migration_item_factory diff --git a/britney2/completer.py b/britney2/completer.py index dee6262..1e7a23c 100644 --- a/britney2/completer.py +++ b/britney2/completer.py @@ -33,7 +33,7 @@ class Completer(object): self.cmds = ['easy', 'hint', 'force-hint', 'force', 'remove', 'force', 'age-days', 'urgent', 'block-all', 'block', 'block-udeb', 'unblock', 'unblock-udeb', - 'approve', 'exit', 'quit'] + 'approve', 'exit', 'quit', 'python-console'] self.britney = britney suite_info = britney.suite_info # generate a completion list from excuses. diff --git a/britney2/console.py b/britney2/console.py new file mode 100644 index 0000000..93512ea --- /dev/null +++ b/britney2/console.py @@ -0,0 +1,32 @@ +import code + + +class SubInterpreterExit(SystemExit): + pass + + +def console_quit(): + raise SubInterpreterExit() + + +def run_python_console(britney_obj): + console_locals = { + 'britney': britney_obj, + '__name__': '__console__', + '__doc__': None, + 'quit': console_quit, + 'exit': console_quit, + } + console = code.InteractiveConsole(locals=console_locals) + banner = """\ +Interactive python (REPL) shell in britney. + +Locals available + * britney: Instance of the Britney object. + * quit()/exit(): leave this REPL console. +""" + try: + console.interact(banner=banner, exitmsg='') + except SubInterpreterExit: + pass +