From 8caf879ffd862cd25c2de06ad56256709499f38d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 12 Apr 2018 10:56:15 +0200 Subject: [PATCH 1/2] Integrate terminal command magic class from test repo to TFW --- lib/tfw/components/__init__.py | 1 + lib/tfw/components/terminal_commands.py | 34 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 lib/tfw/components/terminal_commands.py diff --git a/lib/tfw/components/__init__.py b/lib/tfw/components/__init__.py index 18e7aae..9d2a1ac 100644 --- a/lib/tfw/components/__init__.py +++ b/lib/tfw/components/__init__.py @@ -6,3 +6,4 @@ from .process_managing_event_handler import ProcessManagingEventHandler from .terminado_event_handler import TerminadoEventHandler from .webide_event_handler import WebideEventHandler from .history_monitor import HistoryMonitor, BashMonitor, GDBMonitor +from .terminal_commands import TerminalCommands diff --git a/lib/tfw/components/terminal_commands.py b/lib/tfw/components/terminal_commands.py new file mode 100644 index 0000000..9b5a7b9 --- /dev/null +++ b/lib/tfw/components/terminal_commands.py @@ -0,0 +1,34 @@ +from abc import ABC +from re import match + +from tfw.config.logs import logging + +LOG = logging.getLogger(__name__) + + +class TerminalCommands(ABC): + 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) From 16c1fe7b50a2e637d309bc09e2c761ba01a591e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 12 Apr 2018 11:07:56 +0200 Subject: [PATCH 2/2] Implement appending command aliases to bashrc to avoid command not found --- lib/tfw/components/terminal_commands.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/tfw/components/terminal_commands.py b/lib/tfw/components/terminal_commands.py index 9b5a7b9..b07f652 100644 --- a/lib/tfw/components/terminal_commands.py +++ b/lib/tfw/components/terminal_commands.py @@ -7,10 +7,18 @@ LOG = logging.getLogger(__name__) class TerminalCommands(ABC): - def __init__(self): + def __init__(self, bashrc=None): 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)} + if bashrc is not None: + self._setup_bashrc_aliases(bashrc) + + def _setup_bashrc_aliases(self, bashrc): + with open(bashrc, 'a') as ofile: + alias_template = 'alias {0}="{0} > /dev/null 2>&1"\n' + for command in self.command_implemetations.keys(): + ofile.write(alias_template.format(command)) def _is_command_implementation(self, method_name): return bool(self._match_command_regex(method_name))