145
|
1 # -*- coding: utf-8 -*- |
|
2 import collections |
|
3 import json |
|
4 import config |
148
|
5 from utils import W,L,E,EX |
145
|
6 |
|
7 _FIELD_DEFAULTS = { |
|
8 'fridge_setpoint': 16, |
|
9 'fridge_difference': 0.2, |
|
10 'overshoot_delay': 720, # 12 minutes |
|
11 'overshoot_factor': 1, # ÂșC |
|
12 } |
|
13 |
|
14 class Params(dict): |
|
15 class Error(Exception): |
|
16 pass |
|
17 |
|
18 def __init__(self): |
|
19 self.update(_FIELD_DEFAULTS) |
|
20 |
|
21 def __getattr__(self, k): |
|
22 return self[k] |
|
23 |
|
24 def __setattr__(self, k, v): |
|
25 # fail if we set a bad value |
|
26 self[k] |
|
27 self[k] = v |
|
28 |
|
29 def load(self, f = None): |
|
30 if not f: |
148
|
31 try: |
|
32 f = file(config.PARAMS_FILE, 'r') |
|
33 except IOError, e: |
|
34 W("Missing parameter file, using defaults. %s", e) |
|
35 return |
145
|
36 u = json.load(f) |
|
37 for k in u: |
|
38 if k not in self: |
|
39 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) |
|
40 self.update(u) |
|
41 |
|
42 def save(self, f = None): |
|
43 if not f: |
|
44 f = file(config.PARAMS_FILE, 'w') |
|
45 json.dump(self, f, sort_keys=True, indent=4) |
155
|
46 f.write('\n') |
145
|
47 f.flush() |