2018-03-03 16:15:21 +00:00
|
|
|
from watchdog.observers import Observer
|
2018-03-05 15:57:50 +00:00
|
|
|
from watchdog.events import PatternMatchingEventHandler
|
2018-03-03 16:15:21 +00:00
|
|
|
from os.path import dirname
|
|
|
|
|
2018-03-07 09:12:58 +00:00
|
|
|
from tfw.components.mixins.callback_mixin import CallbackMixin
|
|
|
|
|
2018-03-03 16:15:21 +00:00
|
|
|
|
2018-03-05 15:57:50 +00:00
|
|
|
class CallbackEventHandler(PatternMatchingEventHandler):
|
|
|
|
def __init__(self, files, *callbacks):
|
|
|
|
super().__init__(files)
|
2018-03-03 16:15:21 +00:00
|
|
|
self.callbacks = callbacks
|
|
|
|
|
|
|
|
def on_modified(self, event):
|
|
|
|
for callback in self.callbacks:
|
|
|
|
callback()
|
|
|
|
|
|
|
|
|
2018-03-07 09:12:58 +00:00
|
|
|
class HistoryMonitor(CallbackMixin):
|
2018-03-03 16:15:21 +00:00
|
|
|
def __init__(self, histfile):
|
2018-03-07 09:12:58 +00:00
|
|
|
CallbackMixin.__init__(self)
|
2018-03-03 16:15:21 +00:00
|
|
|
self.histfile = histfile
|
2018-03-03 21:44:05 +00:00
|
|
|
self._history = []
|
2018-03-05 15:23:01 +00:00
|
|
|
self._last_length = len(self._history)
|
2018-03-03 16:15:21 +00:00
|
|
|
self.observer = Observer()
|
2018-03-05 15:57:50 +00:00
|
|
|
self.observer.schedule(CallbackEventHandler([self.histfile],
|
|
|
|
self._fetch_history,
|
2018-03-05 15:23:01 +00:00
|
|
|
self._invoke_callbacks),
|
|
|
|
dirname(self.histfile))
|
2018-03-03 16:15:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def history(self):
|
|
|
|
return self._history
|
|
|
|
|
|
|
|
def _fetch_history(self):
|
2018-03-05 15:23:01 +00:00
|
|
|
self._last_length = len(self._history)
|
2018-03-03 16:15:21 +00:00
|
|
|
with open(self.histfile, 'r') as ifile:
|
2018-03-03 22:02:19 +00:00
|
|
|
self._history = [line.rstrip() for line in ifile.readlines()]
|
2018-03-03 16:15:21 +00:00
|
|
|
|
2018-03-05 15:23:01 +00:00
|
|
|
def _invoke_callbacks(self):
|
|
|
|
if self._last_length < len(self._history):
|
2018-03-07 09:12:58 +00:00
|
|
|
self._execute_callbacks(self.history)
|
2018-03-05 15:23:01 +00:00
|
|
|
|
2018-03-03 16:15:21 +00:00
|
|
|
def watch(self):
|
|
|
|
self.observer.start()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.observer.stop()
|
|
|
|
self.observer.join()
|