summaryrefslogtreecommitdiff
path: root/lorrycontroller/workingstate.py
diff options
context:
space:
mode:
Diffstat (limited to 'lorrycontroller/workingstate.py')
-rw-r--r--lorrycontroller/workingstate.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/lorrycontroller/workingstate.py b/lorrycontroller/workingstate.py
index 39d0065..acaf55b 100644
--- a/lorrycontroller/workingstate.py
+++ b/lorrycontroller/workingstate.py
@@ -4,6 +4,35 @@
import json
import os
import logging
+import string
+
+class LorryFileRunner(object):
+ def __init__(self, mgr, lorryname):
+ self.mgr = mgr
+ self.lorryname = lorryname
+ self.lorryfile = os.path.join(self.mgr.workdir,
+ self._esc(lorryname) + ".lorry")
+
+ def _esc(self, name):
+ valid_chars = string.digits + string.letters + '%_'
+ transl = lambda x: x if x in valid_chars else '_'
+ return ''.join([transl(x) for x in name])
+
+ def __enter__(self):
+ lorry_obj = { self.lorryname:
+ self.mgr.lorry_state[self.lorryname]['lorry'] }
+ with open(self.lorryfile, "w") as fh:
+ json.dump(lorry_obj, fh)
+ fh.write("\n")
+ return self
+
+ def __exit__(self, exctype, excvalue, exctraceback):
+ os.unlink(self.lorryfile)
+
+ def run_lorry(self, *args):
+ cmdargs = list(args)
+ cmdargs.append(self.lorryfile)
+ self.mgr.app.runcmd(cmdargs)
class WorkingStateManager(object):
'''Manage the working state of lorry-controller'''
@@ -35,3 +64,5 @@ class WorkingStateManager(object):
json.dump(self.lorry_state, fh, sort_keys=True, indent=4)
fh.write("\n")
+ def runner(self, lorryname):
+ return LorryFileRunner(self, lorryname)