/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-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-17 21:29:32 UTC
  • Revision ID: teddy@recompile.se-20190317212932-r3libgz33mkb85rw
mandos-ctl: Refactor

* mandos-ctl: For Python 2, use StringIO.StringIO as a replacement for
              io.StringIO, since Python 2's io.StringIO won't work
              with print redirection.
  (Output.run, Output.output): Remove.
  (DumpJSON.output): Rename to "run" and change signature to match.
                     Also change code to print instead of returning
                     string.
  (PrintTable.output): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
 
62
62
if sys.version_info.major == 2:
63
63
    str = unicode
 
64
    import StringIO
 
65
    io.StringIO = StringIO.StringIO
64
66
 
65
67
locale.setlocale(locale.LC_ALL, "")
66
68
 
613
615
                        "Checker", "ExtendedTimeout", "Expires",
614
616
                        "LastCheckerStatus")
615
617
 
616
 
        def run(self, clients, bus=None, mandos=None):
617
 
            print(self.output(clients.values()))
618
 
 
619
 
        def output(self, clients):
620
 
            raise NotImplementedError()
621
 
 
622
618
 
623
619
    class DumpJSON(Output):
624
 
        def output(self, clients):
 
620
        def run(self, clients, bus=None, mandos=None):
625
621
            data = {client["Name"]:
626
622
                    {key: self.dbus_boolean_to_bool(client[key])
627
623
                     for key in self.all_keywords}
628
 
                    for client in clients}
629
 
            return json.dumps(data, indent=4, separators=(',', ': '))
 
624
                    for client in clients.values()}
 
625
            print(json.dumps(data, indent=4, separators=(',', ': ')))
630
626
 
631
627
        @staticmethod
632
628
        def dbus_boolean_to_bool(value):
639
635
        def __init__(self, verbose=False):
640
636
            self.verbose = verbose
641
637
 
642
 
        def output(self, clients):
 
638
        def run(self, clients, bus=None, mandos=None):
643
639
            default_keywords = ("Name", "Enabled", "Timeout",
644
640
                                "LastCheckedOK")
645
641
            keywords = default_keywords
646
642
            if self.verbose:
647
643
                keywords = self.all_keywords
648
 
            return str(self.TableOfClients(clients, keywords))
 
644
            print(self.TableOfClients(clients.values(), keywords))
649
645
 
650
646
        class TableOfClients(object):
651
647
            tableheaders = {