mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 00:11:33 +00:00
Implement callback subscription logic in HistoryMonitor
This commit is contained in:
parent
7dd2512300
commit
0a20cffa09
@ -17,17 +17,31 @@ class HistoryMonitor:
|
|||||||
def __init__(self, histfile):
|
def __init__(self, histfile):
|
||||||
self.histfile = histfile
|
self.histfile = histfile
|
||||||
self._history = []
|
self._history = []
|
||||||
|
self._last_length = len(self._history)
|
||||||
|
self._callbacks = []
|
||||||
self.observer = Observer()
|
self.observer = Observer()
|
||||||
self.observer.schedule(CallbackEventHandler(self._fetch_history), dirname(self.histfile))
|
self.observer.schedule(CallbackEventHandler(self._fetch_history,
|
||||||
|
self._invoke_callbacks),
|
||||||
|
dirname(self.histfile))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def history(self):
|
def history(self):
|
||||||
return self._history
|
return self._history
|
||||||
|
|
||||||
|
@property
|
||||||
|
def callbacks(self):
|
||||||
|
return self._callbacks
|
||||||
|
|
||||||
def _fetch_history(self):
|
def _fetch_history(self):
|
||||||
|
self._last_length = len(self._history)
|
||||||
with open(self.histfile, 'r') as ifile:
|
with open(self.histfile, 'r') as ifile:
|
||||||
self._history = [line.rstrip() for line in ifile.readlines()]
|
self._history = [line.rstrip() for line in ifile.readlines()]
|
||||||
|
|
||||||
|
def _invoke_callbacks(self):
|
||||||
|
if self._last_length < len(self._history):
|
||||||
|
for callback in self.callbacks:
|
||||||
|
callback(self.history)
|
||||||
|
|
||||||
def watch(self):
|
def watch(self):
|
||||||
self.observer.start()
|
self.observer.start()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user