mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-09-09 00:14:10 +00:00
27 lines
946 B
Python
27 lines
946 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)
|
|
|
|
def unsubscribe_message_handler(self, msghandler):
|
|
self.message_handlers.remove(msghandler)
|