mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 07:41:32 +00:00
Move common parts of FSM to a baseclass
This commit is contained in:
parent
0a3c06109e
commit
d3db21f26e
@ -1,8 +1,7 @@
|
||||
from transitions import Machine
|
||||
from fsm_base import FSMBase
|
||||
|
||||
import event_handler_connector
|
||||
|
||||
class Buttons:
|
||||
class Buttons(FSMBase):
|
||||
states = ['A', 'B', 'C']
|
||||
transitions = [
|
||||
{'trigger': 'anchor_a', 'source': 'A', 'dest': 'B'},
|
||||
@ -11,17 +10,7 @@ class Buttons:
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
self.machine = Machine(model=self,
|
||||
states=Buttons.states,
|
||||
transitions=Buttons.transitions,
|
||||
initial='A',
|
||||
send_event=True,
|
||||
ignore_invalid_triggers=True,
|
||||
after_state_change='forward_message')
|
||||
|
||||
def forward_message(self, event_data):
|
||||
message = event_data.kwargs.get('message')
|
||||
event_handler_connector.send_message(message)
|
||||
super().__init__('A')
|
||||
|
||||
|
||||
fsm = Buttons()
|
20
src/app/fsm_base.py
Normal file
20
src/app/fsm_base.py
Normal file
@ -0,0 +1,20 @@
|
||||
from transitions import Machine
|
||||
|
||||
import event_handler_connector
|
||||
|
||||
|
||||
class FSMBase:
|
||||
states, transitions = [], []
|
||||
|
||||
def __init__(self, initial: str = None):
|
||||
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')
|
||||
event_handler_connector.send_message(message)
|
@ -1,9 +1,7 @@
|
||||
from transitions import Machine
|
||||
|
||||
import event_handler_connector
|
||||
from fsm_base import FSMBase
|
||||
|
||||
|
||||
class SQLInjectionFSM:
|
||||
class SQLInjectionFSM(FSMBase):
|
||||
states = ['start', 'stripped_code', 'sql', 'commented_code', 'sql_with_substitutions', 'sql_output', 'end']
|
||||
transitions = [
|
||||
{'trigger': 'anchor_webide', 'source': 'start', 'dest': 'stripped_code'},
|
||||
@ -16,17 +14,7 @@ class SQLInjectionFSM:
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
self.machine = Machine(model=self,
|
||||
states=SQLInjectionFSM.states,
|
||||
transitions=SQLInjectionFSM.transitions,
|
||||
initial='start',
|
||||
send_event=True,
|
||||
ignore_invalid_triggers=True,
|
||||
after_state_change='forward_message')
|
||||
|
||||
def forward_message(self, event_data):
|
||||
message = event_data.kwargs.get('message')
|
||||
event_handler_connector.send_message(message)
|
||||
super().__init__('start')
|
||||
|
||||
|
||||
fsm = SQLInjectionFSM()
|
||||
|
Loading…
Reference in New Issue
Block a user