Mercurial > templog
annotate py/params.py @ 532:9b1d71310c83
better arg parser. seems close to ready
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 11 Apr 2015 23:32:59 +0800 |
parents | 73257bae83ea |
children | 0a1b642e3086 |
rev | line source |
---|---|
444 | 1 # -*- coding: utf-8 -*- |
2 import collections | |
3 import json | |
456 | 4 import signal |
529 | 5 import io |
456 | 6 |
444 | 7 import config |
447 | 8 from utils import W,L,E,EX |
444 | 9 |
10 _FIELD_DEFAULTS = { | |
11 'fridge_setpoint': 16, | |
12 'fridge_difference': 0.2, | |
13 'overshoot_delay': 720, # 12 minutes | |
14 'overshoot_factor': 1, # ÂșC | |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
15 'disabled': False, |
467 | 16 'nowort': False, |
469
de4abcbe8f46
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
467
diff
changeset
|
17 'fridge_range_lower': 3, |
de4abcbe8f46
make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents:
467
diff
changeset
|
18 'fridge_range_upper': 3, |
444 | 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 | |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
36 def _do_load(self, f): |
456 | 37 try: |
38 u = json.load(f) | |
529 | 39 except Exception as e: |
456 | 40 raise self.Error(e) |
41 | |
444 | 42 for k in u: |
463
f2cca8062128
- use the fridge temperature for control too, keep it in a 6deg band
Matt Johnston <matt@ucc.asn.au>
parents:
461
diff
changeset
|
43 if k.startswith('_'): |
f2cca8062128
- use the fridge temperature for control too, keep it in a 6deg band
Matt Johnston <matt@ucc.asn.au>
parents:
461
diff
changeset
|
44 continue |
444 | 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 | |
459 | 49 L("Loaded parameters") |
50 L(self.save_string()) | |
51 | |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
52 def load(self, f = None): |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
53 if f: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
54 return self._do_load(f) |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
55 else: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
56 with open(config.PARAMS_FILE, 'r') as f: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
57 try: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
58 return self._do_load(f) |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
59 except IOError as e: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
60 W("Missing parameter file, using defaults. %s" % str(e)) |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
61 return |
459 | 62 |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
63 def _do_save(self, f): |
444 | 64 json.dump(self, f, sort_keys=True, indent=4) |
454 | 65 f.write('\n') |
444 | 66 f.flush() |
456 | 67 |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
68 def save(self, f = None): |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
69 if f: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
70 return self._do_save(f) |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
71 else: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
72 with file(config.PARAMS_FILE, 'w') as f: |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
73 return self._do_save(f) |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
74 |
459 | 75 def save_string(self): |
529 | 76 s = io.StringIO() |
459 | 77 self.save(s) |
78 return s.getvalue() |