mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-09-08 23:54:09 +00:00
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
from typing import List
|
|
|
|
from transitions import Machine
|
|
|
|
|
|
class FSMBase:
|
|
states, transitions = [], []
|
|
|
|
def __init__(self, initial: str = None, accepted_states: List[str] = None):
|
|
self.callbacks = []
|
|
self.accepted_states = accepted_states or [self.states[-1]]
|
|
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='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)
|
|
|
|
def is_solved(self):
|
|
return self.state in self.accepted_states
|