1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-01-27 07:06:13 +01:00

Merge pull request #1093 from abitrolly/distformat

Move out table formatting in `osc dist` into helper function
This commit is contained in:
Daniel Mach 2022-08-02 15:43:53 +02:00 committed by GitHub
commit 43337afdf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 57 deletions

View File

@ -19,7 +19,7 @@ from . import conf
from . import oscerr
from .core import *
from .util import safewriter
from .util.helper import _html_escape
from .util.helper import _html_escape, format_table
MAN_HEADER = r""".TH %(ucname)s "1" "%(date)s" "%(name)s %(version)s" "User Commands"
@ -5341,10 +5341,6 @@ Please submit there instead, or use --nodevelproject to force direct submission.
@cmdln.alias('dists')
# FIXME: using just ^DISCONTINUED as match is not a general approach and only valid for one instance
# we need to discuss an api call for that, if we need this
# @cmdln.option('-d', '--discontinued', action='store_true',
# help='show discontinued distributions')
def do_distributions(self, subcmd, opts, *args):
"""${cmd_name}: Shows all available distributions
@ -5358,7 +5354,14 @@ Please submit there instead, or use --nodevelproject to force direct submission.
"""
apiurl = self.get_api_url()
print('\n'.join(get_distibutions(apiurl))) # FIXME:, opts.discontinued))
dists = get_distributions(apiurl)
if dists:
headers = dists[0].keys()
rows = []
for dist in dists:
rows.append([dist[h] for h in headers])
print(format_table(rows, headers).rstrip())
@cmdln.hide(1)
def do_results_meta(self, subcmd, opts, *args):

View File

@ -5603,59 +5603,23 @@ def get_repositories(apiurl):
return r
def get_distibutions(apiurl, discon=False):
r = []
def get_distributions(apiurl):
"""Returns list of dicts with headers
'distribution', 'project', 'repository', 'reponame'"""
# FIXME: this is just a naming convention on api.opensuse.org, but not a general valid apparoach
if discon:
result_line_templ = '%(name)-25s %(project)s'
f = http_GET(makeurl(apiurl, ['build']))
root = ET.fromstring(''.join(f))
f = http_GET(makeurl(apiurl, ['distributions']))
root = ET.fromstring(b''.join(f))
for node in root.findall('entry'):
if node.get('name').startswith('DISCONTINUED:'):
rmap = {}
rmap['name'] = node.get('name').replace('DISCONTINUED:', '').replace(':', ' ')
rmap['project'] = node.get('name')
r.append(result_line_templ % rmap)
r.insert(0, 'distribution project')
r.insert(1, '------------ -------')
else:
f = http_GET(makeurl(apiurl, ['distributions']))
root = ET.fromstring(b''.join(f))
distlist = []
for node in root.findall('distribution'):
dmap = {}
for child in node:
if child.tag in ('name', 'project', 'repository', 'reponame'):
dmap[child.tag] = child.text
dmap['distribution'] = dmap.pop('name')
distlist.append(dmap)
# pretty printing table
headers = ('distribution', 'project', 'repository', 'reponame')
maxlen = [len(h) for h in headers]
for d in distlist:
for i,field in enumerate(headers):
maxlen[i] = max(maxlen[i], len(d[field]))
def format_row(dist, proj, repotype, reponame):
result_line_templ = '%-*s %-*s %-*s %-s'
return result_line_templ % (
maxlen[0], dist,
maxlen[1], proj,
maxlen[2], repotype,
reponame
)
r.append(format_row('distribution', 'project', 'repository', 'reponame'))
r.append(format_row('-'*maxlen[0], '-'*maxlen[1], '-'*maxlen[2], '-'*maxlen[2]))
for d in distlist:
r.append(format_row(d['distribution'], d['project'], d['repository'], d['reponame']))
return r
distlist = []
for node in root.findall('distribution'):
dmap = {}
for child in node:
if child.tag == 'name':
dmap['distribution'] = child.text
elif child.tag in ('project', 'repository', 'reponame'):
dmap[child.tag] = child.text
distlist.append(dmap)
return distlist
# old compat lib call

View File

@ -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