diff --git a/auxilary.py b/auxilary.py index 6b22250..54ae887 100644 --- a/auxilary.py +++ b/auxilary.py @@ -2,30 +2,11 @@ Various helper functions and classes ''' -import time, yaml, os +import time, os from subprocess import check_output, DEVNULL, CalledProcessError from threading import Event from exceptionThreading import ExceptionThread -class ConfigFile(): - ''' - Presents a config yaml file as a dict-like object - ''' - def __init__(self, path): - self._path = path - with open(self._path, 'r') as f: - self._dict = yaml.safe_load(f) - - def __getitem__(self, key): - return self._dict[key] - - def __setitem__(self, key, value): - self._dict[key] = value - - def sync(self): - with open(self._path, 'w') as f: - yaml.dump(self._dict, f, default_flow_style=False) - class CountdownTimer(ExceptionThread): ''' Launches thread which self terminates after some time (given in seconds). diff --git a/config.py b/config.py new file mode 100644 index 0000000..419f4de --- /dev/null +++ b/config.py @@ -0,0 +1,27 @@ +''' +Presents an interface for yaml files as a dict-like object +''' + +import yaml +from threading import Lock + +class _ConfigFile(): + 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] + + 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) + +configFile = _ConfigFile('config.yaml') diff --git a/gmail.py b/gmail.py index 26d5093..4dbd416 100644 --- a/gmail.py +++ b/gmail.py @@ -1,5 +1,5 @@ import logging, time -from auxilary import ConfigFile +from config import configFile from exceptionThreading import async from smtplib import SMTP from datetime import datetime @@ -8,7 +8,7 @@ from email.mime.text import MIMEText logger = logging.getLogger(__name__) -gmail = ConfigFile('config.yaml')['gmail'] +gmail = configFile['gmail'] def _getNextDate(): m = datetime.now().month + 1 diff --git a/stateMachine.py b/stateMachine.py index b46768c..5b4e1c6 100644 --- a/stateMachine.py +++ b/stateMachine.py @@ -4,7 +4,8 @@ from threading import Lock from functools import partial from collections import namedtuple -from auxilary import CountdownTimer, ConfigFile, resetUSBDevice +from auxilary import CountdownTimer, resetUSBDevice +from config import configFile from sensors import setupDoorSensor, setupMotionSensor from gmail import intruderAlert from listeners import KeypadListener, PipeListener @@ -69,7 +70,6 @@ class State: class StateMachine: def __init__(self): self.soundLib = SoundLib() - self._cfg = ConfigFile('config.yaml') def startTimer(t, sound): self._timer = CountdownTimer(t, partial(self.selectState, SIGNALS.TIMOUT), sound) @@ -110,7 +110,7 @@ class StateMachine: ) ) - self.currentState = getattr(self.states, self._cfg['state']) + self.currentState = getattr(self.states, configFile['state']) self.transitionTable = { (self.states.disarmed, SIGNALS.ARM): self.states.disarmedCountdown, @@ -191,15 +191,14 @@ class StateMachine: self.currentState.entry() def selectState(self, signal): - with self._lock + with self._lock: nextState = self.currentState.next(signal) if nextState != self.currentState: self.currentState.exit() self.currentState = nextState self.currentState.entry() - self._cfg['state'] = self.currentState.name - self._cfg.sync() + configFile['state'] = self.currentState.name logger.info('state changed to %s', self.currentState)