37
33
urwid.curses_display.curses.A_UNDERLINE |= (
38
34
urwid.curses_display.curses.A_BLINK)
40
def isoformat_to_datetime(iso):
41
"Parse an ISO 8601 date string to a datetime.datetime()"
44
d, t = iso.split(u"T", 1)
45
year, month, day = d.split(u"-", 2)
46
hour, minute, second = t.split(u":", 2)
47
second, fraction = divmod(float(second), 1)
48
return datetime.datetime(int(year),
53
int(second), # Whole seconds
54
int(fraction*1000000)) # Microseconds
56
36
class MandosClientPropertyCache(object):
57
37
"""This wraps a Mandos Client D-Bus proxy object, caches the
58
38
properties and calls a hook function when any of them are
61
def __init__(self, proxy_object=None, *args, **kwargs):
41
def __init__(self, proxy_object=None, properties=None, *args,
62
43
self.proxy = proxy_object # Mandos Client proxy object
64
self.properties = dict()
45
if properties is None:
46
self.properties = dict()
48
self.properties = properties
65
49
self.proxy.connect_to_signal(u"PropertyChanged",
66
50
self.property_changed,
70
self.properties.update(
71
self.proxy.GetAll(client_interface,
72
dbus_interface = dbus.PROPERTIES_IFACE))
54
if properties is None:
55
self.properties.update(self.proxy.GetAll(client_interface,
57
dbus.PROPERTIES_IFACE))
73
58
super(MandosClientPropertyCache, self).__init__(
74
proxy_object=proxy_object, *args, **kwargs)
59
proxy_object=proxy_object,
60
properties=properties, *args, **kwargs)
76
62
def property_changed(self, property=None, value=None):
77
63
"""This is called whenever we get a PropertyChanged signal
125
108
client_interface,
126
109
byte_arrays=True)
127
last_checked_ok = isoformat_to_datetime(self.properties
129
if last_checked_ok is None:
130
self.last_checker_failed = True
132
self.last_checker_failed = ((datetime.datetime.utcnow()
136
self.properties["interval"]))
137
if self.last_checker_failed:
138
self._update_timer_callback_tag = (gobject.timeout_add
142
111
def checker_completed(self, exitstatus, condition, command):
143
112
if exitstatus == 0:
144
if self.last_checker_failed:
145
self.last_checker_failed = False
146
gobject.source_remove(self._update_timer_callback_tag)
147
self._update_timer_callback_tag = None
148
113
self.logger(u'Checker for client %s (command "%s")'
149
114
u' was successful'
150
115
% (self.properties[u"name"], command))
154
if not self.last_checker_failed:
155
self.last_checker_failed = True
156
self._update_timer_callback_tag = (gobject.timeout_add
159
117
if os.WIFEXITED(condition):
160
118
self.logger(u'Checker for client %s (command "%s")'
161
119
u' failed with exit code %s'
162
120
% (self.properties[u"name"], command,
163
121
os.WEXITSTATUS(condition)))
164
elif os.WIFSIGNALED(condition):
123
if os.WIFSIGNALED(condition):
165
124
self.logger(u'Checker for client %s (command "%s")'
166
125
u' was killed by signal %s'
167
126
% (self.properties[u"name"], command,
168
127
os.WTERMSIG(condition)))
169
elif os.WCOREDUMP(condition):
129
if os.WCOREDUMP(condition):
170
130
self.logger(u'Checker for client %s (command "%s")'
172
132
% (self.properties[u"name"], command))
174
self.logger(u'Checker for client %s completed mysteriously')
133
self.logger(u'Checker for client %s completed mysteriously')
177
135
def checker_started(self, command):
178
136
self.logger(u'Client %s started checker "%s"'
216
174
# Rebuild focus and non-focus widgets using current properties
217
self._text = (u'%(name)s: %(enabled)s%(timer)s'
175
self._text = (u'%(name)s: %(enabled)s'
218
176
% { u"name": self.properties[u"name"],
221
179
if self.properties[u"enabled"]
223
u"timer": (unicode(datetime.timedelta
229
- isoformat_to_datetime
230
(max((self.properties
235
self.properties[u"last_enabled"]))))
236
if (self.last_checker_failed
240
181
if not urwid.supports_unicode():
241
182
self._text = self._text.encode("ascii", "replace")
242
183
textlist = [(u"normal", self._text)]
253
194
if self.update_hook is not None:
254
195
self.update_hook()
256
def update_timer(self):
259
return True # Keep calling this
261
197
def delete(self):
262
if self._update_timer_callback_tag is not None:
263
gobject.source_remove(self._update_timer_callback_tag)
264
self._update_timer_callback_tag = None
265
198
if self.delete_hook is not None:
266
199
self.delete_hook(self)
492
425
self.remove_client(client, path)
494
def add_new_client(self, path):
427
def add_new_client(self, path, properties):
495
428
client_proxy_object = self.bus.get_object(self.busname, path)
496
429
self.add_client(MandosClientWidget(server_proxy_object
497
430
=self.mandos_serv,
499
432
=client_proxy_object,
433
properties=properties,
437
=self.remove_client),
508
440
def add_client(self, client, path=None):