annotate py/params.py @ 456:3db3665af2e2

reload config on SIGHUP
author Matt Johnston <matt@ucc.asn.au>
date Thu, 03 Jan 2013 20:12:07 +0800
parents dce9f7841696
children 256505f98c4d
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
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
4 import signal
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
5
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
6 import gevent
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
7
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 import config
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
9 from utils import W,L,E,EX
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 _FIELD_DEFAULTS = {
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 'fridge_setpoint': 16,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 'fridge_difference': 0.2,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 'overshoot_delay': 720, # 12 minutes
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 'overshoot_factor': 1, # ÂșC
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 }
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 class Params(dict):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 class Error(Exception):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 pass
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 def __init__(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 self.update(_FIELD_DEFAULTS)
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
24 gevent.signal(signal.SIGHUP, self.reload_signal)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 def __getattr__(self, k):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 return self[k]
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 __setattr__(self, k, v):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 # fail if we set a bad value
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 self[k]
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 self[k] = v
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 def load(self, f = None):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 if not f:
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
36 try:
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
37 f = file(config.PARAMS_FILE, 'r')
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
38 except IOError, e:
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
39 W("Missing parameter file, using defaults. %s", e)
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
40 return
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
41 try:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
42 u = json.load(f)
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
43 except Exception, e:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
44 raise self.Error(e)
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
45
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 for k in u:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 if k not in self:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 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
49 self.update(u)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 def save(self, f = None):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 if not f:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 f = file(config.PARAMS_FILE, 'w')
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 json.dump(self, f, sort_keys=True, indent=4)
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
55 f.write('\n')
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 f.flush()
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
57
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
58 def reload_signal(self):
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
59 try:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
60 self.load()
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
61 L("Reloaded params")
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
62 except self.Error, e:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
63 W("Problem reloading: %s" % str(e))