From 96df7871cb72f32788c8be01d7a62717d65554e8 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Tue, 6 Nov 2018 15:28:27 -0500 Subject: [PATCH] Convert nodediscover to use Tabulator This opens the path to more customized output and have the appropriate formatting. Also revise the Tabulator code to more closely match the original nodediscover list output. --- confluent_client/bin/nodediscover | 15 ++++++++------- confluent_client/confluent/client.py | 7 +++++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/confluent_client/bin/nodediscover b/confluent_client/bin/nodediscover index d9514651..b4c35dfb 100755 --- a/confluent_client/bin/nodediscover +++ b/confluent_client/bin/nodediscover @@ -31,15 +31,13 @@ import confluent.client as client tabformat = '{0:>15}|{1:>15}|{2:>15}|{3:>36}|{4:>17}|{5:>12}|{6:>48}' columns = ['Node', 'Model', 'Serial', 'UUID', 'Mac Address', 'Type', 'Current IP Addresses'] -delimit = ['-' * 15, '-' * 15, '-' * 15, '-' * 36, '-' * 17, '-' * 12, - '-' * 48] def dumpmacs(procinfo): return ','.join(procinfo['macs']) # + procinfo.get('relatedmacs', [])) -def print_disco(options, session, currmac): +def print_disco(options, session, currmac, outhandler): procinfo = {} for tmpinfo in session.read('/discovery/by-mac/{0}'.format(currmac)): @@ -51,7 +49,8 @@ def print_disco(options, session, currmac): if options.csv: csv.writer(sys.stdout).writerow(record) else: - print(tabformat.format(*record)) + outhandler.add_row(record) + #print(tabformat.format(*record)) def process_header(header): @@ -188,10 +187,12 @@ def list_discovery(options, session): if options.csv: csv.writer(sys.stdout).writerow(columns) else: - print(tabformat.format(*columns)) - print(tabformat.format(*delimit)) + outhandler = client.Tabulator(columns) + #print(tabformat.format(*columns)) + #print(tabformat.format(*delimit)) for mac in list_matching_macs(options, session): - print_disco(options, session, mac) + print_disco(options, session, mac, outhandler) + print(outhandler.get_table()) def clear_discovery(options, session): for mac in list_matching_macs(options, session): diff --git a/confluent_client/confluent/client.py b/confluent_client/confluent/client.py index d5ed1035..4d89272e 100644 --- a/confluent_client/confluent/client.py +++ b/confluent_client/confluent/client.py @@ -46,15 +46,18 @@ class Tabulator(object): def get_table(self): i = 0 fmtstr = '' + separator = [] for head in self.headers: neededlen = len(head) for row in self.rows: if len(row[i]) > neededlen: neededlen = len(row[i]) - fmtstr += '{{{0}:>{1}}} |'.format(i, neededlen + 1) + separator.append('-' * (neededlen + 1)) + fmtstr += '{{{0}:>{1}}}|'.format(i, neededlen + 1) i = i + 1 - fmtstr = fmtstr[:-2] + fmtstr = fmtstr[:-1] yield fmtstr.format(*self.headers) + yield fmtstr.format(*separator) for row in self.rows: yield fmtstr.format(*row)