diff options
Diffstat (limited to 'lorrycontroller/workingstate.py')
-rw-r--r-- | lorrycontroller/workingstate.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lorrycontroller/workingstate.py b/lorrycontroller/workingstate.py new file mode 100644 index 0000000..39d0065 --- /dev/null +++ b/lorrycontroller/workingstate.py @@ -0,0 +1,37 @@ +# Copyright (C) 2012 Codethink Limited +# + +import json +import os +import logging + +class WorkingStateManager(object): + '''Manage the working state of lorry-controller''' + + def __init__(self, app): + self.app = app + self.workdir = os.path.join(self.app.settings['work-area'], 'work') + + def __enter__(self): + self._load_state() + return self + + def __exit__(self, exctype, excvalue, exctraceback): + self.save_state() + + def _load_state(self): + self.lorry_state_file = os.path.join(self.workdir, + "last-lorry-state.json") + if os.path.exists(self.lorry_state_file): + logging.debug("Loading state file: %s" % self.lorry_state_file) + with open(self.lorry_state_file, "r") as fh: + self.lorry_state = json.load(fh) + else: + self.lorry_state = dict() + + def save_state(self): + logging.debug("Serialising state: %s" % self.lorry_state_file) + with open(self.lorry_state_file, "w") as fh: + json.dump(self.lorry_state, fh, sort_keys=True, indent=4) + fh.write("\n") + |