37
30
urwid.curses_display.curses.A_UNDERLINE |= (
38
31
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
33
class MandosClientPropertyCache(object):
57
34
"""This wraps a Mandos Client D-Bus proxy object, caches the
58
35
properties and calls a hook function when any of them are
61
def __init__(self, proxy_object=None, *args, **kwargs):
38
def __init__(self, proxy_object=None, properties=None, *args,
62
40
self.proxy = proxy_object # Mandos Client proxy object
64
self.properties = dict()
65
self.proxy.connect_to_signal(u"PropertyChanged",
42
if properties is None:
43
self.properties = dict()
45
self.properties = properties
46
self.proxy.connect_to_signal("PropertyChanged",
66
47
self.property_changed,
70
self.properties.update(
71
self.proxy.GetAll(client_interface,
72
dbus_interface = dbus.PROPERTIES_IFACE))
51
if properties is None:
52
self.properties.update(self.proxy.GetAll(client_interface,
54
dbus.PROPERTIES_IFACE))
73
55
super(MandosClientPropertyCache, self).__init__(
74
proxy_object=proxy_object, *args, **kwargs)
56
proxy_object=proxy_object,
57
properties=properties, *args, **kwargs)
76
59
def property_changed(self, property=None, value=None):
77
60
"""This is called whenever we get a PropertyChanged signal
88
71
def __init__(self, server_proxy_object=None, update_hook=None,
89
delete_hook=None, logger=None, *args, **kwargs):
72
delete_hook=None, *args, **kwargs):
91
74
self.update_hook = update_hook
93
76
self.delete_hook = delete_hook
94
77
# Mandos Server proxy object
95
78
self.server_proxy_object = server_proxy_object
99
self._update_timer_callback_tag = None
100
self.last_checker_failed = False
102
80
# The widget shown normally
103
self._text_widget = urwid.Text(u"")
81
self._text_widget = urwid.Text("")
104
82
# The widget shown when we have focus
105
self._focus_text_widget = urwid.Text(u"")
83
self._focus_text_widget = urwid.Text("")
106
84
super(MandosClientWidget, self).__init__(
107
85
update_hook=update_hook, delete_hook=delete_hook,
110
88
self.opened = False
111
self.proxy.connect_to_signal(u"CheckerCompleted",
112
self.checker_completed,
115
self.proxy.connect_to_signal(u"CheckerStarted",
116
self.checker_started,
119
self.proxy.connect_to_signal(u"GotSecret",
123
self.proxy.connect_to_signal(u"Rejected",
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
def checker_completed(self, exitstatus, condition, command):
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
self.logger(u'Checker for client %s (command "%s")'
150
% (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
if os.WIFEXITED(condition):
160
self.logger(u'Checker for client %s (command "%s")'
161
u' failed with exit code %s'
162
% (self.properties[u"name"], command,
163
os.WEXITSTATUS(condition)))
164
elif os.WIFSIGNALED(condition):
165
self.logger(u'Checker for client %s (command "%s")'
166
u' was killed by signal %s'
167
% (self.properties[u"name"], command,
168
os.WTERMSIG(condition)))
169
elif os.WCOREDUMP(condition):
170
self.logger(u'Checker for client %s (command "%s")'
172
% (self.properties[u"name"], command))
174
self.logger(u'Checker for client %s completed mysteriously')
177
def checker_started(self, command):
178
self.logger(u'Client %s started checker "%s"'
179
% (self.properties[u"name"], unicode(command)))
181
def got_secret(self):
182
self.logger(u'Client %s received its secret'
183
% self.properties[u"name"])
186
self.logger(u'Client %s was rejected'
187
% self.properties[u"name"])
189
90
def selectable(self):
190
91
"""Make this a "selectable" widget.
216
117
# Rebuild focus and non-focus widgets using current properties
217
self._text = (u'%(name)s: %(enabled)s%(timer)s'
218
% { u"name": self.properties[u"name"],
221
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
118
self._text = (u'name="%(name)s", enabled=%(enabled)s'
240
120
if not urwid.supports_unicode():
241
121
self._text = self._text.encode("ascii", "replace")
242
textlist = [(u"normal", self._text)]
122
textlist = [(u"normal", u"BLARGH: "), (u"bold", self._text)]
243
123
self._text_widget.set_text(textlist)
244
124
self._focus_text_widget.set_text([(with_standout[text[0]],
398
270
mandos_clients = dbus.Dictionary()
400
272
(self.mandos_serv
401
.connect_to_signal(u"ClientRemoved",
273
.connect_to_signal("ClientRemoved",
402
274
self.find_and_remove_client,
403
275
dbus_interface=server_interface,
404
276
byte_arrays=True))
405
277
(self.mandos_serv
406
.connect_to_signal(u"ClientAdded",
278
.connect_to_signal("ClientAdded",
407
279
self.add_new_client,
408
280
dbus_interface=server_interface,
409
281
byte_arrays=True))
411
.connect_to_signal(u"ClientNotFound",
412
self.client_not_found,
413
dbus_interface=server_interface,
415
282
for path, client in mandos_clients.iteritems():
416
283
client_proxy_object = self.bus.get_object(self.busname,