diff --git a/confluent_client/bin/nodediscover b/confluent_client/bin/nodediscover index f2119426..c7cfce79 100755 --- a/confluent_client/bin/nodediscover +++ b/confluent_client/bin/nodediscover @@ -27,6 +27,7 @@ if path.startswith('/opt'): sys.path.append(path) import confluent.client as client +import confluent.sortutil as sortutil defcolumns = ['Node', 'Model', 'Serial', 'UUID', 'Mac Address', 'Type', 'Current IP Addresses'] @@ -195,12 +196,15 @@ def import_csv(options, session): def list_discovery(options, session): outhandler = None + orderby = None if options.fields: columns = [] for field in options.fields.split(','): for cdt in columnmapping: if cdt.lower() == field.lower(): columns.append(cdt) + if options.order and options.order.lower() == cdt.lower(): + orderby = cdt else: columns = defcolumns if options.csv: @@ -210,7 +214,7 @@ def list_discovery(options, session): for mac in list_matching_macs(options, session): print_disco(options, session, mac, outhandler, columns) if outhandler: - for row in outhandler.get_table(): + for row in outhandler.get_table(orderby): print(row) def clear_discovery(options, session): @@ -309,6 +313,8 @@ def main(): parser.add_option('-f', '--fields', dest='fields', help='Select fields for output', metavar='FIELDS') + parser.add_option('-o', '--order', dest='order', + help='Order output by given field', metavar='ORDER') (options, args) = parser.parse_args() if len(args) == 0 or args[0] not in ('list', 'assign', 'rescan', 'clear'): parser.print_help() diff --git a/confluent_client/confluent/client.py b/confluent_client/confluent/client.py index 4d89272e..f60faf85 100644 --- a/confluent_client/confluent/client.py +++ b/confluent_client/confluent/client.py @@ -25,6 +25,7 @@ import socket import ssl import sys import confluent.tlvdata as tlvdata +import confluent.sortutil as sortutil SO_PASSCRED = 16 @@ -43,11 +44,13 @@ class Tabulator(object): def add_row(self, row): self.rows.append(row) - def get_table(self): + def get_table(self, order=None): i = 0 fmtstr = '' separator = [] for head in self.headers: + if order and order == head: + order = i neededlen = len(head) for row in self.rows: if len(row[i]) > neededlen: @@ -58,8 +61,14 @@ class Tabulator(object): fmtstr = fmtstr[:-1] yield fmtstr.format(*self.headers) yield fmtstr.format(*separator) - for row in self.rows: - yield fmtstr.format(*row) + if order is not None: + for row in sorted( + self.rows, + key=lambda x: sortutil.naturalize_string(x[order])): + yield fmtstr.format(*row) + else: + for row in self.rows: + yield fmtstr.format(*row) def printerror(res, node=None):