Mercurial > templog
comparison py/params.py @ 259:26eee8591f61
long polling works
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 09 Jun 2015 23:27:44 +0800 |
parents | 0a1b642e3086 |
children | ef3a75128116 |
comparison
equal
deleted
inserted
replaced
256:6d06795aefbb | 259:26eee8591f61 |
---|---|
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 io | |
6 import tempfile | 5 import tempfile |
6 import os | |
7 import binascii | |
7 | 8 |
8 import config | 9 import config |
9 from utils import W,L,E,EX | 10 from utils import W,L,E,EX |
10 import utils | 11 import utils |
11 | 12 |
24 class Error(Exception): | 25 class Error(Exception): |
25 pass | 26 pass |
26 | 27 |
27 def __init__(self): | 28 def __init__(self): |
28 self.update(_FIELD_DEFAULTS) | 29 self.update(_FIELD_DEFAULTS) |
29 self._epoch = None | 30 self._set_epoch(None) |
30 | 31 |
31 def __getattr__(self, k): | 32 def __getattr__(self, k): |
32 return self[k] | 33 return self[k] |
33 | 34 |
34 def __setattr__(self, k, v): | 35 def __setattr__(self, k, v): |
35 # fail if we set a bad value | 36 # fail if we set a bad value |
36 self[k] | 37 self[k] |
37 self[k] = v | 38 self[k] = v |
38 | 39 |
40 def _set_epoch(self, epoch): | |
41 # since __setattr__ is overridden | |
42 object.__setattr__(self, '_epoch', epoch) | |
43 | |
39 def _do_load(self, f): | 44 def _do_load(self, f): |
40 try: | 45 try: |
41 u = json.load(f) | 46 u = utils.json_load_round_float(f.read()) |
42 except Exception as e: | 47 except Exception as e: |
43 raise self.Error(e) | 48 raise self.Error(e) |
44 | 49 |
45 for k in u: | 50 for k in u: |
46 if k.startswith('_'): | 51 if k.startswith('_'): |
47 continue | 52 continue |
48 if k not in self: | 53 if k not in self: |
49 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) | 54 raise self.Error("Unknown parameter %s=%s in file '%s'" % (str(k), str(u[k]), getattr(f, 'name', '???'))) |
50 self.update(u) | 55 self.update(u) |
51 self._epoch = utils.hexnonce() | 56 # new epoch, 120 random bits |
57 self._set_epoch(binascii.hexlify(os.urandom(15)).decode()) | |
52 | 58 |
53 L("Loaded parameters") | 59 L("Loaded parameters") |
54 L(self.save_string()) | 60 L(self.save_string()) |
55 | 61 |
56 def load(self, f = None): | 62 def load(self, f = None): |
85 if tb == int: | 91 if tb == int: |
86 tb = float | 92 tb = float |
87 | 93 |
88 return ta == tb | 94 return ta == tb |
89 | 95 |
90 if self.keys() != new_params.keys(): | 96 if self.keys() != params.keys(): |
91 diff = self.keys() ^ new_params.keys() | 97 diff = self.keys() ^ params.keys() |
92 E("Mismatching params, %s" % str(diff)) | 98 E("Mismatching params, %s" % str(diff)) |
93 return False | 99 return False |
94 | 100 |
95 for k, v in new_params.items(): | 101 for k, v in params.items(): |
96 if not same_type(v, self[k]): | 102 if not same_type(v, self[k]): |
97 E("Bad type for %s" % k) | 103 E("Bad type for %s" % k) |
98 return False | 104 return False |
99 | 105 |
100 dir = os.path.dirname(config.PARAMS_FILE) | 106 dir = os.path.dirname(config.PARAMS_FILE) |
101 try: | 107 try: |
102 t = tempfile.NamedTemporaryFile(prefix='config', | 108 t = tempfile.NamedTemporaryFile(prefix='config', |
109 mode='w+t', | |
103 dir = dir, | 110 dir = dir, |
104 delete = False) | 111 delete = False) |
105 | 112 |
106 t.write(json.dumps(new_params, sort_keys=True, indent=4)+'\n') | 113 out = json.dumps(params, sort_keys=True, indent=4)+'\n' |
114 t.write(out) | |
107 name = t.name | 115 name = t.name |
108 t.close() | 116 t.close() |
109 | 117 |
110 os.rename(name, config.PARAMS_FILE) | 118 os.rename(name, config.PARAMS_FILE) |
111 return True | 119 return True |
112 except Exception as e: | 120 except Exception as e: |
113 E("Problem: %s" % e) | 121 EX("Problem: %s" % e) |
114 return False | 122 return False |
115 | 123 |
116 def save_string(self): | 124 def save_string(self): |
117 return json.dumps(self, sort_keys=True, indent=4) | 125 return json.dumps(self, sort_keys=True, indent=4) |