Skip to content

Commit 713b4a4

Browse files
authored
feat(cli): show dataset metadata for tag (#2919)
1 parent b6895bd commit 713b4a4

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

renku/core/dataset/dataset.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,17 +733,30 @@ def update_datasets(
733733
return imported_datasets_view_models, dataset_files_view_models
734734

735735

736-
def show_dataset(name):
736+
def show_dataset(name: str, tag: Optional[str] = None):
737737
"""Show detailed dataset information.
738738
739739
Args:
740-
name: Name of dataset to show details for.
740+
name(str): Name of dataset to show details for.
741+
tag(str, optional): Tags for which to get the metadata (Default value = None).
741742
742743
Returns:
743744
dict: JSON dictionary of dataset details.
744745
"""
745-
dataset = DatasetsProvenance().get_by_name(name, strict=True)
746+
datasets_provenance = DatasetsProvenance()
747+
dataset = datasets_provenance.get_by_name(name, strict=True)
748+
749+
if tag is None:
750+
return DatasetDetailsJson().dump(dataset)
751+
752+
tags = datasets_provenance.get_all_tags(dataset=cast(Dataset, dataset))
753+
754+
selected_tag = next((t for t in tags if t.name == tag), None)
755+
756+
if selected_tag is None:
757+
raise errors.DatasetTagNotFound(tag)
746758

759+
dataset = datasets_provenance.get_by_id(selected_tag.dataset_id.value)
747760
return DatasetDetailsJson().dump(dataset)
748761

749762

renku/core/errors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ def __init__(self, *, name=None, message=None):
270270
super().__init__(msg)
271271

272272

273+
class DatasetTagNotFound(RenkuException):
274+
"""Raise when a tag can't be found."""
275+
276+
def __init__(self, tag) -> None:
277+
msg = f"Couldn't find dataset tag '{tag}'."
278+
super().__init__(msg)
279+
280+
273281
class ExternalFileNotFound(RenkuException):
274282
"""Raise when an external file is not found."""
275283

renku/ui/cli/dataset.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
Description:
134134
Just some dataset
135135
136+
You can also show details for a specific tag using the ``--tag`` option.
137+
136138
Deleting a dataset:
137139
138140
.. code-block:: console
@@ -681,13 +683,14 @@ def edit(name, title, description, creators, metadata, keyword):
681683

682684

683685
@dataset.command("show")
686+
@click.option("-t", "--tag", default=None, type=click.STRING, help="Tag for which to show dataset metadata.")
684687
@click.argument("name", shell_complete=_complete_datasets)
685-
def show(name):
688+
def show(tag, name):
686689
"""Show metadata of a dataset."""
687690
from renku.command.dataset import show_dataset_command
688691
from renku.ui.cli.utils.terminal import print_markdown
689692

690-
result = show_dataset_command().build().execute(name=name)
693+
result = show_dataset_command().build().execute(name=name, tag=tag)
691694
ds = result.output
692695

693696
click.echo(click.style("Name: ", bold=True, fg=color.MAGENTA) + click.style(ds["name"], bold=True))

tests/cli/test_datasets.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,77 @@ def test_dataset_show(runner, client, subdirectory):
128128
assert "##" not in result.output
129129

130130

131+
def test_dataset_show_tag(runner, client, subdirectory):
132+
"""Test creating and showing a dataset with metadata."""
133+
result = runner.invoke(cli, ["dataset", "show", "my-dataset"])
134+
assert 1 == result.exit_code, format_result_exception(result)
135+
assert 'Dataset "my-dataset" is not found.' in result.output
136+
137+
metadata = {
138+
"@id": "https://example.com/annotation1",
139+
"@type": "https://schema.org/specialType",
140+
"https://schema.org/specialProperty": "some_unique_value",
141+
}
142+
metadata_path = client.path / "metadata.json"
143+
metadata_path.write_text(json.dumps(metadata))
144+
145+
result = runner.invoke(
146+
cli,
147+
[
148+
"dataset",
149+
"create",
150+
"my-dataset",
151+
"--title",
152+
"Long Title",
153+
"--description",
154+
"description1",
155+
],
156+
)
157+
assert 0 == result.exit_code, format_result_exception(result)
158+
assert "OK" in result.output
159+
160+
result = runner.invoke(cli, ["dataset", "tag", "my-dataset", "tag1"])
161+
assert 0 == result.exit_code, format_result_exception(result)
162+
163+
result = runner.invoke(cli, ["dataset", "edit", "-d", "description2", "my-dataset"])
164+
assert 0 == result.exit_code, format_result_exception(result)
165+
assert "Successfully updated: description" in result.output
166+
167+
result = runner.invoke(cli, ["dataset", "tag", "my-dataset", "tag2"])
168+
assert 0 == result.exit_code, format_result_exception(result)
169+
170+
result = runner.invoke(cli, ["dataset", "edit", "-d", "description3", "my-dataset"])
171+
assert 0 == result.exit_code, format_result_exception(result)
172+
assert "Successfully updated: description" in result.output
173+
174+
result = runner.invoke(cli, ["dataset", "tag", "my-dataset", "tag3"])
175+
assert 0 == result.exit_code, format_result_exception(result)
176+
177+
result = runner.invoke(cli, ["dataset", "show", "my-dataset"])
178+
assert 0 == result.exit_code, format_result_exception(result)
179+
assert "description3" in result.output
180+
assert "description2" not in result.output
181+
assert "description1" not in result.output
182+
183+
result = runner.invoke(cli, ["dataset", "show", "--tag", "tag3", "my-dataset"])
184+
assert 0 == result.exit_code, format_result_exception(result)
185+
assert "description3" in result.output
186+
assert "description2" not in result.output
187+
assert "description1" not in result.output
188+
189+
result = runner.invoke(cli, ["dataset", "show", "--tag", "tag2", "my-dataset"])
190+
assert 0 == result.exit_code, format_result_exception(result)
191+
assert "description2" in result.output
192+
assert "description3" not in result.output
193+
assert "description1" not in result.output
194+
195+
result = runner.invoke(cli, ["dataset", "show", "--tag", "tag1", "my-dataset"])
196+
assert 0 == result.exit_code, format_result_exception(result)
197+
assert "description1" in result.output
198+
assert "description2" not in result.output
199+
assert "description3" not in result.output
200+
201+
131202
def test_datasets_create_different_names(runner, client):
132203
"""Test creating datasets with same title but different name."""
133204
result = runner.invoke(cli, ["dataset", "create", "dataset-1", "--title", "title"])

0 commit comments

Comments
 (0)