Skip to content

Commit 4d725d2

Browse files
authored
Merge pull request #1384 from dandi/gh-1383
`dandi download dandi://…/dandiset.yaml` now downloads `dandiset.yaml`
2 parents 32a6862 + c4a3828 commit 4d725d2

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

dandi/download.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@
2828
from . import get_logger
2929
from .consts import RETRY_STATUSES, dandiset_metadata_file
3030
from .dandiapi import AssetType, BaseRemoteZarrAsset, RemoteDandiset
31-
from .dandiarchive import DandisetURL, ParsedDandiURL, SingleAssetURL, parse_dandi_url
31+
from .dandiarchive import (
32+
AssetItemURL,
33+
DandisetURL,
34+
ParsedDandiURL,
35+
SingleAssetURL,
36+
parse_dandi_url,
37+
)
3238
from .dandiset import Dandiset
3339
from .exceptions import NotFoundError
3440
from .files import LocalAsset, find_dandi_files
@@ -227,7 +233,13 @@ def download_generator(self) -> Iterator[dict]:
227233
"""
228234

229235
with self.url.navigate(strict=True) as (client, dandiset, assets):
230-
if isinstance(self.url, DandisetURL) and self.get_metadata:
236+
if (
237+
isinstance(self.url, DandisetURL)
238+
or (
239+
isinstance(self.url, AssetItemURL)
240+
and self.url.path == "dandiset.yaml"
241+
)
242+
) and self.get_metadata:
231243
assert dandiset is not None
232244
for resp in _populate_dandiset_yaml(
233245
self.output_path, dandiset, self.existing
@@ -236,6 +248,8 @@ def download_generator(self) -> Iterator[dict]:
236248
"path": str(self.output_prefix / dandiset_metadata_file),
237249
**resp,
238250
}
251+
if isinstance(self.url, AssetItemURL):
252+
return
239253

240254
# TODO: do analysis of assets for early detection of needed renames
241255
# etc to avoid any need for late treatment of existing and also for

dandi/tests/test_download.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
)
3232
from ..exceptions import NotFoundError
3333
from ..support.digests import Digester
34-
from ..utils import list_paths
34+
from ..utils import list_paths, yaml_load
3535

3636

3737
# both urls point to 000027 (lean test dataset), and both draft and "released"
@@ -182,6 +182,18 @@ def test_download_item(text_dandiset: SampleDandiset, tmp_path: Path) -> None:
182182
assert (tmp_path / "coconut.txt").read_text() == "Coconut\n"
183183

184184

185+
def test_download_dandiset_yaml(text_dandiset: SampleDandiset, tmp_path: Path) -> None:
186+
dandiset_id = text_dandiset.dandiset_id
187+
download(
188+
f"dandi://{text_dandiset.api.instance_id}/{dandiset_id}/dandiset.yaml",
189+
tmp_path,
190+
)
191+
assert list_paths(tmp_path, dirs=True) == [tmp_path / dandiset_metadata_file]
192+
with (tmp_path / dandiset_metadata_file).open(encoding="utf-8") as fp:
193+
metadata = yaml_load(fp)
194+
assert metadata["id"] == f"DANDI:{dandiset_id}/draft"
195+
196+
185197
def test_download_asset_id(text_dandiset: SampleDandiset, tmp_path: Path) -> None:
186198
asset = text_dandiset.dandiset.get_asset_by_path("subdir2/coconut.txt")
187199
download(asset.download_url, tmp_path)

0 commit comments

Comments
 (0)