comparison py/params.py @ 465:a40f30fad8f6

Merge
author Matt Johnston <matt@ucc.asn.au>
date Mon, 21 Jan 2013 07:25:52 +0800
parents 1eb68df9f8ab
children 5d5424acfed0
comparison
equal deleted inserted replaced
464:c3926e7cfb0c 465:a40f30fad8f6
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 import collections 2 import collections
3 import json 3 import json
4 import signal 4 import signal
5 import StringIO
5 6
6 import gevent 7 import gevent
7 8
8 import config 9 import config
9 from utils import W,L,E,EX 10 from utils import W,L,E,EX
11 _FIELD_DEFAULTS = { 12 _FIELD_DEFAULTS = {
12 'fridge_setpoint': 16, 13 'fridge_setpoint': 16,
13 'fridge_difference': 0.2, 14 'fridge_difference': 0.2,
14 'overshoot_delay': 720, # 12 minutes 15 'overshoot_delay': 720, # 12 minutes
15 'overshoot_factor': 1, # ºC 16 'overshoot_factor': 1, # ºC
17 'disabled': False,
16 } 18 }
17 19
18 class Params(dict): 20 class Params(dict):
19 class Error(Exception): 21 class Error(Exception):
20 pass 22 pass
46 for k in u: 48 for k in u:
47 if k not in self: 49 if k not in self:
48 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) 50 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???')))
49 self.update(u) 51 self.update(u)
50 52
53 L("Loaded parameters")
54 L(self.save_string())
55
56
51 def save(self, f = None): 57 def save(self, f = None):
52 if not f: 58 if not f:
53 f = file(config.PARAMS_FILE, 'w') 59 f = file(config.PARAMS_FILE, 'w')
54 json.dump(self, f, sort_keys=True, indent=4) 60 json.dump(self, f, sort_keys=True, indent=4)
55 f.write('\n') 61 f.write('\n')
56 f.flush() 62 f.flush()
57 63
64 def save_string(self):
65 s = StringIO.StringIO()
66 self.save(s)
67 return s.getvalue()
68
58 def reload_signal(self): 69 def reload_signal(self):
59 try: 70 try:
60 self.load() 71 self.load()
61 L("Reloaded params") 72 L("Reloaded.")
62 except self.Error, e: 73 except self.Error, e:
63 W("Problem reloading: %s" % str(e)) 74 W("Problem reloading: %s" % str(e))