40
37
urwid.curses_display.curses.A_UNDERLINE |= (
41
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
43
56
class MandosClientPropertyCache(object):
44
57
"""This wraps a Mandos Client D-Bus proxy object, caches the
45
58
properties and calls a hook function when any of them are
105
121
client_interface,
106
122
byte_arrays=True)
107
self.proxy.connect_to_signal(u"NeedApproval",
111
123
self.proxy.connect_to_signal(u"Rejected",
113
125
client_interface,
114
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
116
142
def checker_completed(self, exitstatus, condition, command):
117
143
if exitstatus == 0:
118
#self.logger(u'Checker for client %s (command "%s")'
120
# % (self.properties[u"name"], 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
122
159
if os.WIFEXITED(condition):
123
160
self.logger(u'Checker for client %s (command "%s")'
124
161
u' failed with exit code %s'
125
162
% (self.properties[u"name"], command,
126
163
os.WEXITSTATUS(condition)))
128
if os.WIFSIGNALED(condition):
164
elif os.WIFSIGNALED(condition):
129
165
self.logger(u'Checker for client %s (command "%s")'
130
166
u' was killed by signal %s'
131
167
% (self.properties[u"name"], command,
132
168
os.WTERMSIG(condition)))
134
if os.WCOREDUMP(condition):
169
elif os.WCOREDUMP(condition):
135
170
self.logger(u'Checker for client %s (command "%s")'
137
172
% (self.properties[u"name"], command))
138
self.logger(u'Checker for client %s completed mysteriously')
174
self.logger(u'Checker for client %s completed mysteriously')
140
177
def checker_started(self, command):
141
#self.logger(u'Client %s started checker "%s"'
142
# % (self.properties[u"name"], unicode(command)))
178
self.logger(u'Client %s started checker "%s"'
179
% (self.properties[u"name"], unicode(command)))
145
181
def got_secret(self):
146
182
self.logger(u'Client %s received its secret'
147
183
% self.properties[u"name"])
149
def need_approval(self, timeout, default):
151
message = u'Client %s needs approval within %s seconds'
153
message = u'Client %s will get its secret in %s seconds'
155
% (self.properties[u"name"], timeout/1000))
157
def rejected(self, reason):
158
self.logger(u'Client %s was rejected; reason: %s'
159
% (self.properties[u"name"], reason))
186
self.logger(u'Client %s was rejected'
187
% self.properties[u"name"])
161
189
def selectable(self):
162
190
"""Make this a "selectable" widget.
188
216
# Rebuild focus and non-focus widgets using current properties
189
self._text = (u'%(name)s: %(enabled)s'
217
self._text = (u'%(name)s: %(enabled)s%(timer)s'
190
218
% { u"name": self.properties[u"name"],
193
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
195
240
if not urwid.supports_unicode():
196
241
self._text = self._text.encode("ascii", "replace")
197
242
textlist = [(u"normal", self._text)]