2018-04-11 15:34:44 +00:00
|
|
|
from re import match
|
|
|
|
|
2018-03-23 14:27:42 +00:00
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
|
2018-04-07 12:18:41 +00:00
|
|
|
from tfw.components import WebideEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor
|
2018-04-06 14:23:20 +00:00
|
|
|
from tfw.networking import MessageSender, TFWServerConnector
|
2018-03-25 14:34:31 +00:00
|
|
|
from tfw.config import TFWENV
|
2018-03-23 14:27:42 +00:00
|
|
|
from tfw.config.logs import logging
|
2018-03-31 22:45:15 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-03-23 14:27:42 +00:00
|
|
|
|
|
|
|
|
2018-03-27 14:04:33 +00:00
|
|
|
def cenator(history):
|
2018-03-31 22:45:15 +00:00
|
|
|
LOG.debug('User executed command: "%s"', history[-1])
|
2018-03-27 14:04:33 +00:00
|
|
|
MessageSender().send('JOHN CENA', 'You\'ve executed "{}"'.format(history[-1]))
|
|
|
|
|
|
|
|
|
2018-04-11 15:34:44 +00:00
|
|
|
class Commands:
|
|
|
|
def __init__(self):
|
|
|
|
self.command_impls = {self._parse_command_name(fun): getattr(self, fun) for fun in dir(self)
|
|
|
|
if callable(getattr(self, fun))
|
|
|
|
and self._is_command_implementation(fun)}
|
2018-03-27 14:04:33 +00:00
|
|
|
|
2018-04-11 15:34:44 +00:00
|
|
|
@staticmethod
|
|
|
|
def _is_command_implementation(method_name):
|
|
|
|
return bool(Commands._match_command_regex(method_name))
|
2018-03-27 14:04:33 +00:00
|
|
|
|
2018-04-11 15:34:44 +00:00
|
|
|
@staticmethod
|
|
|
|
def _parse_command_name(method_name):
|
|
|
|
try:
|
|
|
|
return Commands._match_command_regex(method_name).groups()[0]
|
|
|
|
except AttributeError:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _match_command_regex(string):
|
|
|
|
command_impl_regex = r'^_command_impl_(.+)$'
|
|
|
|
return match(command_impl_regex, string)
|
|
|
|
|
|
|
|
def callback(self, history):
|
|
|
|
parts = history[-1].split()
|
|
|
|
command = parts[0]
|
|
|
|
if command in self.command_impls.keys():
|
|
|
|
try:
|
|
|
|
self.command_impls[command](*parts[1:])
|
|
|
|
except IndexError:
|
|
|
|
LOG.debug('Command "%s" failed!', command)
|
|
|
|
|
|
|
|
def _command_impl_selectdir(self, *args):
|
|
|
|
TFWServerConnector().send_to_eventhandler('webide',
|
|
|
|
{'data': {'command': 'selectdir',
|
|
|
|
'directory': args[0]}})
|
|
|
|
|
|
|
|
def _command_impl_trigger(self, *args):
|
|
|
|
TFWServerConnector().send('selectdir_needs_no_key',
|
|
|
|
{'trigger': args[0]})
|
|
|
|
|
|
|
|
def _command_impl_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
|
2018-03-27 14:04:33 +00:00
|
|
|
|
|
|
|
|
2018-03-23 14:27:42 +00:00
|
|
|
if __name__ == '__main__':
|
2018-03-31 22:45:15 +00:00
|
|
|
# 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'])
|
2018-03-29 09:34:53 +00:00
|
|
|
terminado = TerminadoEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE))
|
2018-03-27 14:04:33 +00:00
|
|
|
terminado.historymonitor.subscribe_callback(cenator)
|
2018-04-11 15:34:44 +00:00
|
|
|
commands = Commands()
|
|
|
|
terminado.historymonitor.subscribe_callback(commands.callback)
|
2018-03-23 14:27:42 +00:00
|
|
|
processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor)
|
|
|
|
|
|
|
|
eventhandlers = {ide, terminado, processmanager}
|
|
|
|
try:
|
|
|
|
IOLoop.instance().start()
|
|
|
|
finally:
|
2018-03-31 22:45:15 +00:00
|
|
|
for eh in eventhandlers:
|
|
|
|
eh.cleanup()
|