mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 14:41:20 +00:00
24 lines
841 B
Python
24 lines
841 B
Python
from transitions import Machine
|
|
|
|
|
|
class FSMBase:
|
|
states, transitions = [], []
|
|
|
|
def __init__(self, initial: str = None):
|
|
self.message_handlers = []
|
|
self.machine = Machine(model=self,
|
|
states=self.states,
|
|
transitions=self.transitions,
|
|
initial=initial or self.states[0],
|
|
send_event=True,
|
|
ignore_invalid_triggers=True,
|
|
after_state_change='forward_message')
|
|
|
|
def forward_message(self, event_data):
|
|
message = event_data.kwargs.get('message')
|
|
for msghandler in self.message_handlers:
|
|
msghandler(message)
|
|
|
|
def subscribe_message_handler(self, msghandler):
|
|
self.message_handlers.append(msghandler)
|