From 93f211c41011df7f7ce41f6fbbf164943910372c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 11 Apr 2018 17:34:44 +0200 Subject: [PATCH 1/4] Refactor command implementation boilerplate into Command class --- solvable/src/event_handler_main.py | 71 +++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/solvable/src/event_handler_main.py b/solvable/src/event_handler_main.py index ee602b5..d2dc0e6 100644 --- a/solvable/src/event_handler_main.py +++ b/solvable/src/event_handler_main.py @@ -1,3 +1,5 @@ +from re import match + from tornado.ioloop import IOLoop from tfw.components import WebideEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor @@ -13,28 +15,53 @@ def cenator(history): MessageSender().send('JOHN CENA', 'You\'ve executed "{}"'.format(history[-1])) -def selectdir(history): - try: - cmd = history[-1].split() - if cmd[0] == 'selectdir': - TFWServerConnector().send_to_eventhandler('webide', - {'data': {'command': 'selectdir', - 'directory': cmd[1]}}) - except IndexError: - LOG.exception('Selectdir failed!') +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)} + @staticmethod + def _is_command_implementation(method_name): + return bool(Commands._match_command_regex(method_name)) -def toggle_next(history): - toggle_next.button_state = not toggle_next.button_state - try: - cmd = history[-1].split() - if cmd[0] == 'togglenext': - TFWServerConnector().send('messagecontrol', - {'data': {'command': 'showbutton', - 'next_visibility': toggle_next.button_state}}) - except IndexError: - LOG.exception('Togglenext failed!') -toggle_next.button_state = False + @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 if __name__ == '__main__': @@ -43,8 +70,8 @@ if __name__ == '__main__': 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) + commands = Commands() + terminado.historymonitor.subscribe_callback(commands.callback) processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor) eventhandlers = {ide, terminado, processmanager} From b718f960f7f2a1aae4304fceda1b129ec4db24de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 12 Apr 2018 10:37:47 +0200 Subject: [PATCH 2/4] Rename stuff in Command class to be more expressive --- solvable/src/event_handler_main.py | 32 +++++++++++++----------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/solvable/src/event_handler_main.py b/solvable/src/event_handler_main.py index d2dc0e6..3bb6142 100644 --- a/solvable/src/event_handler_main.py +++ b/solvable/src/event_handler_main.py @@ -17,45 +17,41 @@ def cenator(history): 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)} + 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)} - @staticmethod - def _is_command_implementation(method_name): - return bool(Commands._match_command_regex(method_name)) + def _is_command_implementation(self, method_name): + return bool(self._match_command_regex(method_name)) - @staticmethod - def _parse_command_name(method_name): + def _parse_command_name(self, method_name): try: - return Commands._match_command_regex(method_name).groups()[0] + return self._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 _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_impls.keys(): + if command in self.command_implemetations.keys(): try: - self.command_impls[command](*parts[1:]) + self.command_implemetations[command](*parts[1:]) except IndexError: LOG.debug('Command "%s" failed!', command) - def _command_impl_selectdir(self, *args): + def command_selectdir(self, *args): TFWServerConnector().send_to_eventhandler('webide', {'data': {'command': 'selectdir', 'directory': args[0]}}) - def _command_impl_trigger(self, *args): + def command_trigger(self, *args): TFWServerConnector().send('selectdir_needs_no_key', {'trigger': args[0]}) - def _command_impl_togglenext(self, *args): + def command_togglenext(self, *args): if not hasattr(self, 'togglenext_visible'): self.togglenext_visible = True TFWServerConnector().send('messagecontrol', From 36d4054147506a4e14f5769e2248dd2b3a6cd8a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 12 Apr 2018 10:55:33 +0200 Subject: [PATCH 3/4] Move Commands class to TFW library as TerminalCommands --- solvable/src/event_handler_main.py | 33 +++--------------------------- 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/solvable/src/event_handler_main.py b/solvable/src/event_handler_main.py index 3bb6142..e45f2fa 100644 --- a/solvable/src/event_handler_main.py +++ b/solvable/src/event_handler_main.py @@ -1,8 +1,7 @@ -from re import match - from tornado.ioloop import IOLoop from tfw.components import WebideEventHandler, TerminadoEventHandler, ProcessManagingEventHandler, BashMonitor +from tfw.components import TerminalCommands from tfw.networking import MessageSender, TFWServerConnector from tfw.config import TFWENV from tfw.config.logs import logging @@ -15,33 +14,7 @@ def cenator(history): 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) - +class TestCommands(TerminalCommands): def command_selectdir(self, *args): TFWServerConnector().send_to_eventhandler('webide', {'data': {'command': 'selectdir', @@ -66,7 +39,7 @@ if __name__ == '__main__': directory=TFWENV.WEBIDE_WD, exclude=['*.pyc']) terminado = TerminadoEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE)) terminado.historymonitor.subscribe_callback(cenator) - commands = Commands() + commands = TestCommands() terminado.historymonitor.subscribe_callback(commands.callback) processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor) From 7952ca7e541c0af0b008333184f0de6e33b7714d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 12 Apr 2018 11:08:58 +0200 Subject: [PATCH 4/4] Add .bashrc path to TestCommands instance --- solvable/src/event_handler_main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solvable/src/event_handler_main.py b/solvable/src/event_handler_main.py index e45f2fa..c67647a 100644 --- a/solvable/src/event_handler_main.py +++ b/solvable/src/event_handler_main.py @@ -5,6 +5,7 @@ from tfw.components import TerminalCommands from tfw.networking import MessageSender, TFWServerConnector from tfw.config import TFWENV from tfw.config.logs import logging +from tao.config import TAOENV LOG = logging.getLogger(__name__) @@ -39,7 +40,7 @@ if __name__ == '__main__': directory=TFWENV.WEBIDE_WD, exclude=['*.pyc']) terminado = TerminadoEventHandler(key='shell', monitor=BashMonitor(TFWENV.HISTFILE)) terminado.historymonitor.subscribe_callback(cenator) - commands = TestCommands() + commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc') terminado.historymonitor.subscribe_callback(commands.callback) processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor)