Mercurial > templog
annotate py/params.py @ 236:88dfac30d065
gevent
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 06 May 2015 19:34:23 +0800 |
parents | 19569cb5ed46 |
children | 0a1b642e3086 |
rev | line source |
---|---|
145 | 1 # -*- coding: utf-8 -*- |
2 import collections | |
3 import json | |
157 | 4 import signal |
230 | 5 import io |
157 | 6 |
145 | 7 import config |
148 | 8 from utils import W,L,E,EX |
145 | 9 |
10 _FIELD_DEFAULTS = { | |
11 'fridge_setpoint': 16, | |
12 'fridge_difference': 0.2, | |
13 'overshoot_delay': 720, # 12 minutes | |
14 'overshoot_factor': 1, # ÂșC | |
162
d73077e8cd67
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
160
diff
changeset
|
15 'disabled': False, |
168 | 16 'nowort': False, |
170
78255c49bf9a
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
168
diff
changeset
|
17 'fridge_range_lower': 3, |
78255c49bf9a
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
168
diff
changeset
|
18 'fridge_range_upper': 3, |
145 | 19 } |
20 | |
21 class Params(dict): | |
22 class Error(Exception): | |
23 pass | |
24 | |
25 def __init__(self): | |
26 self.update(_FIELD_DEFAULTS) | |
27 | |
28 def __getattr__(self, k): | |
29 return self[k] | |
30 | |
31 def __setattr__(self, k, v): | |
32 # fail if we set a bad value | |
33 self[k] | |
34 self[k] = v | |
35 | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
36 def _do_load(self, f): |
157 | 37 try: |
38 u = json.load(f) | |
230 | 39 except Exception as e: |
157 | 40 raise self.Error(e) |
41 | |
145 | 42 for k in u: |
166
5d5424acfed0
- use the fridge temperature for control too, keep it in a 6deg band
Matt Johnston <matt@ucc.asn.au>
parents:
162
diff
changeset
|
43 if k.startswith('_'): |
5d5424acfed0
- use the fridge temperature for control too, keep it in a 6deg band
Matt Johnston <matt@ucc.asn.au>
parents:
162
diff
changeset
|
44 continue |
145 | 45 if k not in self: |
46 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) | |
47 self.update(u) | |
48 | |
160 | 49 L("Loaded parameters") |
50 L(self.save_string()) | |
51 | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
52 def load(self, f = None): |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
53 if f: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
54 return self._do_load(f) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
55 else: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
56 with open(config.PARAMS_FILE, 'r') as f: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
57 try: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
58 return self._do_load(f) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
59 except IOError as e: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
60 W("Missing parameter file, using defaults. %s" % str(e)) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
61 return |
160 | 62 |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
63 def _do_save(self, f): |
145 | 64 json.dump(self, f, sort_keys=True, indent=4) |
155 | 65 f.write('\n') |
145 | 66 f.flush() |
157 | 67 |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
68 def save(self, f = None): |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
69 if f: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
70 return self._do_save(f) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
71 else: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
72 with file(config.PARAMS_FILE, 'w') as f: |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
73 return self._do_save(f) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
74 |
160 | 75 def save_string(self): |
230 | 76 s = io.StringIO() |
160 | 77 self.save(s) |
78 return s.getvalue() |