class Trail: IDLE = 'idle' REGISTERED = 'registered' HOLD = 'hold' ACTIVE = 'active' def __init__(self, path=None): self.path = path self.store = Store() self._fsm = Fysom( initial=Trail.IDLE, events=[ ('register', Trail.IDLE, Trail.REGISTERED), ('prepare', Trail.REGISTERED, Trail.HOLD), ('activate', Trail.HOLD, Trail.ACTIVE), ('resolve', Trail.ACTIVE, Trail.IDLE), ('resolve', Trail.HOLD, Trail.IDLE), ('resolve', Trail.REGISTERED, Trail.IDLE)], callbacks=[ ('onbeforeregister', self._onbeforeregister), ('onregistered', self._registered), ('onidle', self._onidle)]) @property def status(self): return self._fsm.current def onchangestate(self, on_change): self._fsm.onchangestate = on_change def register(self): self._fsm.register() def prepare(self): self._fsm.prepare() def activate(self): self._fsm.activate() def resolve(self): self._fsm.resolve() def _onbeforeregister(self, e): self.store.register(self.path) def _registered(self, e): self.prepare() def _onidle(self, e): if e.src is not "none": self.store.unregister(self.path)
class BaseExitSection(object): def __init__(self): self._can_depart = False self._fsm = Fysom( initial=Section.IDLE, final=Section.IDLE, events=[ ('wait', Section.IDLE, Section.WAITING), ('depart', Section.IDLE, Section.DEPARTING), ('depart', Section.WAITING, Section.DEPARTING), ('resolve', Section.DEPARTING, Section.IDLE)], callbacks=[ ('onbeforedepart', self._onbeforedepart), ('ondepart', self.__ondepart__)]) @property def status(self): return self._fsm.current @property def can_depart(self): return self._can_depart def request_depart(self): try: self._fsm.depart() except Canceled: self._fsm.wait() def depart(self): self._can_depart = True if self.status == Section.WAITING: self._fsm.depart() def departed(self): self._fsm.resolve() def _onbeforedepart(self, e): return self.can_depart def __ondepart__(self, e): self._can_depart = False self._ondepart(e) def _ondepart(self, e): pass