Skip to content

Commit

Permalink
Merge pull request #230 from oslokommune/robustify-uri-parsing
Browse files Browse the repository at this point in the history
Robustify dataset/version/edition URI parsing
  • Loading branch information
simenheg authored Sep 24, 2024
2 parents ee37e5c + 6930212 commit 047172b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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

0 comments on commit 047172b

Please sign in to comment.