differentiate b/t readonly and readwrite config files

This commit is contained in:
petrucci4prez 2017-06-03 17:34:30 -04:00
parent c543cebf57
commit 1a2c82da33
3 changed files with 21 additions and 12 deletions

View File

@ -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()
with open(self._path, 'w') as f:
yaml.dump(self._dict, f, default_flow_style=False)
def _sync(self):
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')

View File

@ -3,4 +3,3 @@ gmail:
recipientList:
- natedwarshuis@gmail.com
username: natedwarshuis@gmail.com
state: disarmed

View File

@ -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)