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

Add include_metadata=False parameter to asset-listing DandiAPIClient methods #378

Merged
merged 3 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 22 additions & 6 deletions dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,37 @@ def set_dandiset_metadata(self, dandiset_id, *, metadata):
json={"metadata": metadata, "name": metadata.get("name", "")},
)

def get_dandiset_assets(self, dandiset_id, version, page_size=None, path=None):
def get_dandiset_assets(
self, dandiset_id, version, page_size=None, path=None, include_metadata=False
):
""" A generator to provide asset records """
resp = self.get(
f"/dandisets/{dandiset_id}/versions/{version}/assets/",
parameters={"page_size": page_size, "path": path},
)
while True:
yield from resp["results"]
for asset in resp["results"]:
if include_metadata:
asset["metadata"] = self.get_asset(
dandiset_id, version, asset["uuid"]
)["metadata"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to be resilient and rely on tests to catch if behavior of the api server would change: please get_asset, and only if metadata is within the record - assign to the asset

yield asset
if resp.get("next"):
resp = self.get(resp["next"])
else:
break

def get_dandiset_and_assets(self, dandiset_id, version):
def get_dandiset_and_assets(self, dandiset_id, version, include_metadata=False):
"""This is pretty much an adapter to provide "harmonized" output in both
girder and DANDI api clients.

Harmonization should happen toward DANDI API BUT AFAIK it is still influx
"""
lgr.info(f"Traversing {dandiset_id} (version: {version})")
dandiset = self.get_dandiset(dandiset_id, version)
assets = self.get_dandiset_assets(dandiset_id, version)
assets = self.get_dandiset_assets(
dandiset_id, version, include_metadata=include_metadata
)
return dandiset, assets

def get_download_file_iter(
Expand Down Expand Up @@ -509,13 +518,20 @@ def download_assets_directory(
dandiset_id, version, a["uuid"], filepath, chunk_size=chunk_size
)

def get_asset_bypath(self, dandiset_id, version, asset_path):
def get_asset_bypath(
self, dandiset_id, version, asset_path, include_metadata=False
):
try:
# Weed out any assets that happen to have the given path as a
# proper prefix:
(asset,) = (
a
for a in self.get_dandiset_assets(dandiset_id, version, path=asset_path)
for a in self.get_dandiset_assets(
dandiset_id,
version,
path=asset_path,
include_metadata=include_metadata,
)
if a["path"] == asset_path
)
except ValueError:
Expand Down
33 changes: 33 additions & 0 deletions dandi/tests/test_dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,36 @@ def downloaded_files():
)
assert sorted(downloaded_files()) == [dandiset_yaml, file_in_version]
assert file_in_version.read_text() == "This is test text.\n"


def test_get_asset_include_metadata(local_dandi_api, simple1_nwb, tmp_path):
client = DandiAPIClient(
api_url=local_dandi_api["instance"].api, token=local_dandi_api["api_key"]
)
with client.session():
r = client.create_dandiset(name="Include Metadata Test", metadata={})
dandiset_id = r["identifier"]
client.upload(
dandiset_id, "draft", "testing/simple1.nwb", {"foo": "bar"}, simple1_nwb
)

asset, = client.get_dandiset_assets(dandiset_id, "draft")
assert "metadata" not in asset
asset, = client.get_dandiset_assets(dandiset_id, "draft", include_metadata=True)
assert asset["metadata"] == {"foo": "bar"}

_, (asset,) = client.get_dandiset_and_assets(dandiset_id, "draft")
assert "metadata" not in asset
_, (asset,) = client.get_dandiset_and_assets(
dandiset_id, "draft", include_metadata=True
)
assert asset["metadata"] == {"foo": "bar"}

asset = client.get_asset_bypath(dandiset_id, "draft", "testing/simple1.nwb")
assert asset is not None
assert "metadata" not in asset
asset = client.get_asset_bypath(
dandiset_id, "draft", "testing/simple1.nwb", include_metadata=True
)
assert asset is not None
assert asset["metadata"] == {"foo": "bar"}