From ea76a195951f1f79168a6bfb094b836bead22923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 4 Jul 2018 18:00:41 +0200 Subject: [PATCH] Refactor YamlFSM moar --- lib/tfw/yaml_fsm.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/tfw/yaml_fsm.py b/lib/tfw/yaml_fsm.py index 9b5f331..fe854ba 100644 --- a/lib/tfw/yaml_fsm.py +++ b/lib/tfw/yaml_fsm.py @@ -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,14 +27,16 @@ 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]: - for key in json_obj: - if key in topatch: - json_obj[key] = partial(self.run, json_obj[key]) + 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]) @staticmethod def run(command, event):