1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-11-15 06:37:18 +00:00
test-tutorial-framework/solvable/src/event_handler_main.py

79 lines
3.0 KiB
Python
Raw Normal View History

from re import match
from tornado.ioloop import IOLoop
2018-04-07 12:18:41 +00:00
from tfw.components import WebideEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor
from tfw.networking import MessageSender, TFWServerConnector
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]))
class Commands:
def __init__(self):
self._command_method_regex = r'^command_(.+)$'
self.command_implemetations = {self._parse_command_name(fun): getattr(self, fun) for fun in dir(self)
if callable(getattr(self, fun)) and self._is_command_implementation(fun)}
def _is_command_implementation(self, method_name):
return bool(self._match_command_regex(method_name))
def _parse_command_name(self, method_name):
try:
return self._match_command_regex(method_name).groups()[0]
except AttributeError:
return ''
def _match_command_regex(self, string):
return match(self._command_method_regex, string)
def callback(self, history):
parts = history[-1].split()
command = parts[0]
if command in self.command_implemetations.keys():
try:
self.command_implemetations[command](*parts[1:])
except IndexError:
LOG.debug('Command "%s" failed!', command)
def command_selectdir(self, *args):
TFWServerConnector().send_to_eventhandler('webide',
{'data': {'command': 'selectdir',
'directory': args[0]}})
def command_trigger(self, *args):
TFWServerConnector().send('selectdir_needs_no_key',
{'trigger': args[0]})
def command_togglenext(self, *args):
if not hasattr(self, 'togglenext_visible'):
self.togglenext_visible = True
TFWServerConnector().send('messagecontrol',
{'data': {'command': 'showbutton',
'next_visibility': self.togglenext_visible}})
self.togglenext_visible = not self.togglenext_visible
if __name__ == '__main__':
# pylint: disable=invalid-name
2018-04-07 12:18:41 +00:00
ide = WebideEventHandler(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)
commands = Commands()
terminado.historymonitor.subscribe_callback(commands.callback)
processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor)
eventhandlers = {ide, terminado, processmanager}
try:
IOLoop.instance().start()
finally:
for eh in eventhandlers:
eh.cleanup()