Skip to content

Commit 0b34302

Browse files
jsamjirikuncar
authored andcommitted
chore(datasets): add remapping of headers in tabulate
Updates table header names `short_id` to `id` and `authors_csv` to `authors`.
1 parent 1997e34 commit 0b34302

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

renku/cli/dataset.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
my-dataset/
7878
datafile
7979
"""
80+
from collections import OrderedDict
8081

8182
import click
8283
from click import BadParameter
@@ -96,12 +97,15 @@ def dataset(ctx, client, datadir):
9697
"""Handle datasets."""
9798
ctx.meta['renku.datasets.datadir'] = datadir
9899

99-
if ctx.invoked_subcommand is None:
100-
table = tabulate(
101-
client.datasets.values(),
102-
headers=['short_id', 'name', 'authors_csv', 'created'],
103-
)
104-
click.echo(table)
100+
if ctx.invoked_subcommand is not None:
101+
return
102+
103+
output = tabulate(
104+
client.datasets.values(),
105+
headers=OrderedDict((('short_id', 'id'), ('name', None),
106+
('created', None), ('authors_csv', 'authors'))),
107+
)
108+
click.echo(output)
105109

106110

107111
@dataset.command()

renku/models/_tabulate.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ def format_cell(cell, datetime_fmt=None):
3232

3333
def tabulate(collection, headers, datetime_fmt='%Y-%m-%d %H:%M:%S', **kwargs):
3434
"""Pretty-print a collection."""
35+
if isinstance(headers, dict):
36+
attrs = headers.keys()
37+
# if mapping is not specified keep original
38+
names = [
39+
key if value is None else value for key, value in headers.items()
40+
]
41+
else:
42+
attrs = names = headers
3543
table = [(
3644
format_cell(cell, datetime_fmt=datetime_fmt)
37-
for cell in attrgetter(*headers)(c)
45+
for cell in attrgetter(*attrs)(c)
3846
) for c in collection]
39-
return tblte(table, headers=[h.upper() for h in headers], **kwargs)
47+
return tblte(table, headers=[h.upper() for h in names], **kwargs)

tests/test_cli.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,11 @@ def test_datasets_list_empty(runner, project):
355355
result = runner.invoke(cli.cli, ['dataset'])
356356
assert result.exit_code == 0
357357

358-
rows = result.output.split('\n')
359-
assert len(rows) == 3
358+
output = result.output.split('\n')
359+
assert output.pop(0).split() == ['ID', 'NAME', 'CREATED', 'AUTHORS']
360+
assert set(output.pop(0)) == {'-', ' '}
361+
assert output.pop(0) == ''
362+
assert not output
360363

361364

362365
def test_datasets_list_non_empty(runner, project):
@@ -367,8 +370,13 @@ def test_datasets_list_non_empty(runner, project):
367370
result = runner.invoke(cli.cli, ['dataset'])
368371
assert result.exit_code == 0
369372

370-
rows = set(result.output.split('\n'))
371-
assert len(rows) == 4
373+
output = result.output.split('\n')
374+
375+
assert output.pop(0).split() == ['ID', 'NAME', 'CREATED', 'AUTHORS']
376+
assert set(output.pop(0)) == {'-', ' '}
377+
assert 'dataset' in output.pop(0)
378+
assert output.pop(0) == ''
379+
assert not output
372380

373381

374382
def test_multiple_file_to_dataset(

0 commit comments

Comments
 (0)