Make HistoryMonitor utilize our inotify module

This commit is contained in:
R. Richard 2019-06-11 17:25:35 +02:00 committed by Kristóf Tóth
parent 49856cebe2
commit 85c720127e
3 changed files with 27 additions and 32 deletions

View File

@ -6,25 +6,11 @@ from re import findall
from re import compile as compileregex from re import compile as compileregex
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from watchdog.events import PatternMatchingEventHandler from tfw.components.inotify import InotifyObserver
from tfw.event_handlers import TFWServerUplinkConnector
from tfw.mixins.callback_mixin import CallbackMixin
from tfw.mixins.observer_mixin import ObserverMixin
from tfw.decorators.rate_limiter import RateLimiter
class CallbackEventHandler(PatternMatchingEventHandler, ABC): class HistoryMonitor(ABC, InotifyObserver):
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):
""" """
Abstract class capable of monitoring and parsing a history file such as Abstract class capable of monitoring and parsing a history file such as
bash HISTFILEs. Monitoring means detecting when the file was changed and 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. command pattern property and optionally the sanitize_command method.
See examples below. See examples below.
""" """
def __init__(self, histfile): def __init__(self, domain, histfile):
self.domain = domain
self.histfile = histfile self.histfile = histfile
self._history = [] self._history = []
self._last_length = len(self._history) self._last_length = len(self._history)
self.observer.schedule( self.uplink = TFWServerUplinkConnector()
CallbackEventHandler( super().__init__(dirname(self.histfile), [self.histfile])
[self.histfile],
self._fetch_history, def on_modified(self, event):
self._invoke_callbacks self._fetch_history()
), if self._last_length < len(self._history):
dirname(self.histfile) for command in self._history[self._last_length:]:
) self.send_message(command)
@property @property
def history(self): def history(self):
@ -72,9 +59,11 @@ class HistoryMonitor(CallbackMixin, ObserverMixin, ABC):
# pylint: disable=no-self-use # pylint: disable=no-self-use
return command return command
def _invoke_callbacks(self): def send_message(self, command):
if self._last_length < len(self._history): self.uplink.send_message({
self._execute_callbacks(self.history) 'key': f'history.{self.domain}',
'value': command
})
class BashMonitor(HistoryMonitor): class BashMonitor(HistoryMonitor):
@ -87,6 +76,9 @@ class BashMonitor(HistoryMonitor):
shopt -s histappend shopt -s histappend
unset HISTCONTROL unset HISTCONTROL
""" """
def __init__(self, histfile):
super().__init__('bash', histfile)
@property @property
def command_pattern(self): def command_pattern(self):
return r'.+' return r'.+'
@ -100,6 +92,9 @@ class GDBMonitor(HistoryMonitor):
HistoryMonitor to monitor GDB sessions. HistoryMonitor to monitor GDB sessions.
For this to work "set trace-commands on" must be set in GDB. For this to work "set trace-commands on" must be set in GDB.
""" """
def __init__(self, histfile):
super().__init__('gdb', histfile)
@property @property
def command_pattern(self): def command_pattern(self):
return r'(?<=\n)\+(.+)\n' return r'(?<=\n)\+(.+)\n'

View File

@ -61,8 +61,8 @@ class TerminalCommands(ABC):
def _match_command_regex(self, string): def _match_command_regex(self, string):
return match(self._command_method_regex, string) return match(self._command_method_regex, string)
def callback(self, history): def callback(self, command):
parts = split(history[-1]) parts = split(command)
command = parts[0] command = parts[0]
if command in self.command_implemetations.keys(): if command in self.command_implemetations.keys():
try: try:

View File

@ -43,7 +43,7 @@ class TerminalEventHandler(FrontendEventHandlerBase):
} }
if self._historymonitor: if self._historymonitor:
self._historymonitor.watch() self._historymonitor.start()
self.terminado_server.listen() self.terminado_server.listen()
@property @property