From 0bc142df91048d65dc06d7d4668527ae2de43575 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Fri, 29 Jul 2022 22:32:54 +0300 Subject: [PATCH] Move `format_table` to `util.helper` --- osc/core.py | 21 +-------------------- osc/util/helper.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/osc/core.py b/osc/core.py index 105e359f..27fd612d 100644 --- a/osc/core.py +++ b/osc/core.py @@ -40,7 +40,7 @@ except ImportError: from . import conf from . import oscerr from .connection import http_request, http_GET, http_POST, http_PUT, http_DELETE -from .util.helper import decode_list, decode_it, raw_input, _html_escape +from .util.helper import decode_list, decode_it, raw_input, _html_escape, format_table ET_ENCODING = "unicode" @@ -5626,25 +5626,6 @@ def get_distributions(apiurl, discon=False): f = http_GET(makeurl(apiurl, ['distributions'])) root = ET.fromstring(b''.join(f)) - def format_table(rows, headers): - """Format list of tuples into equal width table with headers""" - maxlens = [len(h) for h in headers] - for r in rows: - for i, c in enumerate(r): - maxlens[i] = max(maxlens[i], len(c)) - tpltpl = [] - for i, m in enumerate(maxlens): - tpltpl.append('{%s:<%s}' % (i, m)) - # {0:12} {1:7} {2:10} {3:8} - templ = ' '.join(tpltpl) + '\n' - - out = '' - out += templ.format(*headers) - out += templ.format(*['-'*m for m in maxlens]) - for r in rows: - out += templ.format(*r) - return out - headers = ('distribution', 'project', 'repository', 'reponame') distlist = [] rows = [] diff --git a/osc/util/helper.py b/osc/util/helper.py index 3bb11a72..77ca8d8e 100644 --- a/osc/util/helper.py +++ b/osc/util/helper.py @@ -51,3 +51,22 @@ def raw_input(*args): def _html_escape(data): return html.escape(data, quote=False) + + +def format_table(rows, headers): + """Format list of tuples into equal width table with headers""" + maxlens = [len(h) for h in headers] + for r in rows: + for i, c in enumerate(r): + maxlens[i] = max(maxlens[i], len(c)) + tpltpl = [] + for i, m in enumerate(maxlens): + tpltpl.append('{%s:<%s}' % (i, m)) + # {0:12} {1:7} {2:10} {3:8} + templ = ' '.join(tpltpl) + '\n' + + out = templ.format(*headers) + out += templ.format(*['-'*m for m in maxlens]) + for r in rows: + out += templ.format(*r) + return out