annotate py/params.py @ 148:b32e5a11a4cb

few updates, seems to run
author Matt Johnston <matt@ucc.asn.au>
date Sat, 15 Dec 2012 23:49:08 +0800
parents 6517ddee3187
children d03157c7ad60
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
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
5 from utils import W,L,E,EX
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 _FIELD_DEFAULTS = {
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 'fridge_setpoint': 16,
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 'fridge_difference': 0.2,
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 'overshoot_delay': 720, # 12 minutes
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 'overshoot_factor': 1, # ÂșC
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
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 class Params(dict):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 class Error(Exception):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 pass
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 def __init__(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 self.update(_FIELD_DEFAULTS)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 def __getattr__(self, k):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 return self[k]
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 def __setattr__(self, k, v):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 # fail if we set a bad value
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 self[k]
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 self[k] = v
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 def load(self, f = None):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 if not f:
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
31 try:
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
32 f = file(config.PARAMS_FILE, 'r')
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
33 except IOError, e:
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
34 W("Missing parameter file, using defaults. %s", e)
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
35 return
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 u = json.load(f)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 for k in u:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 if k not in self:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 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
40 self.update(u)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 def save(self, f = None):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 if not f:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 f = file(config.PARAMS_FILE, 'w')
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 json.dump(self, f, sort_keys=True, indent=4)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 f.flush()