Skip to content

Commit

Permalink
Cut down on code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Oct 28, 2021
1 parent ada12e7 commit eca1fe3
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions dandi/dandiarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,10 @@ def get_assets(
self, client: DandiAPIClient, order: Optional[str] = None, strict: bool = False
) -> Iterator[BaseRemoteAsset]:
"""Returns all assets in the Dandiset"""
try:
with _maybe_strict(strict):
yield from self.get_dandiset(client, lazy=not strict).get_assets(
order=order
)
except NotFoundError:
if strict:
raise
else:
return


class SingleAssetURL(ParsedDandiURL):
Expand Down Expand Up @@ -194,13 +189,8 @@ def get_assets(
a `NotFoundError` is raised if ``strict`` is true, and nothing is
yielded if ``strict`` is false.
"""
try:
with _maybe_strict(strict):
yield client.get_asset(self.asset_id)
except NotFoundError:
if strict:
raise
else:
return

def get_asset_ids(self, client: DandiAPIClient) -> Iterator[str]:
"""Yields the ID of the asset (regardless of whether it exists)"""
Expand All @@ -222,13 +212,8 @@ def get_assets(
exist, then a `NotFoundError` is raised if ``strict`` is true, and
nothing is yielded if ``strict`` is false.
"""
try:
with _maybe_strict(strict):
yield self.get_dandiset(client, lazy=not strict).get_asset(self.asset_id)
except NotFoundError:
if strict:
raise
else:
return

def get_asset_ids(self, client: DandiAPIClient) -> Iterator[str]:
"""Yields the ID of the asset (regardless of whether it exists)"""
Expand All @@ -244,15 +229,10 @@ def get_assets(
self, client: DandiAPIClient, order: Optional[str] = None, strict: bool = False
) -> Iterator[BaseRemoteAsset]:
"""Returns the assets whose paths start with `path`"""
try:
with _maybe_strict(strict):
yield from self.get_dandiset(
client, lazy=not strict
).get_assets_with_path_prefix(self.path, order=order)
except NotFoundError:
if strict:
raise
else:
return


class AssetItemURL(SingleAssetURL):
Expand Down Expand Up @@ -319,15 +299,19 @@ def get_assets(
path = self.path
if not path.endswith("/"):
path += "/"
try:
with _maybe_strict(strict):
yield from self.get_dandiset(
client, lazy=not strict
).get_assets_with_path_prefix(path, order=order)
except NotFoundError:
if strict:
raise
else:
return


@contextmanager
def _maybe_strict(strict: bool) -> Iterator[None]:
try:
yield
except NotFoundError:
if strict:
raise


@contextmanager
Expand Down

0 comments on commit eca1fe3

Please sign in to comment.