mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 00:01:31 +00:00
Refactor callback subscription stuff to a mixin
This commit is contained in:
parent
6db4c80671
commit
6a6236e704
@ -2,6 +2,8 @@ from watchdog.observers import Observer
|
|||||||
from watchdog.events import PatternMatchingEventHandler
|
from watchdog.events import PatternMatchingEventHandler
|
||||||
from os.path import dirname
|
from os.path import dirname
|
||||||
|
|
||||||
|
from tfw.components.mixins.callback_mixin import CallbackMixin
|
||||||
|
|
||||||
|
|
||||||
class CallbackEventHandler(PatternMatchingEventHandler):
|
class CallbackEventHandler(PatternMatchingEventHandler):
|
||||||
def __init__(self, files, *callbacks):
|
def __init__(self, files, *callbacks):
|
||||||
@ -13,12 +15,12 @@ class CallbackEventHandler(PatternMatchingEventHandler):
|
|||||||
callback()
|
callback()
|
||||||
|
|
||||||
|
|
||||||
class HistoryMonitor:
|
class HistoryMonitor(CallbackMixin):
|
||||||
def __init__(self, histfile):
|
def __init__(self, histfile):
|
||||||
|
CallbackMixin.__init__(self)
|
||||||
self.histfile = histfile
|
self.histfile = histfile
|
||||||
self._history = []
|
self._history = []
|
||||||
self._last_length = len(self._history)
|
self._last_length = len(self._history)
|
||||||
self._callbacks = []
|
|
||||||
self.observer = Observer()
|
self.observer = Observer()
|
||||||
self.observer.schedule(CallbackEventHandler([self.histfile],
|
self.observer.schedule(CallbackEventHandler([self.histfile],
|
||||||
self._fetch_history,
|
self._fetch_history,
|
||||||
@ -29,10 +31,6 @@ class HistoryMonitor:
|
|||||||
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)
|
self._last_length = len(self._history)
|
||||||
with open(self.histfile, 'r') as ifile:
|
with open(self.histfile, 'r') as ifile:
|
||||||
@ -40,8 +38,7 @@ class HistoryMonitor:
|
|||||||
|
|
||||||
def _invoke_callbacks(self):
|
def _invoke_callbacks(self):
|
||||||
if self._last_length < len(self._history):
|
if self._last_length < len(self._history):
|
||||||
for callback in self.callbacks:
|
self._execute_callbacks(self.history)
|
||||||
callback(self.history)
|
|
||||||
|
|
||||||
def watch(self):
|
def watch(self):
|
||||||
self.observer.start()
|
self.observer.start()
|
||||||
|
17
lib/tfw/components/mixins/callback_mixin.py
Normal file
17
lib/tfw/components/mixins/callback_mixin.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from functools import partial
|
||||||
|
|
||||||
|
|
||||||
|
class CallbackMixin:
|
||||||
|
def __init__(self):
|
||||||
|
self._callbacks = []
|
||||||
|
|
||||||
|
def subscribe_callback(self, callback, *args, **kwargs):
|
||||||
|
fun = partial(callback, *args, **kwargs)
|
||||||
|
self._callbacks.append(fun)
|
||||||
|
|
||||||
|
def unsubscribe_callback(self, callback):
|
||||||
|
self._callbacks.remove(callback)
|
||||||
|
|
||||||
|
def _execute_callbacks(self, *args, **kwargs):
|
||||||
|
for callback in self._callbacks:
|
||||||
|
callback(*args, **kwargs)
|
@ -1,13 +1,14 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from transitions import Machine
|
from transitions import Machine
|
||||||
|
|
||||||
|
from tfw.components.mixins.callback_mixin import CallbackMixin
|
||||||
|
|
||||||
class FSMBase:
|
|
||||||
|
class FSMBase(CallbackMixin):
|
||||||
states, transitions = [], []
|
states, transitions = [], []
|
||||||
|
|
||||||
def __init__(self, initial: str = None, accepted_states: List[str] = None):
|
def __init__(self, initial: str = None, accepted_states: List[str] = None):
|
||||||
self.callbacks = []
|
CallbackMixin.__init__(self)
|
||||||
self.accepted_states = accepted_states or [self.states[-1]]
|
self.accepted_states = accepted_states or [self.states[-1]]
|
||||||
self.machine = Machine(model=self,
|
self.machine = Machine(model=self,
|
||||||
states=self.states,
|
states=self.states,
|
||||||
@ -18,14 +19,7 @@ class FSMBase:
|
|||||||
after_state_change='execute_callbacks')
|
after_state_change='execute_callbacks')
|
||||||
|
|
||||||
def execute_callbacks(self, event_data):
|
def execute_callbacks(self, event_data):
|
||||||
for callback in self.callbacks:
|
self._execute_callbacks(event_data.kwargs)
|
||||||
callback(event_data.kwargs)
|
|
||||||
|
|
||||||
def subscribe(self, callback):
|
|
||||||
self.callbacks.append(callback)
|
|
||||||
|
|
||||||
def unsubscribe(self, callback):
|
|
||||||
self.callbacks.remove(callback)
|
|
||||||
|
|
||||||
def is_solved(self):
|
def is_solved(self):
|
||||||
return self.state in self.accepted_states
|
return self.state in self.accepted_states
|
||||||
|
@ -14,7 +14,7 @@ class TFWServer:
|
|||||||
self._fsm = fsm_type()
|
self._fsm = fsm_type()
|
||||||
self._fsm_updater = FSMUpdater(self._fsm)
|
self._fsm_updater = FSMUpdater(self._fsm)
|
||||||
self._fsm_manager = FSMManager(self._fsm)
|
self._fsm_manager = FSMManager(self._fsm)
|
||||||
self._fsm.subscribe(self._fsm_updater.update)
|
self._fsm.subscribe_callback(self._fsm_updater.update)
|
||||||
|
|
||||||
self.application = Application(
|
self.application = Application(
|
||||||
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,
|
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,
|
||||||
|
@ -11,7 +11,7 @@ log = logging.getLogger(__name__)
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
ide = SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD)
|
ide = SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD)
|
||||||
terminado = TerminadoEventHandler('shell')
|
terminado = TerminadoEventHandler('shell')
|
||||||
terminado.historymonitor.callbacks.append(lambda hist: log.debug('User executed command: "{}"'.format(hist[-1])))
|
terminado.historymonitor.subscribe_callback(lambda hist: log.debug('User executed command: "{}"'.format(hist[-1])))
|
||||||
processmanager = ProcessManagingEventHandler('processmanager', ide.monitor)
|
processmanager = ProcessManagingEventHandler('processmanager', ide.monitor)
|
||||||
|
|
||||||
eventhandlers = {ide, terminado, processmanager}
|
eventhandlers = {ide, terminado, processmanager}
|
||||||
|
Loading…
Reference in New Issue
Block a user