forked from dwks/kaldi-grammar-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode.py
313 lines (263 loc) · 10.8 KB
/
mode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from datetime import datetime, timedelta
from functools import wraps
import inspect, traceback, collections
def _safely(*funcs):
ret = None
for func in funcs:
try:
ret = func()
except Exception as e:
traceback.print_exc()
return ret
def coroutine(func):
def start(*args,**kwargs):
cr = func(*args,**kwargs)
cr.next()
return cr
return start
################################################################################################################################################################
class State(object):
instances = []
def __init__(self, timeout=None, lockout=None, lockout_oneway=True, hi_trig=None, lo_trig=None):
"""
timeout: time in seconds after last activation, that state is deactivated
lockout: time in seconds after last activation, during which state cannot be activated (or deactivated if lockout_oneway=False)
lockout_oneway: if True, only lockout from activating again after activation; otherwise lockout from activating or deactivating (after activation)
"""
self.timeout = timeout
self.lockout = lockout
self.lockout_oneway = lockout_oneway
self.hi_trig = hi_trig # only called when state is set/checked/polled!
self.lo_trig = lo_trig # only called when state is set/checked/polled!
self._state = False
self.timeout_time = None
self.lockout_time = None
State.instances.append(self)
@property
def state(self):
if self.timeout_time and datetime.today() >= self.timeout_time:
self.state = False
self.timeout_time = None
return self._state
@state.setter
def state(self, value):
# State.state.fset(s, value)
# State.state.__set__(s, value)
if self.lockout_time and (not self.lockout_oneway or value):
if datetime.today() < self.lockout_time:
return
else:
self.lockout_time = None
if bool(self._state) != bool(value):
if value:
if self.hi_trig: _safely(lambda: self.hi_trig())
else:
if self.lo_trig: _safely(lambda: self.lo_trig())
self._state = value
if value:
if self.timeout:
self.timeout_time = datetime.today() + timedelta(seconds=self.timeout)
if self.lockout:
self.lockout_time = datetime.today() + timedelta(seconds=self.lockout)
else:
self.timeout_time = None
def __nonzero__(self):
return bool(self.state)
def set(self, value=True):
initial_value = self.state
self.state = value
return (initial_value != self.state)
def activate(self, force=False):
return self.set(True)
def deactivate(self, force=False):
return self.set(False)
################################################################################################################################################################
class MultiMode:
instances = []
current = None
transition_func = None
def __init__(self, levels={}, color='#555', pump=None, release=None, timeout=None,
process=None, postprocess=None, rule=None, reset=None, thread_coro_init=None, sticky=None, exclusive=None, singleton=False):
self.levels = levels
self.color = color
self.pump_func = pump
self.release_func = release
self.timeout = timeout
self.process_func = process
self.postprocess_func = postprocess
self.rule = rule
self.reset = reset
self.thread_coro_init = thread_coro_init
self.sticky = sticky
self.exclusive = exclusive # FIXME: not implemented
self.level = 0
self.active_time = None
self.timeout_time = None
self.thread_coro = None
frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[2 if singleton else 1]
self._str = "<mode.MultiMode instance from %s:%d>" % (filename, line_number)
MultiMode.instances.append(self)
def __str__(self):
return self._str
def __nonzero__(self):
return bool(self.level)
def __call__(self):
return self.pump()
def _active_level(self):
return max([None] + filter(lambda level: level <= self.level, self.levels.keys()))
def _active_fire_func(self):
if self._active_level(): return self.levels[self._active_level()]
return lambda: None
def pump(self):
self.heartbeat()
if self.exclusive:
self.release_all()
MultiMode.current = self
self.level += 1
self.active_time = datetime.today()
if self.timeout and (1 or self.level == 1):
self.timeout_time = datetime.today() + timedelta(seconds=self.timeout)
if self.thread_coro_init and not self.thread_coro:
self.thread_coro = _safely(lambda: self.thread_coro_init())
if self.process_func:
_safely(lambda: self.process_func(self.level))
if self.pump_func:
_safely(lambda: self.pump_func())
if self.rule:
_safely(lambda: self.rule.enable())
if self.levels and self._active_level():
_safely(lambda: self._active_fire_func()())
if self.reset:
_safely(lambda: self.release())
if self.postprocess_func:
_safely(lambda: self.postprocess_func(self.level))
if self.transition_func:
_safely(lambda: self.transition_func.__func__())
if self.timeout and (1 or self.level == 1):
self.timeout_time = datetime.today() + timedelta(seconds=self.timeout)
return self
def refresh(self):
if self.timeout and self.level >= 1:
self.timeout_time = datetime.today() + timedelta(seconds=self.timeout)
def release(self):
if self.level:
self.level = 0
self.timeout_time = None
if self.release_func:
_safely(lambda: self.release_func())
if self.thread_coro:
_safely(lambda: self.thread_coro.close())
self.thread_coro = None
if self.rule:
_safely(lambda: self.rule.disable())
active_instances = sorted(filter(None, self.instances), key=lambda m: m.active_time)
MultiMode.current = active_instances[-1] if active_instances else None
if self.transition_func:
_safely(lambda: self.transition_func.__func__())
@classmethod
def release_all(cls, sticky=None):
old_mode = cls.current
for mode in cls.instances:
if sticky or not mode.sticky:
_safely(lambda: mode.release())
return old_mode
@classmethod
def heartbeat(cls):
now = datetime.today()
for mode in cls.instances:
if mode.timeout_time and now >= mode.timeout_time:
_safely(lambda: mode.release())
_safely(lambda: Deferred.heartbeat())
for mode in cls.instances:
if mode.thread_coro and mode.level:
try:
mode.thread_coro.next()
except StopIteration as e:
_safely(lambda: mode.release())
except Exception as e:
traceback.print_exc()
_safely(lambda: mode.release())
activate = pump
deactivate = release
deactivate_all = release_all
singletons = dict()
@classmethod
def singleton_pump(cls, *args, **kwargs):
frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[1]
key = (filename, line_number)
if key not in cls.singletons:
kwargs = dict(kwargs, singleton=True)
cls.singletons[key] = cls(*args, **kwargs)
return cls.singletons[key].pump()
################################################################################################################################################################
class Deferred(object):
instances = []
def __init__(self, delay, func):
self.func = func
self.delay = delay
self.target_time = datetime.today() + timedelta(seconds=self.delay)
Deferred.instances.append(self)
def execute(self):
_safely(lambda: self.func())
Deferred.instances.remove(self)
def cancel(self):
Deferred.instances.remove(self)
@classmethod
def heartbeat(cls):
now = datetime.today()
for deferred in cls.instances:
if deferred.target_time and now >= deferred.target_time:
_safely(lambda: deferred.execute())
################################################################################################################################################################
class throttle(object):
"""
Decorator that prevents a function from being called more than once every
time period.
To create a function that cannot be called more than once a minute:
@throttle(minutes=1)
def my_fun():
pass
"""
def __init__(self, seconds=0, minutes=0, hours=0):
self.throttle_period = timedelta(
seconds=seconds, minutes=minutes, hours=hours
)
self.time_of_last_call = datetime.min
def __call__(self, fn):
@wraps(fn)
def wrapper(*args, **kwargs):
now = datetime.now()
time_since_last_call = now - self.time_of_last_call
if time_since_last_call > self.throttle_period:
self.time_of_last_call = now
return fn(*args, **kwargs)
return wrapper
class ramp(object):
"""
Decorator that executes a function if it's called at least count times every time period.
"""
def __init__(self, count=2, seconds=0, minutes=0, hours=0):
self.times = collections.deque(maxlen=count)
self.ramp_period = timedelta(seconds=seconds, minutes=minutes, hours=hours)
def __call__(self, fn):
@wraps(fn)
def wrapper(*args, **kwargs):
now = datetime.now()
self.times.append(now)
while self.times and self.times[0] < (now - self.ramp_period):
self.times.popleft()
if len(self.times) == self.times.maxlen:
return fn(*args, **kwargs)
return wrapper
"""
@mode.throttle(minutes=1)
def ringer():
print "ringer(): won't be executed more than 1 every 1 minute"
@mode.ramp(3, minutes=1)
def ringer():
print "ringer(): must be called at least 3 times within 1 minute"
@mode.ramp(3, minutes=5)
@mode.throttle(minutes=1)
def ringer():
print "ringer(): must be called at least 3 times within 1 minute, but won't be executed more than 1 every 1 minute"
"""