Skip to content

Commit 32f90de

Browse files
committed
Added utility to unpack a Gobbler path to its project/asset/version details.
1 parent 321bdaa commit 32f90de

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/pygobbler/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@
4141
from .approve_probation import approve_probation
4242
from .reject_probation import reject_probation
4343
from .set_permissions import set_permissions
44+
from .unpack_path import unpack_path

src/pygobbler/unpack_path.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Dict
2+
3+
#' @export
4+
#' @importFrom utils tail
5+
def unpack_path(path: str) -> Dict[str, str]:
6+
"""Unpack a path to its combination of project, asset, version, and
7+
(optionally) path, for easier use in the various pygobbler functions.
8+
9+
Args:
10+
path:
11+
Relative path within the Gobbler registry.
12+
13+
Return:
14+
Dictionary with the ``project``, ``asset``, ``version`` and ``path`` keys.
15+
All values are strings except for ``path``, which may be None.
16+
"""
17+
components = path.split("/")
18+
if len(components) < 3:
19+
raise ValueError("expected at least 3 path components in 'path'")
20+
21+
if len(components) == 3 or components[3] == "":
22+
path = None
23+
else:
24+
path = "/".join(components[3:])
25+
26+
return { "project": components[0], "asset": components[1], "version": components[2], "path": path }

tests/test_unpack_path.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pygobbler as pyg
2+
3+
4+
def test_unpack_path():
5+
out = pyg.unpack_path("project/asset/version/path")
6+
assert out["project"] == "project"
7+
assert out["asset"] == "asset"
8+
assert out["version"] == "version"
9+
assert out["path"] == "path"
10+
11+
out = pyg.unpack_path("project/asset/version/foo/bar")
12+
assert out["project"] == "project"
13+
assert out["asset"] == "asset"
14+
assert out["version"] == "version"
15+
assert out["path"] == "foo/bar"
16+
17+
out = pyg.unpack_path("project/asset/version/")
18+
assert out["project"] == "project"
19+
assert out["asset"] == "asset"
20+
assert out["version"] == "version"
21+
assert out["path"] is None
22+
23+
out = pyg.unpack_path("project/asset/version")
24+
assert out["project"] == "project"
25+
assert out["asset"] == "asset"
26+
assert out["version"] == "version"
27+
assert out["path"] is None

0 commit comments

Comments
 (0)