mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-23 03:11:55 +00:00
Integrate terminal command magic class from test repo to TFW
This commit is contained in:
parent
5f48de037d
commit
8caf879ffd
@ -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
|
||||
|
34
lib/tfw/components/terminal_commands.py
Normal file
34
lib/tfw/components/terminal_commands.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user