mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 01:11:33 +00:00
Start work on HistoryMonitor to read command history
This commit is contained in:
parent
6312e22117
commit
eea6a418a8
37
lib/tfw/components/history_monitor.py
Normal file
37
lib/tfw/components/history_monitor.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from watchdog.observers import Observer
|
||||||
|
from watchdog.events import FileSystemEventHandler
|
||||||
|
from collections import deque
|
||||||
|
from os.path import dirname
|
||||||
|
|
||||||
|
|
||||||
|
class CallbackEventHandler(FileSystemEventHandler):
|
||||||
|
def __init__(self, *callbacks):
|
||||||
|
super().__init__()
|
||||||
|
self.callbacks = callbacks
|
||||||
|
|
||||||
|
def on_modified(self, event):
|
||||||
|
for callback in self.callbacks:
|
||||||
|
callback()
|
||||||
|
|
||||||
|
|
||||||
|
class HistoryMonitor:
|
||||||
|
def __init__(self, histfile):
|
||||||
|
self.histfile = histfile
|
||||||
|
self._history = deque()
|
||||||
|
self.observer = Observer()
|
||||||
|
self.observer.schedule(CallbackEventHandler(self._fetch_history), dirname(self.histfile))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def history(self):
|
||||||
|
return self._history
|
||||||
|
|
||||||
|
def _fetch_history(self):
|
||||||
|
with open(self.histfile, 'r') as ifile:
|
||||||
|
self._history = deque(ifile.readlines())
|
||||||
|
|
||||||
|
def watch(self):
|
||||||
|
self.observer.start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.observer.stop()
|
||||||
|
self.observer.join()
|
Loading…
Reference in New Issue
Block a user