annotate py/params.py @ 448:fe729664a5e6

working better. logging works properly, cleanup fridge.off() happens.
author Matt Johnston <matt@ucc.asn.au>
date Sun, 16 Dec 2012 22:14:46 +0800
parents 8fdf86ea41e7
children d03157c7ad60
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import collections
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import json
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import config
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
5 from utils import W,L,E,EX
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 _FIELD_DEFAULTS = {
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 'fridge_setpoint': 16,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 'fridge_difference': 0.2,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 'overshoot_delay': 720, # 12 minutes
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 'overshoot_factor': 1, # ÂșC
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 }
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 class Params(dict):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 class Error(Exception):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 pass
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 def __init__(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 self.update(_FIELD_DEFAULTS)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 def __getattr__(self, k):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 return self[k]
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 def __setattr__(self, k, v):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 # fail if we set a bad value
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 self[k]
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 self[k] = v
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 def load(self, f = None):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 if not f:
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
31 try:
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
32 f = file(config.PARAMS_FILE, 'r')
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
33 except IOError, e:
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
34 W("Missing parameter file, using defaults. %s", e)
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
35 return
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 u = json.load(f)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 for k in u:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 if k not in self:
c5629d79b4ac 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', '???')))
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 self.update(u)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 def save(self, f = None):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 if not f:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 f = file(config.PARAMS_FILE, 'w')
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 json.dump(self, f, sort_keys=True, indent=4)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 f.flush()