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