annotate py/params.py @ 160:256505f98c4d

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