Move common parts of FSM to a baseclass

This commit is contained in:
Bálint Bokros 2017-12-06 01:29:09 +01:00
parent 0a3c06109e
commit d3db21f26e
3 changed files with 27 additions and 30 deletions

View File

@ -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()
fsm = Buttons()

20
src/app/fsm_base.py Normal file
View 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)

View File

@ -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()