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
1 changed files with 20 additions and 9 deletions

View File

@ -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: