annotate py/params.py @ 533:815f811f6a65

Fixed some warnings
author Matt Johnston <matt@ucc.asn.au>
date Tue, 28 Apr 2015 22:44:10 +0800
parents 9b1d71310c83
children 0a1b642e3086
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
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
5 import io
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
6
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 import config
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
8 from utils import W,L,E,EX
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 _FIELD_DEFAULTS = {
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 'fridge_setpoint': 16,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 'fridge_difference': 0.2,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 'overshoot_delay': 720, # 12 minutes
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 'overshoot_factor': 1, # ÂșC
461
1eb68df9f8ab Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
15 'disabled': False,
467
fa79666fb375 add nowort mode
Matt Johnston <matt@ucc.asn.au>
parents: 463
diff changeset
16 'nowort': False,
469
de4abcbe8f46 make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents: 467
diff changeset
17 'fridge_range_lower': 3,
de4abcbe8f46 make fridge air range a parameter
Matt Johnston <matt@ucc.asn.au>
parents: 467
diff changeset
18 'fridge_range_upper': 3,
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 }
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 class Params(dict):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 class Error(Exception):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 pass
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 def __init__(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 self.update(_FIELD_DEFAULTS)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 def __getattr__(self, k):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 return self[k]
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 def __setattr__(self, k, v):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 # fail if we set a bad value
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 self[k]
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 self[k] = v
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
36 def _do_load(self, f):
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
37 try:
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
38 u = json.load(f)
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
39 except Exception as e:
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
40 raise self.Error(e)
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
41
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 for k in u:
463
f2cca8062128 - use the fridge temperature for control too, keep it in a 6deg band
Matt Johnston <matt@ucc.asn.au>
parents: 461
diff changeset
43 if k.startswith('_'):
f2cca8062128 - use the fridge temperature for control too, keep it in a 6deg band
Matt Johnston <matt@ucc.asn.au>
parents: 461
diff changeset
44 continue
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 if k not in self:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 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
47 self.update(u)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
49 L("Loaded parameters")
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
50 L(self.save_string())
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
51
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
52 def load(self, f = None):
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
53 if f:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
54 return self._do_load(f)
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
55 else:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
56 with open(config.PARAMS_FILE, 'r') as f:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
57 try:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
58 return self._do_load(f)
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
59 except IOError as e:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
60 W("Missing parameter file, using defaults. %s" % str(e))
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
61 return
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
62
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
63 def _do_save(self, f):
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 json.dump(self, f, sort_keys=True, indent=4)
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
65 f.write('\n')
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 f.flush()
456
3db3665af2e2 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
67
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
68 def save(self, f = None):
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
69 if f:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
70 return self._do_save(f)
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
71 else:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
72 with file(config.PARAMS_FILE, 'w') as f:
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
73 return self._do_save(f)
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
74
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
75 def save_string(self):
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
76 s = io.StringIO()
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
77 self.save(s)
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 456
diff changeset
78 return s.getvalue()