annotate py/params.py @ 145:6517ddee3187

few more bits
author Matt Johnston <matt@ucc.asn.au>
date Thu, 29 Nov 2012 23:50:40 +0800
parents
children b32e5a11a4cb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import collections
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import json
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import config
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 _FIELD_DEFAULTS = {
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 'fridge_setpoint': 16,
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 'fridge_difference': 0.2,
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 'overshoot_delay': 720, # 12 minutes
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 'overshoot_factor': 1, # ÂșC
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 }
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 class Params(dict):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 class Error(Exception):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 pass
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 def __init__(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 self.update(_FIELD_DEFAULTS)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 def __getattr__(self, k):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 return self[k]
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 def __setattr__(self, k, v):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 # fail if we set a bad value
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 self[k]
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 self[k] = v
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 def load(self, f = None):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 if not f:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 f = file(config.PARAMS_FILE, 'r')
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 u = json.load(f)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 for k in u:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 if k not in self:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???')))
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 self.update(u)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 def save(self, f = None):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 if not f:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 f = file(config.PARAMS_FILE, 'w')
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 json.dump(self, f, sort_keys=True, indent=4)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 f.flush()