/mandos/release

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

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-03 16:25:45 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190303162545-vjdm7icnhpprzuwb
mandos-ctl: Refactor; move TableOfClients into PrintTableCmd

* mandos-ctl (TableOfClients): Moved into PrintTableCmd; user changed.
  (PrintTableCmd.TableOfClients): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
268
268
    return value
269
269
 
270
270
 
271
 
class TableOfClients(object):
272
 
    tableheaders = {
273
 
        "Name": "Name",
274
 
        "Enabled": "Enabled",
275
 
        "Timeout": "Timeout",
276
 
        "LastCheckedOK": "Last Successful Check",
277
 
        "LastApprovalRequest": "Last Approval Request",
278
 
        "Created": "Created",
279
 
        "Interval": "Interval",
280
 
        "Host": "Host",
281
 
        "Fingerprint": "Fingerprint",
282
 
        "KeyID": "Key ID",
283
 
        "CheckerRunning": "Check Is Running",
284
 
        "LastEnabled": "Last Enabled",
285
 
        "ApprovalPending": "Approval Is Pending",
286
 
        "ApprovedByDefault": "Approved By Default",
287
 
        "ApprovalDelay": "Approval Delay",
288
 
        "ApprovalDuration": "Approval Duration",
289
 
        "Checker": "Checker",
290
 
        "ExtendedTimeout": "Extended Timeout",
291
 
        "Expires": "Expires",
292
 
        "LastCheckerStatus": "Last Checker Status",
293
 
    }
294
 
 
295
 
    def __init__(self, clients, keywords, tableheaders=None):
296
 
        self.clients = clients
297
 
        self.keywords = keywords
298
 
        if tableheaders is not None:
299
 
            self.tableheaders = tableheaders
300
 
 
301
 
    def __str__(self):
302
 
        return "\n".join(self.rows())
303
 
 
304
 
    if sys.version_info.major == 2:
305
 
        __unicode__ = __str__
306
 
        def __str__(self):
307
 
            return str(self).encode(locale.getpreferredencoding())
308
 
 
309
 
    def rows(self):
310
 
        format_string = self.row_formatting_string()
311
 
        rows = [self.header_line(format_string)]
312
 
        rows.extend(self.client_line(client, format_string)
313
 
                    for client in self.clients)
314
 
        return rows
315
 
 
316
 
    def row_formatting_string(self):
317
 
        "Format string used to format table rows"
318
 
        return " ".join("{{{key}:{width}}}".format(
319
 
            width=max(len(self.tableheaders[key]),
320
 
                      *(len(self.string_from_client(client, key))
321
 
                        for client in self.clients)),
322
 
            key=key)
323
 
                        for key in self.keywords)
324
 
 
325
 
    def string_from_client(self, client, key):
326
 
        return self.valuetostring(client[key], key)
327
 
 
328
 
    @staticmethod
329
 
    def valuetostring(value, keyword):
330
 
        if isinstance(value, dbus.Boolean):
331
 
            return "Yes" if value else "No"
332
 
        if keyword in ("Timeout", "Interval", "ApprovalDelay",
333
 
                       "ApprovalDuration", "ExtendedTimeout"):
334
 
            return milliseconds_to_string(value)
335
 
        return str(value)
336
 
 
337
 
    def header_line(self, format_string):
338
 
        return format_string.format(**self.tableheaders)
339
 
 
340
 
    def client_line(self, client, format_string):
341
 
        return format_string.format(
342
 
            **{key: self.string_from_client(client, key)
343
 
               for key in self.keywords})
344
 
 
345
 
 
346
271
## Classes for commands.
347
272
 
348
273
# Abstract classes first
396
321
class PrintTableCmd(PrintCmd):
397
322
    def __init__(self, verbose=False):
398
323
        self.verbose = verbose
 
324
 
399
325
    def output(self, clients):
400
326
        if self.verbose:
401
327
            keywords = self.all_keywords
402
328
        else:
403
329
            keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
404
 
        return str(TableOfClients(clients.values(), keywords))
 
330
        return str(self.TableOfClients(clients.values(), keywords))
 
331
 
 
332
    class TableOfClients(object):
 
333
        tableheaders = {
 
334
            "Name": "Name",
 
335
            "Enabled": "Enabled",
 
336
            "Timeout": "Timeout",
 
337
            "LastCheckedOK": "Last Successful Check",
 
338
            "LastApprovalRequest": "Last Approval Request",
 
339
            "Created": "Created",
 
340
            "Interval": "Interval",
 
341
            "Host": "Host",
 
342
            "Fingerprint": "Fingerprint",
 
343
            "KeyID": "Key ID",
 
344
            "CheckerRunning": "Check Is Running",
 
345
            "LastEnabled": "Last Enabled",
 
346
            "ApprovalPending": "Approval Is Pending",
 
347
            "ApprovedByDefault": "Approved By Default",
 
348
            "ApprovalDelay": "Approval Delay",
 
349
            "ApprovalDuration": "Approval Duration",
 
350
            "Checker": "Checker",
 
351
            "ExtendedTimeout": "Extended Timeout",
 
352
            "Expires": "Expires",
 
353
            "LastCheckerStatus": "Last Checker Status",
 
354
        }
 
355
 
 
356
        def __init__(self, clients, keywords, tableheaders=None):
 
357
            self.clients = clients
 
358
            self.keywords = keywords
 
359
            if tableheaders is not None:
 
360
                self.tableheaders = tableheaders
 
361
 
 
362
        def __str__(self):
 
363
            return "\n".join(self.rows())
 
364
 
 
365
        if sys.version_info.major == 2:
 
366
            __unicode__ = __str__
 
367
            def __str__(self):
 
368
                return str(self).encode(locale.getpreferredencoding())
 
369
 
 
370
        def rows(self):
 
371
            format_string = self.row_formatting_string()
 
372
            rows = [self.header_line(format_string)]
 
373
            rows.extend(self.client_line(client, format_string)
 
374
                        for client in self.clients)
 
375
            return rows
 
376
 
 
377
        def row_formatting_string(self):
 
378
            "Format string used to format table rows"
 
379
            return " ".join("{{{key}:{width}}}".format(
 
380
                width=max(len(self.tableheaders[key]),
 
381
                          *(len(self.string_from_client(client, key))
 
382
                            for client in self.clients)),
 
383
                key=key)
 
384
                            for key in self.keywords)
 
385
 
 
386
        def string_from_client(self, client, key):
 
387
            return self.valuetostring(client[key], key)
 
388
 
 
389
        @staticmethod
 
390
        def valuetostring(value, keyword):
 
391
            if isinstance(value, dbus.Boolean):
 
392
                return "Yes" if value else "No"
 
393
            if keyword in ("Timeout", "Interval", "ApprovalDelay",
 
394
                           "ApprovalDuration", "ExtendedTimeout"):
 
395
                return milliseconds_to_string(value)
 
396
            return str(value)
 
397
 
 
398
        def header_line(self, format_string):
 
399
            return format_string.format(**self.tableheaders)
 
400
 
 
401
        def client_line(self, client, format_string):
 
402
            return format_string.format(
 
403
                **{key: self.string_from_client(client, key)
 
404
                   for key in self.keywords})
 
405
 
 
406
 
405
407
 
406
408
class DumpJSONCmd(PrintCmd):
407
409
    def output(self, clients):