/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:40:17 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190302014017-w6c8qytzlroqej24
mandos-ctl: Refactor

* mandos-ctl (table_rows_of_clients): Refactor into TableOfClients
                                      class.  All callers changed.
  (class TableOfClients): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
292
292
 
293
293
 
294
294
def print_clients(clients, keywords):
295
 
    print('\n'.join(table_rows_of_clients(clients, keywords)))
296
 
 
297
 
def table_rows_of_clients(clients, keywords):
 
295
    print('\n'.join(TableOfClients(clients, keywords).rows()))
 
296
 
 
297
class TableOfClients(object):
 
298
    def __init__(self, clients, keywords):
 
299
        self.clients = clients
 
300
        self.keywords = keywords
 
301
 
 
302
    @staticmethod
298
303
    def valuetostring(value, keyword):
299
304
        if isinstance(value, dbus.Boolean):
300
305
            return "Yes" if value else "No"
303
308
            return milliseconds_to_string(value)
304
309
        return str(value)
305
310
 
306
 
    # Create format string to print table rows
307
 
    format_string = " ".join("{{{key}:{width}}}".format(
308
 
        width=max(len(tablewords[key]),
309
 
                  max(len(valuetostring(client[key], key))
310
 
                      for client in clients)),
311
 
        key=key)
312
 
                             for key in keywords)
313
 
    # Start with header line
314
 
    rows = [format_string.format(**tablewords)]
315
 
    for client in clients:
316
 
        rows.append(format_string
317
 
                    .format(**{key: valuetostring(client[key], key)
318
 
                               for key in keywords}))
319
 
    return rows
 
311
    def rows(self):
 
312
        # Create format string to format table rows
 
313
        format_string = " ".join("{{{key}:{width}}}".format(
 
314
            width=max(len(tablewords[key]),
 
315
                      max(len(self.valuetostring(client[key], key))
 
316
                          for client in self.clients)),
 
317
            key=key)
 
318
                                 for key in self.keywords)
 
319
        # Start with header line
 
320
        rows = [format_string.format(**tablewords)]
 
321
        for client in self.clients:
 
322
            rows.append(format_string
 
323
                        .format(**{key: self.valuetostring(client[key], key)
 
324
                                   for key in self.keywords}))
 
325
        return rows
320
326
 
321
327
 
322
328
def has_actions(options):
574
580
            value = string_to_delta("2h")
575
581
        self.assertEqual(value, datetime.timedelta(0, 7200))
576
582
 
577
 
class Test_table_rows_of_clients(unittest.TestCase):
 
583
class Test_TableOfClients(unittest.TestCase):
578
584
    def setUp(self):
579
585
        global tablewords
580
586
        self.old_tablewords = tablewords
627
633
        global tablewords
628
634
        tablewords = self.old_tablewords
629
635
    def test_short_header(self):
630
 
        rows = table_rows_of_clients(self.clients, self.keywords)
 
636
        rows = TableOfClients(self.clients, self.keywords).rows()
631
637
        expected_rows = [
632
638
            "X  Yy",
633
639
            "x1 y1",
635
641
        self.assertEqual(rows, expected_rows)
636
642
    def test_booleans(self):
637
643
        keywords = ["Bool", "NonDbusBoolean"]
638
 
        rows = table_rows_of_clients(self.clients, keywords)
 
644
        rows = TableOfClients(self.clients, keywords).rows()
639
645
        expected_rows = [
640
646
            "A D-BUS Boolean A Non-D-BUS Boolean",
641
647
            "No              False              ",
645
651
    def test_milliseconds_detection(self):
646
652
        keywords = ["Integer", "Timeout", "Interval", "ApprovalDelay",
647
653
                    "ApprovalDuration", "ExtendedTimeout"]
648
 
        rows = table_rows_of_clients(self.clients, keywords)
 
654
        rows = TableOfClients(self.clients, keywords).rows()
649
655
        expected_rows = ("""
650
656
An Integer Timedelta 1 Timedelta 2 Timedelta 3 Timedelta 4 Timedelta 5
651
657
0          00:00:00    00:00:01    00:00:02    00:00:03    00:00:04   
655
661
        self.assertEqual(rows, expected_rows)
656
662
    def test_empty_and_long_string_values(self):
657
663
        keywords = ["String"]
658
 
        rows = table_rows_of_clients(self.clients, keywords)
 
664
        rows = TableOfClients(self.clients, keywords).rows()
659
665
        expected_rows = ("""
660
666
A String                                                                                                                                                                                                                                                                                                                                  
661
667