Refactor YamlFSM moar

This commit is contained in:
Kristóf Tóth 2018-07-04 18:00:41 +02:00
parent bfa1bffbc5
commit ea76a19595

View File

@ -10,7 +10,7 @@ from tfw import FSMBase
class YamlFSM(FSMBase):
def __init__(self, config_file):
self.config = self.parse_config(config_file)
self.patch_config_callbacks()
self.for_config_states_and_transitions_do(self.patch_config_callbacks)
self.setup_states()
super().__init__() # FSMBase.__init__() requires states
self.setup_transitions()
@ -27,11 +27,13 @@ class YamlFSM(FSMBase):
for transition in self.config['transitions']:
self.add_transition(**transition)
def patch_config_callbacks(self):
topatch = ('on_enter', 'on_exit', 'prepare', 'before', 'after')
def for_config_states_and_transitions_do(self, what):
for array in ('states', 'transitions'):
for json_obj in self.config[array]:
what(json_obj)
def patch_config_callbacks(self, json_obj):
topatch = ('on_enter', 'on_exit', 'prepare', 'before', 'after')
for key in json_obj:
if key in topatch:
json_obj[key] = partial(self.run, json_obj[key])