From 99461141866422f1499f8fecd4fb90c446eab31f Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Tue, 11 Jun 2019 17:27:37 +0200 Subject: [PATCH] Create event handler to monitor bash commands --- solvable/src/event_handler_main.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/solvable/src/event_handler_main.py b/solvable/src/event_handler_main.py index 5654713..9d48fd6 100644 --- a/solvable/src/event_handler_main.py +++ b/solvable/src/event_handler_main.py @@ -19,13 +19,25 @@ LOG = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) -def cenator(history): +class TerminalCallbackEventHandler(EventHandlerBase): """ - Logs commands executed in terminal to messages. + Logs commands executed in terminal to messages and invokes an + additional callback function to handle special commands. !! Please remove from production code. !! """ - LOG.debug('User executed command: "%s"', history[-1]) - MessageSender().send('JOHN CENA', f'You\'ve executed "{history[-1]}"') + def __init__(self, key, callback): + self.callback = callback + super().__init__(key) + + def handle_event(self, message): + command = message['value'] + self.cenator(command) + self.callback(command) + + @staticmethod + def cenator(command): + LOG.debug('User executed command: "%s"', command) + MessageSender().send('JOHN CENA', f'You\'ve executed "{command}"') class TestCommands(TerminalCommands): @@ -123,6 +135,10 @@ def main(): key='shell', monitor=BashMonitor(TFWENV.HISTFILE) ) + commands = TerminalCallbackEventHandler( # Reacts to terminal commands + 'history.bash', + TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc').callback + ) processmanager = ProcessManagingEventHandler( # Handles 'deploy' button clicks key='processmanager', dirmonitor=ide.monitor, @@ -147,11 +163,6 @@ def main(): key='test' ) - # Terminal command handlers - commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc') - terminal.historymonitor.subscribe_callback(commands.callback) - terminal.historymonitor.subscribe_callback(cenator) - event_handlers = EventHandlerBase.get_local_instances() def stop(sig, frame): for eh in event_handlers: