Skip to content

Commit

Permalink
Merge pull request #981 from dandi/gh-979
Browse files Browse the repository at this point in the history
Add `get_assets_by_glob()`
  • Loading branch information
yarikoptic authored Jul 8, 2022
2 parents 9ce5b1e + b64aad7 commit 5aa08ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,31 @@ def get_assets_with_path_prefix(
f"No such version: {self.version_id!r} of Dandiset {self.identifier}"
)

def get_assets_by_glob(
self, pattern: str, order: Optional[str] = None
) -> Iterator["RemoteAsset"]:
"""
.. versionadded:: 0.44.0
Returns an iterator of all assets in this version of the Dandiset whose
`~RemoteAsset.path` attributes match the glob pattern ``pattern``
Assets can be sorted by a given field by passing the name of that field
as the ``order`` parameter. The accepted field names are
``"created"``, ``"modified"``, and ``"path"``. Prepend a hyphen to the
field name to reverse the sort order.
"""
try:
for a in self.client.paginate(
f"{self.version_api_path}assets/",
params={"glob": pattern, "order": order},
):
yield RemoteAsset.from_data(self, a)
except HTTP404Error:
raise NotFoundError(
f"No such version: {self.version_id!r} of Dandiset {self.identifier}"
)

def get_asset_by_path(self, path: str) -> "RemoteAsset":
"""
Fetch the asset in this version of the Dandiset whose
Expand Down
14 changes: 14 additions & 0 deletions dandi/tests/test_dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,20 @@ def test_get_assets_with_path_prefix(text_dandiset: SampleDandiset) -> None:
] == ["subdir2/coconut.txt", "subdir2/banana.txt", "subdir1/apple.txt"]


def test_get_assets_by_glob(text_dandiset: SampleDandiset) -> None:
assert sorted(
asset.path for asset in text_dandiset.dandiset.get_assets_by_glob("*a*.txt")
) == ["subdir1/apple.txt", "subdir2/banana.txt"]
assert [
asset.path
for asset in text_dandiset.dandiset.get_assets_by_glob("*a*.txt", order="path")
] == ["subdir1/apple.txt", "subdir2/banana.txt"]
assert [
asset.path
for asset in text_dandiset.dandiset.get_assets_by_glob("*a*.txt", order="-path")
] == ["subdir2/banana.txt", "subdir1/apple.txt"]


def test_empty_zarr_iterfiles(new_dandiset: SampleDandiset) -> None:
client = new_dandiset.client
r = client.post(
Expand Down

0 comments on commit 5aa08ac

Please sign in to comment.