37
37
urwid.curses_display.curses.A_UNDERLINE |= (
38
38
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
40
56
class MandosClientPropertyCache(object):
41
57
"""This wraps a Mandos Client D-Bus proxy object, caches the
42
58
properties and calls a hook function when any of them are
81
97
self.logger = logger
99
self._update_timer_callback_tag = None
100
self.last_checker_failed = False
83
102
# The widget shown normally
84
103
self._text_widget = urwid.Text(u"")
85
104
# The widget shown when we have focus
106
125
client_interface,
107
126
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
109
142
def checker_completed(self, exitstatus, condition, command):
110
143
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
111
148
self.logger(u'Checker for client %s (command "%s")'
112
149
u' was successful'
113
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
115
159
if os.WIFEXITED(condition):
116
160
self.logger(u'Checker for client %s (command "%s")'
117
161
u' failed with exit code %s'
118
162
% (self.properties[u"name"], command,
119
163
os.WEXITSTATUS(condition)))
121
if os.WIFSIGNALED(condition):
164
elif os.WIFSIGNALED(condition):
122
165
self.logger(u'Checker for client %s (command "%s")'
123
166
u' was killed by signal %s'
124
167
% (self.properties[u"name"], command,
125
168
os.WTERMSIG(condition)))
127
if os.WCOREDUMP(condition):
169
elif os.WCOREDUMP(condition):
128
170
self.logger(u'Checker for client %s (command "%s")'
130
172
% (self.properties[u"name"], command))
131
self.logger(u'Checker for client %s completed mysteriously')
174
self.logger(u'Checker for client %s completed mysteriously')
133
177
def checker_started(self, command):
134
178
self.logger(u'Client %s started checker "%s"'
172
216
# Rebuild focus and non-focus widgets using current properties
173
self._text = (u'%(name)s: %(enabled)s'
217
self._text = (u'%(name)s: %(enabled)s%(timer)s'
174
218
% { u"name": self.properties[u"name"],
177
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
179
240
if not urwid.supports_unicode():
180
241
self._text = self._text.encode("ascii", "replace")
181
242
textlist = [(u"normal", self._text)]
192
253
if self.update_hook is not None:
193
254
self.update_hook()
256
def update_timer(self):
259
return True # Keep calling this
195
261
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
196
265
if self.delete_hook is not None:
197
266
self.delete_hook(self)