mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-22 22:11:57 +00:00
Make HistoryMonitor utilize our inotify module
This commit is contained in:
parent
49856cebe2
commit
85c720127e
@ -6,25 +6,11 @@ from re import findall
|
||||
from re import compile as compileregex
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from watchdog.events import PatternMatchingEventHandler
|
||||
|
||||
from tfw.mixins.callback_mixin import CallbackMixin
|
||||
from tfw.mixins.observer_mixin import ObserverMixin
|
||||
from tfw.decorators.rate_limiter import RateLimiter
|
||||
from tfw.components.inotify import InotifyObserver
|
||||
from tfw.event_handlers import TFWServerUplinkConnector
|
||||
|
||||
|
||||
class CallbackEventHandler(PatternMatchingEventHandler, ABC):
|
||||
def __init__(self, files, *callbacks):
|
||||
super().__init__(files)
|
||||
self.callbacks = callbacks
|
||||
|
||||
@RateLimiter(rate_per_second=2)
|
||||
def on_modified(self, event):
|
||||
for callback in self.callbacks:
|
||||
callback()
|
||||
|
||||
|
||||
class HistoryMonitor(CallbackMixin, ObserverMixin, ABC):
|
||||
class HistoryMonitor(ABC, InotifyObserver):
|
||||
"""
|
||||
Abstract class capable of monitoring and parsing a history file such as
|
||||
bash HISTFILEs. Monitoring means detecting when the file was changed and
|
||||
@ -36,18 +22,19 @@ class HistoryMonitor(CallbackMixin, ObserverMixin, ABC):
|
||||
command pattern property and optionally the sanitize_command method.
|
||||
See examples below.
|
||||
"""
|
||||
def __init__(self, histfile):
|
||||
def __init__(self, domain, histfile):
|
||||
self.domain = domain
|
||||
self.histfile = histfile
|
||||
self._history = []
|
||||
self._last_length = len(self._history)
|
||||
self.observer.schedule(
|
||||
CallbackEventHandler(
|
||||
[self.histfile],
|
||||
self._fetch_history,
|
||||
self._invoke_callbacks
|
||||
),
|
||||
dirname(self.histfile)
|
||||
)
|
||||
self.uplink = TFWServerUplinkConnector()
|
||||
super().__init__(dirname(self.histfile), [self.histfile])
|
||||
|
||||
def on_modified(self, event):
|
||||
self._fetch_history()
|
||||
if self._last_length < len(self._history):
|
||||
for command in self._history[self._last_length:]:
|
||||
self.send_message(command)
|
||||
|
||||
@property
|
||||
def history(self):
|
||||
@ -72,9 +59,11 @@ class HistoryMonitor(CallbackMixin, ObserverMixin, ABC):
|
||||
# pylint: disable=no-self-use
|
||||
return command
|
||||
|
||||
def _invoke_callbacks(self):
|
||||
if self._last_length < len(self._history):
|
||||
self._execute_callbacks(self.history)
|
||||
def send_message(self, command):
|
||||
self.uplink.send_message({
|
||||
'key': f'history.{self.domain}',
|
||||
'value': command
|
||||
})
|
||||
|
||||
|
||||
class BashMonitor(HistoryMonitor):
|
||||
@ -87,6 +76,9 @@ class BashMonitor(HistoryMonitor):
|
||||
shopt -s histappend
|
||||
unset HISTCONTROL
|
||||
"""
|
||||
def __init__(self, histfile):
|
||||
super().__init__('bash', histfile)
|
||||
|
||||
@property
|
||||
def command_pattern(self):
|
||||
return r'.+'
|
||||
@ -100,6 +92,9 @@ class GDBMonitor(HistoryMonitor):
|
||||
HistoryMonitor to monitor GDB sessions.
|
||||
For this to work "set trace-commands on" must be set in GDB.
|
||||
"""
|
||||
def __init__(self, histfile):
|
||||
super().__init__('gdb', histfile)
|
||||
|
||||
@property
|
||||
def command_pattern(self):
|
||||
return r'(?<=\n)\+(.+)\n'
|
||||
|
@ -61,8 +61,8 @@ class TerminalCommands(ABC):
|
||||
def _match_command_regex(self, string):
|
||||
return match(self._command_method_regex, string)
|
||||
|
||||
def callback(self, history):
|
||||
parts = split(history[-1])
|
||||
def callback(self, command):
|
||||
parts = split(command)
|
||||
command = parts[0]
|
||||
if command in self.command_implemetations.keys():
|
||||
try:
|
||||
|
@ -43,7 +43,7 @@ class TerminalEventHandler(FrontendEventHandlerBase):
|
||||
}
|
||||
|
||||
if self._historymonitor:
|
||||
self._historymonitor.watch()
|
||||
self._historymonitor.start()
|
||||
self.terminado_server.listen()
|
||||
|
||||
@property
|
||||
|
Loading…
Reference in New Issue
Block a user