annotate py/utils.py @ 265:78c542f03030

Limit log
author Matt Johnston <matt@ucc.asn.au>
date Sat, 13 Jun 2015 22:49:14 +0800
parents 26eee8591f61
children 9d0313b685ae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import sys
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
3 import ctypes
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import time
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import select
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 import logging
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 230
diff changeset
7 import binascii
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
8 import json
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
9 import datetime
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
11 D = logging.debug
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 L = logging.info
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 W = logging.warning
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 E = logging.error
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 DEFAULT_TRIES = 3
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 READLINE_SELECT_TIMEOUT = 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
19 def EX(msg, *args, **kwargs):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
20 kwargs['exc_info'] = True
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
21 logging.error(msg, *args, **kwargs)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
22
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 clock_gettime = None
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 no_clock_gettime = True
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 def monotonic_time():
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 global clock_gettime
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 global no_clock_gettime
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 if no_clock_gettime:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 return time.time()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 class timespec(ctypes.Structure):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 _fields_ = [
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 ('tv_sec', ctypes.c_long),
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 ('tv_nsec', ctypes.c_long)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 ]
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 if not clock_gettime:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 try:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 librt = ctypes.CDLL('librt.so.0', use_errno=True)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 clock_gettime = librt.clock_gettime
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 except:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 W("No clock_gettime(), using fake fallback.")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 no_clock_gettime = True
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 return time.time()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 t = timespec()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 CLOCK_MONOTONIC = 1 # see <linux/time.h>
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 errno_ = ctypes.get_errno()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 raise OSError(errno_, os.strerror(errno_))
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 return t.tv_sec + t.tv_nsec * 1e-9
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 # decorator, tries a number of times, returns None on failure, sleeps between
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 # Must be used as "@retry()" if arguments are defaulted
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 def retry(retries=DEFAULT_TRIES, try_time = 1):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 def inner(func):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 def new_f(*args, **kwargs):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 for i in range(retries):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 d = func(*args, **kwargs)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 if d is not None:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 return d
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 time.sleep(try_time)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 return None
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
66 new_f.__name__ = func.__name__
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 return new_f
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 return inner
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 def readline(sock):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 timeout = READLINE_SELECT_TIMEOUT
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 buf = ''
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 while True:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 (rlist, wlist, xlist) = select.select([sock], [], [], timeout)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 if sock not in rlist:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 # hit timeout
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 return None
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 c = sock.recv(1)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 if c == '':
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 # lightblue timeout
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 return None
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 if c == '\r':
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 continue
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 buf += c
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 if c == '\n':
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 return buf
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 def crc16(buff, crc = 0, poly = 0x8408):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 l = len(buff)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 i = 0
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 while i < l:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 ch = ord(buff[i])
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 uc = 0
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 while uc < 8:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 if (crc & 1) ^ (ch & 1):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 crc = (crc >> 1) ^ poly
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 else:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 crc >>= 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 ch >>= 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 uc += 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 i += 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 return crc
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 def cheap_daemon():
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 L("Daemonising.")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 sys.stdout.flush()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 sys.stderr.flush()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 out = file('/dev/null', 'a+')
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 os.dup2(out.fileno(), sys.stdout.fileno())
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 os.dup2(out.fileno(), sys.stderr.fileno())
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 try:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 pid = os.fork()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 if pid > 0:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 sys.exit(0)
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
119 except OSError as e:
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 E("Bad fork()")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 sys.exit(1)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 os.setsid()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 try:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 pid = os.fork()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 if pid > 0:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 sys.exit(0)
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
129 except OSError as e:
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 E("Bad fork()")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 sys.exit(1)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
133 def uptime():
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
134 try:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
135 return float(open('/proc/uptime', 'r').read().split(' ', 1)[0])
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
136 except Exception as e:
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
137 return -1
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
139
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
140 def json_load_round_float(s, **args):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
141 return json.loads(s,parse_float = lambda f: round(float(f), 2), **args)
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
142
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
143 class NotTooOften(object):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
144 """ prevents things happening more than once per limit.
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
145 Isn't monotonic, good enough for logging. eg
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
146 self.logfailure = NotTooOften(180) # 3 minutes
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
147 ...
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
148 if self.logfailure():
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
149 L("blah")
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
150 """
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
151 def __init__(self, limit):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
152 """ limit is a delay in seconds or TimeDelta """
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
153 if type(limit) is datetime.timedelta:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
154 self.limit = limit
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
155 else:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
156 self.limit = datetime.timedelta(seconds=limit)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
157
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
158 # must be positive
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
159 assert self.limit > datetime.timedelta(0)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
160 self.last = datetime.datetime(10, 1, 1)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
161
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
162 def __call__(self):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
163 if datetime.datetime.now() - self.last > self.limit:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
164 self.last = datetime.datetime.now()
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
165 return True
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
166
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
167 def log(self, msg):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
168 """ calls L(msg) if it isn't too often, otherwise D(msg)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
169 """
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
170 if self():
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
171 L(msg + " (log interval %s)" % str(self.limit))
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
172 else:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
173 D(msg)