mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 23:51:21 +00:00
Implement monkey patching callbacks in YamlFSM config
This commit is contained in:
parent
5e4303ac06
commit
022a997dc2
@ -1,3 +1,6 @@
|
|||||||
|
from subprocess import Popen
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from transitions import State
|
from transitions import State
|
||||||
|
|
||||||
@ -7,12 +10,32 @@ 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.states = [State(**state) for state in self.config['states']]
|
self.patch_config_callbacks()
|
||||||
super(YamlFSM, self).__init__()
|
self.setup_states()
|
||||||
for transition in self.config['transitions']:
|
super(YamlFSM, self).__init__() # FSMBase.__init__() requires states
|
||||||
self.machine.add_transition(**transition)
|
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.load(ifile)
|
||||||
|
|
||||||
|
def setup_states(self):
|
||||||
|
self.states = [State(**state) for state in self.config['states']]
|
||||||
|
|
||||||
|
def setup_transitions(self):
|
||||||
|
for transition in self.config['transitions']:
|
||||||
|
self.machine.add_transition(**transition)
|
||||||
|
|
||||||
|
def patch_config_callbacks(self):
|
||||||
|
topatch = {'on_enter', 'on_exit', 'prepare', 'before', 'after'}
|
||||||
|
|
||||||
|
for array in {'states', 'transitions'}:
|
||||||
|
for i, json_obj in enumerate(self.config[array]):
|
||||||
|
for attribute, value in json_obj.items():
|
||||||
|
if attribute in topatch:
|
||||||
|
self.config[array][i][attribute] = partial(self.run, value)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def run(command, event):
|
||||||
|
Popen(command, shell=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user