mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 00:11:33 +00:00
Refactor YamlFSM
This commit is contained in:
parent
7a92d88b73
commit
bfa1bffbc5
@ -12,29 +12,29 @@ class YamlFSM(FSMBase):
|
|||||||
self.config = self.parse_config(config_file)
|
self.config = self.parse_config(config_file)
|
||||||
self.patch_config_callbacks()
|
self.patch_config_callbacks()
|
||||||
self.setup_states()
|
self.setup_states()
|
||||||
super(YamlFSM, self).__init__() # FSMBase.__init__() requires states
|
super().__init__() # FSMBase.__init__() requires states
|
||||||
self.setup_transitions()
|
self.setup_transitions()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_config(config_file):
|
def parse_config(config_file):
|
||||||
with open(config_file, 'r') as ifile:
|
with open(config_file, 'r') as ifile:
|
||||||
return yaml.load(ifile)
|
return yaml.safe_load(ifile)
|
||||||
|
|
||||||
def setup_states(self):
|
def setup_states(self):
|
||||||
self.states = [State(**state) for state in self.config['states']]
|
self.states = [State(**state) for state in self.config['states']]
|
||||||
|
|
||||||
def setup_transitions(self):
|
def setup_transitions(self):
|
||||||
for transition in self.config['transitions']:
|
for transition in self.config['transitions']:
|
||||||
self.machine.add_transition(**transition)
|
self.add_transition(**transition)
|
||||||
|
|
||||||
def patch_config_callbacks(self):
|
def patch_config_callbacks(self):
|
||||||
topatch = {'on_enter', 'on_exit', 'prepare', 'before', 'after'}
|
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 attribute, value in json_obj.items():
|
for key in json_obj:
|
||||||
if attribute in topatch:
|
if key in topatch:
|
||||||
json_obj[attribute] = partial(self.run, value)
|
json_obj[key] = partial(self.run, json_obj[key])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def run(command, event):
|
def run(command, event):
|
||||||
|
Loading…
Reference in New Issue
Block a user