Skip to content
13 changes: 11 additions & 2 deletions dandi/cli/cmd_service_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from requests.auth import HTTPBasicAuth
from requests.exceptions import HTTPError

from dandi.consts import known_instances

from .base import ChoiceList, instance_option, map_to_click_exceptions
from .. import __version__, lgr
from ..dandiapi import DandiAPIClient, RemoteBlobAsset, RESTFullAPIClient
Expand Down Expand Up @@ -246,8 +248,15 @@ def update_dandiset_from_doi(
Update the metadata for the draft version of a Dandiset with information
from a given DOI record.
"""
if dandiset.startswith("DANDI:"):
dandiset = dandiset[6:]
known_instance_names = [k.upper() for k in known_instances.keys()]

# Strip instance name prefix from dandiset ID, if present
for name in known_instance_names:
dandiset_id_prefix = f"{name}:"
if dandiset.startswith(dandiset_id_prefix):
dandiset = dandiset[len(dandiset_id_prefix) :]
break

start_time = datetime.now().astimezone()
with DandiAPIClient.for_dandi_instance(dandi_instance, authenticate=True) as client:
with RESTFullAPIClient(
Expand Down
5 changes: 3 additions & 2 deletions dandi/cli/tests/test_service_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import anys
from click.testing import CliRunner
from dandischema.consts import DANDI_SCHEMA_VERSION
from dandischema.models import ID_PATTERN
import pytest

from dandi import __version__
Expand Down Expand Up @@ -105,13 +106,13 @@ def test_update_dandiset_from_doi(
metadata = new_dandiset.dandiset.get_raw_metadata()
with (DATA_DIR / "update_dandiset_from_doi" / f"{name}.json").open() as fp:
expected = json.load(fp)
expected["id"] = f"DANDI:{dandiset_id}/draft"
expected["id"] = anys.AnyFullmatch(rf"{ID_PATTERN}:{dandiset_id}/draft")
expected["url"] = f"{repository}/dandiset/{dandiset_id}/draft"
expected["@context"] = (
"https://raw.githubusercontent.com/dandi/schema/master/releases"
f"/{DANDI_SCHEMA_VERSION}/context.json"
)
expected["identifier"] = f"DANDI:{dandiset_id}"
expected["identifier"] = anys.AnyFullmatch(rf"{ID_PATTERN}:{dandiset_id}")
expected["repository"] = repository
expected["dateCreated"] = anys.ANY_AWARE_DATETIME_STR
expected["schemaVersion"] = DANDI_SCHEMA_VERSION
Expand Down
3 changes: 2 additions & 1 deletion dandi/dandiarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,8 @@ def parse(
continue
groups = match.groupdict()
if "instance_name" in groups:
# map to lower case so we could immediately map DANDI: into "dandi" instance
# map to lower case so we could immediately map an instance name to
# an instance in `dandi.consts.known_instances`
groups["instance_name"] = groups["instance_name"].lower()
lgr.log(5, "Matched %r into %s", url, groups)
rewrite = settings.get("rewrite", False)
Expand Down
12 changes: 1 addition & 11 deletions dandi/dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,7 @@ def _get_identifier(metadata: dict) -> str | None:
id_ = metadata["identifier"]
lgr.debug("Found identifier %s in top level 'identifier'", str(id_))

if isinstance(id_, dict):
# New formalized model, but see below DANDI: way
# TODO: add schemaVersion handling but only after we have them provided
# in all metadata records from dandi-api server
if id_.get("propertyID") != "DANDI":
raise ValueError(
f"Got following identifier record when was expecting a record "
f"with 'propertyID: DANDI': {id_}"
)
id_ = str(id_.get("value", ""))
elif id_ is not None:
if id_ is not None:
assert isinstance(id_, str)
# result of https://github.com/dandi/dandi-cli/pull/348 which ???
# TODO: RF to avoid this evil!!!
Expand Down
3 changes: 2 additions & 1 deletion dandi/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import time
from unittest import mock

from dandischema.models import ID_PATTERN
import numpy as np
import pytest
from pytest_mock import MockerFixture
Expand Down Expand Up @@ -258,7 +259,7 @@ def test_download_dandiset_yaml(text_dandiset: SampleDandiset, tmp_path: Path) -
assert list_paths(tmp_path, dirs=True) == [tmp_path / dandiset_metadata_file]
with (tmp_path / dandiset_metadata_file).open(encoding="utf-8") as fp:
metadata = yaml_load(fp)
assert metadata["id"] == f"DANDI:{dandiset_id}/draft"
assert re.match(rf"^{ID_PATTERN}:{dandiset_id}/draft$", metadata["id"])


def test_download_asset_id(text_dandiset: SampleDandiset, tmp_path: Path) -> None:
Expand Down