-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
70 lines (47 loc) · 1.64 KB
/
common.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
import time
def time_in_millis():
return int(round(time.time() * 1000))
class Listenable(object):
def __init__(self):
self.listeners = []
def add_listener(self, o):
if not o in self.listeners:
self.listeners.append(o)
def clear_listeners(self):
self.listeners = []
def remove_listener(self, o):
if o in self.listeners:
self.listeners.remove(o)
def notify(self, func, *args):
for o in self.listeners:
getattr(o, func)(*args)
class Message(Listenable):
def __init__(self):
super(Message, self).__init__()
self._clear()
def has_message(self):
return self.text != None
def _set(self, text, level, timeout, timestamp):
assert timeout > 0
assert level in ["info", "warning", "error"]
self.text = text
self.level = level
self.timeout = timeout
self.timestamp = timestamp
self.notify("message_changed", self)
def _clear(self):
self.text, self.level, self.timeout, self.timestamp = None, None, 0, 0
self.notify("message_changed", self)
def info(self, text, timeout):
self._set(text, "info", timeout, time_in_millis())
def warning(self, text, timeout):
self._set(text, "warning", timeout, time_in_millis())
def error(self, text, timeout):
self._set(text, "error", timeout, time_in_millis())
def update(self, time):
if self.has_message():
if (time - self.timestamp) >= self.timeout * 1000:
self._clear()
class MessageListener(object):
def message_changed(self, msg):
pass