1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-11-14 22:07:17 +00:00

Create event handler to monitor bash commands

This commit is contained in:
R. Richard 2019-06-11 17:27:37 +02:00
parent ba9d745e75
commit 9946114186

View File

@ -19,13 +19,25 @@ LOG = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG) 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. !! !! Please remove from production code. !!
""" """
LOG.debug('User executed command: "%s"', history[-1]) def __init__(self, key, callback):
MessageSender().send('JOHN CENA', f'You\'ve executed "{history[-1]}"') 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): class TestCommands(TerminalCommands):
@ -123,6 +135,10 @@ def main():
key='shell', key='shell',
monitor=BashMonitor(TFWENV.HISTFILE) 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 processmanager = ProcessManagingEventHandler( # Handles 'deploy' button clicks
key='processmanager', key='processmanager',
dirmonitor=ide.monitor, dirmonitor=ide.monitor,
@ -147,11 +163,6 @@ def main():
key='test' 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() event_handlers = EventHandlerBase.get_local_instances()
def stop(sig, frame): def stop(sig, frame):
for eh in event_handlers: for eh in event_handlers: