Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robustify dataset/version/edition URI parsing #230

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## ?.?.? - Unreleased

* More robust dataset/version/edition URI parsing.

## 4.2.0 - 2024-06-18

* New commands `okdata -e` and `okdata -v` for printing the current environment
Expand Down
15 changes: 11 additions & 4 deletions okdata/cli/commands/datasets/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ def copy_file(self):
)

def _dataset_components_from_uri(
self, dataset_uri, create_edition=False, auto_resolve=True
self, uri, create_edition=False, auto_resolve=True
):
"""Return an ID/version/edition tuple given a dataset URI.
"""Return a dataset ID/version/edition tuple given a URI.

Four different URI formats are supported:

Expand All @@ -289,8 +289,15 @@ def _dataset_components_from_uri(

Otherwise `None` is returned for missing parts.
"""
parts = dataset_uri.split("/")
dataset_id, version, edition = parts + [None] * (3 - len(parts))
parts = uri.split("/")

try:
dataset_id, version, edition = parts + [None] * (3 - len(parts))
except ValueError:
sys.exit(
"URI must be on the format 'dataset_id', "
"'dataset_id/version', or 'dataset_id/version/edition'."
)

# First verify that the dataset exists; `get_dataset` raises an error
# if not.
Expand Down
5 changes: 5 additions & 0 deletions tests/origocli/commands/datasets/datasets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def test_edition(self, mocker, output):
cmd.handler()
assert output_with_argument(output, [edition])

def test_invalid_uri(self, mocker, output):
cmd = create_cmd(mocker, "ls", "a/b/c/d/e")
with pytest.raises(SystemExit):
cmd.handler()


class TestDatasetsCp:
def test_copy_local_files(self, mocker):
Expand Down
Loading