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): class YamlFSM(FSMBase):
def __init__(self, config_file): def __init__(self, config_file):
self.config = self.parse_config(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() self.setup_states()
super().__init__() # FSMBase.__init__() requires states super().__init__() # FSMBase.__init__() requires states
self.setup_transitions() self.setup_transitions()
@ -27,14 +27,16 @@ class YamlFSM(FSMBase):
for transition in self.config['transitions']: for transition in self.config['transitions']:
self.add_transition(**transition) self.add_transition(**transition)
def patch_config_callbacks(self): def for_config_states_and_transitions_do(self, what):
topatch = ('on_enter', 'on_exit', 'prepare', 'before', 'after')
for array in ('states', 'transitions'): for array in ('states', 'transitions'):
for json_obj in self.config[array]: for json_obj in self.config[array]:
for key in json_obj: what(json_obj)
if key in topatch:
json_obj[key] = partial(self.run, json_obj[key]) 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])
@staticmethod @staticmethod
def run(command, event): def run(command, event):