From 1a2c82da33b6e72c89006fdc5f48f19aa3dab368 Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Sat, 3 Jun 2017 17:34:30 -0400 Subject: [PATCH] differentiate b/t readonly and readwrite config files --- config.py | 26 ++++++++++++++++++-------- config.yaml => config/pyledriver.yaml | 1 - stateMachine.py | 6 +++--- 3 files changed, 21 insertions(+), 12 deletions(-) rename config.yaml => config/pyledriver.yaml (87%) diff --git a/config.py b/config.py index 419f4de..d7a6c44 100644 --- a/config.py +++ b/config.py @@ -5,23 +5,33 @@ Presents an interface for yaml files as a dict-like object import yaml from threading import Lock -class _ConfigFile(): +class _ReadOnlyFile(): + ''' + Opens a yaml file for reading. Intended for config files. + ''' def __init__(self, path): self._path = path - self._lock = Lock() with open(self._path, 'r') as f: self._dict = yaml.safe_load(f) def __getitem__(self, key): return self._dict[key] +class _ReadWriteFile(_ReadOnlyFile): + ''' + Same as above but adds write functionality. Intended for files that retain + program state so that it may return to the same state when recovering from + a crash (eg someone can't crash the system to disarm it) + ''' + def __init__(self, path): + super().__init__(path) + self._lock = Lock() + def __setitem__(self, key, value): with self._lock: self._dict[key] = value - self._sync() - - def _sync(self): - with open(self._path, 'w') as f: - yaml.dump(self._dict, f, default_flow_style=False) + with open(self._path, 'w') as f: + yaml.dump(self._dict, f, default_flow_style=False) -configFile = _ConfigFile('config.yaml') +configFile = _ReadOnlyFile('config/pyledriver.yaml') +stateFile = _ReadWriteFile('config/state.yaml') diff --git a/config.yaml b/config/pyledriver.yaml similarity index 87% rename from config.yaml rename to config/pyledriver.yaml index 94f8313..cb4cbce 100644 --- a/config.yaml +++ b/config/pyledriver.yaml @@ -3,4 +3,3 @@ gmail: recipientList: - natedwarshuis@gmail.com username: natedwarshuis@gmail.com -state: disarmed diff --git a/stateMachine.py b/stateMachine.py index 5b4e1c6..2bab8d0 100644 --- a/stateMachine.py +++ b/stateMachine.py @@ -5,7 +5,7 @@ from functools import partial from collections import namedtuple from auxilary import CountdownTimer, resetUSBDevice -from config import configFile +from config import stateFile from sensors import setupDoorSensor, setupMotionSensor from gmail import intruderAlert from listeners import KeypadListener, PipeListener @@ -110,7 +110,7 @@ class StateMachine: ) ) - self.currentState = getattr(self.states, configFile['state']) + self.currentState = getattr(self.states, stateFile['state']) self.transitionTable = { (self.states.disarmed, SIGNALS.ARM): self.states.disarmedCountdown, @@ -198,7 +198,7 @@ class StateMachine: self.currentState = nextState self.currentState.entry() - configFile['state'] = self.currentState.name + stateFile['state'] = self.currentState.name logger.info('state changed to %s', self.currentState)