/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-02 01:56:35 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190302015635-xgodxd3ivltju7qn
mandos-ctl: Refactor

* mandos-ctl (global tablewords): Refactor into TableOfClients.
  (TableOfClients.tablewords): New overrideable class attribute.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
 
62
62
locale.setlocale(locale.LC_ALL, "")
63
63
 
64
 
tablewords = {
65
 
    "Name": "Name",
66
 
    "Enabled": "Enabled",
67
 
    "Timeout": "Timeout",
68
 
    "LastCheckedOK": "Last Successful Check",
69
 
    "LastApprovalRequest": "Last Approval Request",
70
 
    "Created": "Created",
71
 
    "Interval": "Interval",
72
 
    "Host": "Host",
73
 
    "Fingerprint": "Fingerprint",
74
 
    "KeyID": "Key ID",
75
 
    "CheckerRunning": "Check Is Running",
76
 
    "LastEnabled": "Last Enabled",
77
 
    "ApprovalPending": "Approval Is Pending",
78
 
    "ApprovedByDefault": "Approved By Default",
79
 
    "ApprovalDelay": "Approval Delay",
80
 
    "ApprovalDuration": "Approval Duration",
81
 
    "Checker": "Checker",
82
 
    "ExtendedTimeout": "Extended Timeout",
83
 
    "Expires": "Expires",
84
 
    "LastCheckerStatus": "Last Checker Status",
85
 
}
86
64
defaultkeywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
87
65
domain = "se.recompile"
88
66
busname = domain + ".Mandos"
295
273
    print('\n'.join(TableOfClients(clients, keywords).rows()))
296
274
 
297
275
class TableOfClients(object):
298
 
    def __init__(self, clients, keywords):
 
276
    tablewords = {
 
277
        "Name": "Name",
 
278
        "Enabled": "Enabled",
 
279
        "Timeout": "Timeout",
 
280
        "LastCheckedOK": "Last Successful Check",
 
281
        "LastApprovalRequest": "Last Approval Request",
 
282
        "Created": "Created",
 
283
        "Interval": "Interval",
 
284
        "Host": "Host",
 
285
        "Fingerprint": "Fingerprint",
 
286
        "KeyID": "Key ID",
 
287
        "CheckerRunning": "Check Is Running",
 
288
        "LastEnabled": "Last Enabled",
 
289
        "ApprovalPending": "Approval Is Pending",
 
290
        "ApprovedByDefault": "Approved By Default",
 
291
        "ApprovalDelay": "Approval Delay",
 
292
        "ApprovalDuration": "Approval Duration",
 
293
        "Checker": "Checker",
 
294
        "ExtendedTimeout": "Extended Timeout",
 
295
        "Expires": "Expires",
 
296
        "LastCheckerStatus": "Last Checker Status",
 
297
    }
 
298
 
 
299
    def __init__(self, clients, keywords, tablewords=None):
299
300
        self.clients = clients
300
301
        self.keywords = keywords
 
302
        if tablewords is not None:
 
303
            self.tablewords = tablewords
301
304
 
302
305
    @staticmethod
303
306
    def valuetostring(value, keyword):
311
314
    def rows(self):
312
315
        # Create format string to format table rows
313
316
        format_string = " ".join("{{{key}:{width}}}".format(
314
 
            width=max(len(tablewords[key]),
 
317
            width=max(len(self.tablewords[key]),
315
318
                      max(len(self.valuetostring(client[key], key))
316
319
                          for client in self.clients)),
317
320
            key=key)
318
321
                                 for key in self.keywords)
319
322
        # Start with header line
320
 
        rows = [format_string.format(**tablewords)]
 
323
        rows = [format_string.format(**self.tablewords)]
321
324
        for client in self.clients:
322
325
            rows.append(format_string
323
326
                        .format(**{key: self.valuetostring(client[key], key)
582
585
 
583
586
class Test_TableOfClients(unittest.TestCase):
584
587
    def setUp(self):
585
 
        global tablewords
586
 
        self.old_tablewords = tablewords
587
 
        tablewords = {
 
588
        self.tablewords = {
588
589
            "Attr1": "X",
589
590
            "AttrTwo": "Yy",
590
591
            "AttrThree": "Zzz",
629
630
                "String": "A huge string which will not fit," * 10,
630
631
            },
631
632
        ]
632
 
    def tearDown(self):
633
 
        global tablewords
634
 
        tablewords = self.old_tablewords
635
633
    def test_short_header(self):
636
 
        rows = TableOfClients(self.clients, self.keywords).rows()
 
634
        rows = TableOfClients(self.clients, self.keywords,
 
635
                              self.tablewords).rows()
637
636
        expected_rows = [
638
637
            "X  Yy",
639
638
            "x1 y1",
641
640
        self.assertEqual(rows, expected_rows)
642
641
    def test_booleans(self):
643
642
        keywords = ["Bool", "NonDbusBoolean"]
644
 
        rows = TableOfClients(self.clients, keywords).rows()
 
643
        rows = TableOfClients(self.clients, keywords,
 
644
                              self.tablewords).rows()
645
645
        expected_rows = [
646
646
            "A D-BUS Boolean A Non-D-BUS Boolean",
647
647
            "No              False              ",
651
651
    def test_milliseconds_detection(self):
652
652
        keywords = ["Integer", "Timeout", "Interval", "ApprovalDelay",
653
653
                    "ApprovalDuration", "ExtendedTimeout"]
654
 
        rows = TableOfClients(self.clients, keywords).rows()
 
654
        rows = TableOfClients(self.clients, keywords,
 
655
                              self.tablewords).rows()
655
656
        expected_rows = ("""
656
657
An Integer Timedelta 1 Timedelta 2 Timedelta 3 Timedelta 4 Timedelta 5
657
658
0          00:00:00    00:00:01    00:00:02    00:00:03    00:00:04   
661
662
        self.assertEqual(rows, expected_rows)
662
663
    def test_empty_and_long_string_values(self):
663
664
        keywords = ["String"]
664
 
        rows = TableOfClients(self.clients, keywords).rows()
 
665
        rows = TableOfClients(self.clients, keywords,
 
666
                              self.tablewords).rows()
665
667
        expected_rows = ("""
666
668
A String                                                                                                                                                                                                                                                                                                                                  
667
669