Mercurial > templog
annotate py/params.py @ 529:5201f441bf4a
run 2to3
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 10 Apr 2015 23:59:16 +0800 |
parents | dd8895652485 |
children | e39ed85d87a5 |
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 | |
36 def load(self, f = None): | |
37 if not f: | |
447 | 38 try: |
39 f = file(config.PARAMS_FILE, 'r') | |
529 | 40 except IOError as e: |
447 | 41 W("Missing parameter file, using defaults. %s", e) |
42 return | |
456 | 43 try: |
44 u = json.load(f) | |
529 | 45 except Exception as e: |
456 | 46 raise self.Error(e) |
47 | |
444 | 48 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
|
49 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
|
50 continue |
444 | 51 if k not in self: |
52 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) | |
53 self.update(u) | |
54 | |
459 | 55 L("Loaded parameters") |
56 L(self.save_string()) | |
57 | |
58 | |
444 | 59 def save(self, f = None): |
60 if not f: | |
61 f = file(config.PARAMS_FILE, 'w') | |
62 json.dump(self, f, sort_keys=True, indent=4) | |
454 | 63 f.write('\n') |
444 | 64 f.flush() |
456 | 65 |
459 | 66 def save_string(self): |
529 | 67 s = io.StringIO() |
459 | 68 self.save(s) |
69 return s.getvalue() |