diff --git a/solvable/src/event_handler_main.py b/solvable/src/event_handler_main.py index 914583e..1ff27f1 100644 --- a/solvable/src/event_handler_main.py +++ b/solvable/src/event_handler_main.py @@ -1,3 +1,5 @@ +from ast import literal_eval + from tornado.ioloop import IOLoop from tfw.components import IdeEventHandler, TerminalEventHandler, ProcessManagingEventHandler, BashMonitor @@ -16,53 +18,77 @@ def cenator(history): class TestCommands(TerminalCommands): + """ + Some example commands useful for debugging. + Please remove from production code and inherit your own + class from TerminalCommands if you need to define custom + commands in your challenge. + """ # pylint: disable=unused-argument, attribute-defined-outside-init, no-self-use - def command_selectdir(self, *args): - TFWServerConnector().send_to_eventhandler({'key': 'ide', - 'data': {'command': 'selectdir', - 'directory': args[0]}}) + def command_sendmessage(self, *args): + """ + Insert TFW message template as first argument if executed without args. - def command_trigger(self, *args): - TFWServerConnector().send({'key': '', - 'trigger': args[0]}) - - def command_togglenext(self, *args): - if not hasattr(self, 'togglenext_visible'): - self.togglenext_visible = True - TFWServerConnector().send({'key': 'messagecontrol', - 'data': {'command': 'showbutton', - 'next_visibility': self.togglenext_visible}}) - self.togglenext_visible = not self.togglenext_visible + Evaluate first argumen as a dict and send it to the frontend. + This is useful for playing around with frontend APIs. + """ + if not args: + message_template = """'{"key": "", "data": {"command": ""}}'""" + TFWServerConnector().send_to_eventhandler({ + 'key': 'shell', + 'data': { + 'command': 'write', + 'shellcmd': f'sendmessage {message_template}' + } + }) + else: + TFWServerConnector().send(literal_eval(args[0])) def command_seppuku_tfw(self, *args): + """ + Restart tfw_server.py and event_handler_main.py. + This can speed up development when combined with mounting + volumes from host to container. + """ seppuku = ('nohup sh -c "supervisorctl restart tfwserver event_handler_main" &> /dev/null & ' 'clear && echo "Committed seppuku! :)" && sleep infinity') uplink = TFWServerConnector() - uplink.send_to_eventhandler({'key': 'shell', - 'data': {'command': 'write', - 'shellcmd': f'{seppuku}\n'}}) - uplink.send({'key': 'dashboard', - 'data' :{'command': 'reload_frontend'}}) - - def command_changelayout(self, *args): - message = {'key': 'dashboard', - 'data': {'command': 'layout', - 'layout': args[0]}} - if len(args) >= 2: - message['data']['hide_messages'] = (args[1] in ['yes', 'y', '1', 'true']) - TFWServerConnector().send(message) + uplink.send_to_eventhandler({ + 'key': 'shell', + 'data': { + 'command': 'write', + 'shellcmd': f'{seppuku}\n' + } + }) + uplink.send({ + 'key': 'dashboard', + 'data': { + 'command': 'reload_frontend' + } + }) if __name__ == '__main__': - ide = IdeEventHandler(key='ide', allowed_directories=[TFWENV.IDE_WD, TFWENV.WEBSERVICE_DIR], - directory=TFWENV.IDE_WD, exclude=['*.pyc'], additional_watched_directories=[TFWENV.WEBSERVICE_DIR]) - terminado = TerminalEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE)) - terminado.historymonitor.subscribe_callback(cenator) - commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc') - terminado.historymonitor.subscribe_callback(commands.callback) - processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor) + ide = IdeEventHandler( # Web IDE backend + key='ide', allowed_directories=[TFWENV.IDE_WD, TFWENV.WEBSERVICE_DIR], + directory=TFWENV.IDE_WD, exclude=['*.pyc'], + additional_watched_directories=[TFWENV.WEBSERVICE_DIR] + ) + terminal = TerminalEventHandler( # Web shell backend + key='shell', + monitor=BashMonitor(TFWENV.HISTFILE) + ) + processmanager = ProcessManagingEventHandler( # Handles 'deploy' button clicks + key='processmanager', + dirmonitor=ide.monitor + ) - eventhandlers = {ide, terminado, processmanager} + terminal.historymonitor.subscribe_callback(cenator) + + commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc') + terminal.historymonitor.subscribe_callback(commands.callback) + + eventhandlers = {ide, terminal, processmanager} try: IOLoop.instance().start() finally: