Merge pull request #18 from avatao-content/callbackmixin

Refactor callback subscription stuff to a mixin
This commit is contained in:
Bokros Bálint 2018-03-07 12:05:03 +01:00 committed by GitHub
commit be6f657de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 21 deletions

View File

@ -2,6 +2,8 @@ from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from os.path import dirname
from tfw.components.mixins.callback_mixin import CallbackMixin
class CallbackEventHandler(PatternMatchingEventHandler):
def __init__(self, files, *callbacks):
@ -13,12 +15,12 @@ class CallbackEventHandler(PatternMatchingEventHandler):
callback()
class HistoryMonitor:
class HistoryMonitor(CallbackMixin):
def __init__(self, histfile):
CallbackMixin.__init__(self)
self.histfile = histfile
self._history = []
self._last_length = len(self._history)
self._callbacks = []
self.observer = Observer()
self.observer.schedule(CallbackEventHandler([self.histfile],
self._fetch_history,
@ -29,10 +31,6 @@ class HistoryMonitor:
def history(self):
return self._history
@property
def callbacks(self):
return self._callbacks
def _fetch_history(self):
self._last_length = len(self._history)
with open(self.histfile, 'r') as ifile:
@ -40,8 +38,7 @@ class HistoryMonitor:
def _invoke_callbacks(self):
if self._last_length < len(self._history):
for callback in self.callbacks:
callback(self.history)
self._execute_callbacks(self.history)
def watch(self):
self.observer.start()

View 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)

View File

@ -1,13 +1,14 @@
from typing import List
from transitions import Machine
from tfw.components.mixins.callback_mixin import CallbackMixin
class FSMBase:
class FSMBase(CallbackMixin):
states, transitions = [], []
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.machine = Machine(model=self,
states=self.states,
@ -18,14 +19,7 @@ class FSMBase:
after_state_change='execute_callbacks')
def execute_callbacks(self, event_data):
for callback in self.callbacks:
callback(event_data.kwargs)
def subscribe(self, callback):
self.callbacks.append(callback)
def unsubscribe(self, callback):
self.callbacks.remove(callback)
self._execute_callbacks(event_data.kwargs)
def is_solved(self):
return self.state in self.accepted_states

View File

@ -14,7 +14,7 @@ class TFWServer:
self._fsm = fsm_type()
self._fsm_updater = FSMUpdater(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(
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,

View File

@ -11,7 +11,7 @@ log = logging.getLogger(__name__)
if __name__ == '__main__':
ide = SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD)
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)
eventhandlers = {ide, terminado, processmanager}