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

* mandos-ctl (TableOfClients.rows): Extract methods.
  (TableOfClients.row_formatting_string): New.
  (TableOfClients.string_from_client): New.
  (TableOfClients.header_line): New.
  (TableOfClients.client_line): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
302
302
        if tablewords is not None:
303
303
            self.tablewords = tablewords
304
304
 
 
305
    def rows(self):
 
306
        format_string = self.row_formatting_string()
 
307
        rows = [self.header_line(format_string)]
 
308
        rows.extend(self.client_line(client, format_string)
 
309
                    for client in self.clients)
 
310
        return rows
 
311
 
 
312
    def row_formatting_string(self):
 
313
        "Format string used to format table rows"
 
314
        return " ".join("{{{key}:{width}}}".format(
 
315
            width=max(len(self.tablewords[key]),
 
316
                      max(len(self.string_from_client(client, key))
 
317
                          for client in self.clients)),
 
318
            key=key)
 
319
                                 for key in self.keywords)
 
320
 
 
321
    def string_from_client(self, client, key):
 
322
        return self.valuetostring(client[key], key)
 
323
 
305
324
    @staticmethod
306
325
    def valuetostring(value, keyword):
307
326
        if isinstance(value, dbus.Boolean):
311
330
            return milliseconds_to_string(value)
312
331
        return str(value)
313
332
 
314
 
    def rows(self):
315
 
        # Create format string to format table rows
316
 
        format_string = " ".join("{{{key}:{width}}}".format(
317
 
            width=max(len(self.tablewords[key]),
318
 
                      max(len(self.valuetostring(client[key], key))
319
 
                          for client in self.clients)),
320
 
            key=key)
321
 
                                 for key in self.keywords)
322
 
        # Start with header line
323
 
        rows = [format_string.format(**self.tablewords)]
324
 
        for client in self.clients:
325
 
            rows.append(format_string
326
 
                        .format(**{key: self.valuetostring(client[key], key)
327
 
                                   for key in self.keywords}))
328
 
        return rows
 
333
    def header_line(self, format_string):
 
334
        return format_string.format(**self.tablewords)
 
335
 
 
336
    def client_line(self, client, format_string):
 
337
        return format_string.format(
 
338
            **{key: self.string_from_client(client, key)
 
339
               for key in self.keywords})
329
340
 
330
341
 
331
342
def has_actions(options):