Implement crude FSMManager – wip

This commit is contained in:
Kristóf Tóth 2018-02-26 14:26:14 +01:00
parent 41a32437b2
commit 5e9eb88bfc

View File

@ -36,6 +36,30 @@ class TFWServer:
self.application.listen(port)
class FSMManager:
def __init__(self, fsm_type):
self.fsm = fsm_type()
self.trigger_handlers = {}
def trigger(self, trigger):
self._prepare_handlerlist(trigger)
if all((handler() for handler in self.trigger_handlers[trigger])):
try: self.fsm.trigger(trigger, message='')
except AttributeError: log.debug('FSM failed to execute nonexistent trigger: "{}"'.format(trigger))
def subscribe_handler(self, trigger, handler):
self._prepare_handlerlist(trigger)
self.trigger_handlers[trigger].append(handler)
def unsubscribe_handler(self, trigger, handler):
self._prepare_handlerlist(trigger)
self.trigger_handlers[trigger].append(handler)
def _prepare_handlerlist(self, trigger):
try: self.trigger_handlers[trigger]
except KeyError: self.trigger_handlers[trigger] = []
class FSMUpdater:
def __init__(self, fsm):
self.fsm = fsm