1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-11-14 16:17:17 +00:00

Move Commands class to TFW library as TerminalCommands

This commit is contained in:
Kristóf Tóth 2018-04-12 10:55:33 +02:00
parent b718f960f7
commit 36d4054147

View File

@ -1,8 +1,7 @@
from re import match
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tfw.components import WebideEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor from tfw.components import WebideEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor
from tfw.components import TerminalCommands
from tfw.networking import MessageSender, TFWServerConnector from tfw.networking import MessageSender, TFWServerConnector
from tfw.config import TFWENV from tfw.config import TFWENV
from tfw.config.logs import logging from tfw.config.logs import logging
@ -15,33 +14,7 @@ def cenator(history):
MessageSender().send('JOHN CENA', 'You\'ve executed "{}"'.format(history[-1])) MessageSender().send('JOHN CENA', 'You\'ve executed "{}"'.format(history[-1]))
class Commands: class TestCommands(TerminalCommands):
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): def command_selectdir(self, *args):
TFWServerConnector().send_to_eventhandler('webide', TFWServerConnector().send_to_eventhandler('webide',
{'data': {'command': 'selectdir', {'data': {'command': 'selectdir',
@ -66,7 +39,7 @@ if __name__ == '__main__':
directory=TFWENV.WEBIDE_WD, exclude=['*.pyc']) directory=TFWENV.WEBIDE_WD, exclude=['*.pyc'])
terminado = TerminadoEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE)) terminado = TerminadoEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE))
terminado.historymonitor.subscribe_callback(cenator) terminado.historymonitor.subscribe_callback(cenator)
commands = Commands() commands = TestCommands()
terminado.historymonitor.subscribe_callback(commands.callback) terminado.historymonitor.subscribe_callback(commands.callback)
processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor) processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor)