from tornado.ioloop import IOLoop from tfw.components import SourceCodeEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor from tfw.networking import MessageSender, ServerUplinkConnector from tfw.config import TFWENV from tfw.config.logs import logging LOG = logging.getLogger(__name__) def cenator(history): LOG.debug('User executed command: "%s"', history[-1]) MessageSender().send('JOHN CENA', 'You\'ve executed "{}"'.format(history[-1])) def selectdir(history): try: cmd = history[-1].split() if cmd[0] == 'selectdir': ServerUplinkConnector().send_to_eventhandler('webide', {'data': {'command': 'selectdir', 'directory': cmd[1]}}) except IndexError: LOG.exception('Selectdir failed!') def toggle_next(history): toggle_next.button_state = not toggle_next.button_state try: cmd = history[-1].split() if cmd[0] == 'togglenext': ServerUplinkConnector().send('messagecontrol', {'data': {'command': 'showbutton', 'next_visibility': toggle_next.button_state}}) except IndexError: LOG.exception('Togglenext failed!') toggle_next.button_state = False if __name__ == '__main__': # pylint: disable=invalid-name ide = SourceCodeEventHandler(key='webide', allowed_directories=[TFWENV.WEBIDE_WD], directory=TFWENV.WEBIDE_WD, exclude=['*.pyc']) terminado = TerminadoEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE)) terminado.historymonitor.subscribe_callback(cenator) terminado.historymonitor.subscribe_callback(selectdir) terminado.historymonitor.subscribe_callback(toggle_next) processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor) eventhandlers = {ide, terminado, processmanager} try: IOLoop.instance().start() finally: for eh in eventhandlers: eh.cleanup()