/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-monitor

  • Committer: Teddy Hogeborn
  • Date: 2012-05-07 20:46:51 UTC
  • Revision ID: teddy@recompile.se-20120507204651-qx5o5ztwmau54mh4
* mandos-monitor: Speedup: Use properties from D-Bus
                  GetAllClientsWithProperties() call.  Also use
                  super() properly - only use keyword arguments and
                  strip them off as "kwargs" is passed on.
  (MandosClientPropertyCache.__init__): Take new "properties"
                                        argument; use it to skip
                                        calling D-Bus GetAll() method.
                                        Call super().  Bind self._
  (MandosClientPropertyCache._property_changed): New helper function.
  (MandosClientPropertyCache.property_changed): Changed to take
                                                keyword arguments.
  (MandosClientPropertyCache.delete): Bug fix: Don't call
                                      super().__init__().
  (MandosClientWidget.__init__): Don't take *args.  Strip used keyword
                                 arguments before calling super().
  (MandosClientWidget.property_changed): Removed dead code.
  (MandosClientWidget.delete): Don't take *args.
  (MandosClientWidget.property_changed): Don't take "value" argument,
                                         since it is not used.
  (ConstrainedListBox.keypress): Take *args, **kwargs and pass them on
                                 to super().

Show diffs side-by-side

added added

removed removed

Lines of Context:
134
134
        self.logger = logger
135
135
        
136
136
        self._update_timer_callback_tag = None
 
137
        self._update_timer_callback_lock = 0
137
138
        
138
139
        # The widget shown normally
139
140
        self._text_widget = urwid.Text("")
143
144
        self.update()
144
145
        self.opened = False
145
146
        
 
147
        last_checked_ok = isoformat_to_datetime(self.properties
 
148
                                                ["LastCheckedOK"])
 
149
        
 
150
        if self.properties ["LastCheckerStatus"] != 0:
 
151
            self.using_timer(True)
 
152
        
 
153
        if self.need_approval:
 
154
            self.using_timer(True)
 
155
        
146
156
        self.match_objects = (
147
157
            self.proxy.connect_to_signal("CheckerCompleted",
148
158
                                         self.checker_completed,
167
177
        #self.logger('Created client {0}'
168
178
        #            .format(self.properties["Name"]))
169
179
    
 
180
    def property_changed(self, property=None, value=None):
 
181
        super(self, MandosClientWidget).property_changed(property,
 
182
                                                         value)
 
183
        if property == "ApprovalPending":
 
184
            using_timer(bool(value))
 
185
        if property == "LastCheckerStatus":
 
186
            using_timer(value != 0)
 
187
            #self.logger('Checker for client {0} (command "{1}") was '
 
188
            #            ' successful'.format(self.properties["Name"],
 
189
            #                                 command))
 
190
    
170
191
    def using_timer(self, flag):
171
192
        """Call this method with True or False when timer should be
172
193
        activated or deactivated.
173
194
        """
174
 
        if flag and self._update_timer_callback_tag is None:
 
195
        old = self._update_timer_callback_lock
 
196
        if flag:
 
197
            self._update_timer_callback_lock += 1
 
198
        else:
 
199
            self._update_timer_callback_lock -= 1
 
200
        if old == 0 and self._update_timer_callback_lock:
175
201
            # Will update the shown timer value every second
176
202
            self._update_timer_callback_tag = (gobject.timeout_add
177
203
                                               (1000,
178
204
                                                self.update_timer))
179
 
        elif not (flag or self._update_timer_callback_tag is None):
 
205
        elif old and self._update_timer_callback_lock == 0:
180
206
            gobject.source_remove(self._update_timer_callback_tag)
181
207
            self._update_timer_callback_tag = None
182
208
    
224
250
            message = 'Client {0} will get its secret in {1} seconds'
225
251
        self.logger(message.format(self.properties["Name"],
226
252
                                   timeout/1000))
 
253
        self.using_timer(True)
227
254
    
228
255
    def rejected(self, reason):
229
256
        self.logger('Client {0} was rejected; reason: {1}'
255
282
                          "bold-underline-blink":
256
283
                              "bold-underline-blink-standout",
257
284
                          }
258
 
        
 
285
 
259
286
        # Rebuild focus and non-focus widgets using current properties
260
 
        
 
287
 
261
288
        # Base part of a client. Name!
262
289
        base = '{name}: '.format(name=self.properties["Name"])
263
290
        if not self.properties["Enabled"]:
264
291
            message = "DISABLED"
265
 
            self.using_timer(False)
266
292
        elif self.properties["ApprovalPending"]:
267
293
            timeout = datetime.timedelta(milliseconds
268
294
                                         = self.properties
270
296
            last_approval_request = isoformat_to_datetime(
271
297
                self.properties["LastApprovalRequest"])
272
298
            if last_approval_request is not None:
273
 
                timer = max(timeout - (datetime.datetime.utcnow()
274
 
                                       - last_approval_request),
275
 
                            datetime.timedelta())
 
299
                timer = timeout - (datetime.datetime.utcnow()
 
300
                                   - last_approval_request)
276
301
            else:
277
302
                timer = datetime.timedelta()
278
303
            if self.properties["ApprovedByDefault"]:
280
305
            else:
281
306
                message = "Denial in {0}. (a)pprove?"
282
307
            message = message.format(unicode(timer).rsplit(".", 1)[0])
283
 
            self.using_timer(True)
284
308
        elif self.properties["LastCheckerStatus"] != 0:
285
309
            # When checker has failed, show timer until client expires
286
310
            expires = self.properties["Expires"]
289
313
            else:
290
314
                expires = (datetime.datetime.strptime
291
315
                           (expires, '%Y-%m-%dT%H:%M:%S.%f'))
292
 
                timer = max(expires - datetime.datetime.utcnow(),
293
 
                            datetime.timedelta())
 
316
                timer = expires - datetime.datetime.utcnow()
294
317
            message = ('A checker has failed! Time until client'
295
318
                       ' gets disabled: {0}'
296
319
                       .format(unicode(timer).rsplit(".", 1)[0]))
297
 
            self.using_timer(True)
298
320
        else:
299
321
            message = "enabled"
300
 
            self.using_timer(False)
301
322
        self._text = "{0}{1}".format(base, message)
302
 
        
 
323
            
303
324
        if not urwid.supports_unicode():
304
325
            self._text = self._text.encode("ascii", "replace")
305
326
        textlist = [("normal", self._text)]