From 5e4303ac065fd619901adfb8583ac1c0f4718bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 3 Jul 2018 15:14:00 +0200 Subject: [PATCH] Add first version of YamlFSM --- lib/tfw/__init__.py | 1 + lib/tfw/yaml_fsm.py | 18 ++++++++++++++++++ requirements.txt | 1 + 3 files changed, 20 insertions(+) create mode 100644 lib/tfw/yaml_fsm.py diff --git a/lib/tfw/__init__.py b/lib/tfw/__init__.py index 9736ade..53e80d7 100644 --- a/lib/tfw/__init__.py +++ b/lib/tfw/__init__.py @@ -4,3 +4,4 @@ from .event_handler_base import EventHandlerBase, TriggeredEventHandler, BroadcastingEventHandler from .fsm_base import FSMBase from .linear_fsm import LinearFSM +from .yaml_fsm import YamlFSM diff --git a/lib/tfw/yaml_fsm.py b/lib/tfw/yaml_fsm.py new file mode 100644 index 0000000..fb54103 --- /dev/null +++ b/lib/tfw/yaml_fsm.py @@ -0,0 +1,18 @@ +import yaml +from transitions import State + +from tfw import FSMBase + + +class YamlFSM(FSMBase): + def __init__(self, config_file): + self.config = self.parse_config(config_file) + self.states = [State(**state) for state in self.config['states']] + super(YamlFSM, self).__init__() + for transition in self.config['transitions']: + self.machine.add_transition(**transition) + + @staticmethod + def parse_config(config_file): + with open(config_file, 'r') as ifile: + return yaml.load(ifile) diff --git a/requirements.txt b/requirements.txt index cdc46e3..a324ff0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ pyzmq==17.0.0 transitions==0.6.4 terminado==0.8.1 watchdog==0.8.3 +PyYAML==3.12