Skip to content

Commit

Permalink
Fix handling of DOIs that start with doi: (#59)
Browse files Browse the repository at this point in the history
* Fix handling of DOIs that start with `doi:`

* Run tests on Python 3.9; remove schedule
  • Loading branch information
hoechenberger authored Jan 13, 2022
1 parent a4fb393 commit 584896d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ on:
create:
branches: [main]
tags: ['**']
schedule:
- cron: "0 4 * * *"
# schedule:
# - cron: "0 4 * * *"

jobs:
lint:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ on:
create:
branches: [main]
tags: ['**']
schedule:
- cron: "0 4 * * *"
# schedule:
# - cron: "0 4 * * *"

jobs:
test:
Expand All @@ -25,7 +25,7 @@ jobs:
fail-fast: false
matrix:
# python-version: [3.7, 3.8, 3.9]
python-version: [3.7]
python-version: [3.9]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2022.1.0

- Fix handling of DOIs that start with `doi:`, as found e.g. in `ds002778`.

## 2021.10.1

- New release for PyPI.
Expand Down
10 changes: 8 additions & 2 deletions openneuro/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,22 @@ def _get_local_tag(
'"DatasetDOI" field. Are you sure this is the '
'correct directory?')

local_doi = local_json['DatasetDOI']
if local_doi.startswith('doi:'):
# Remove the "protocol" prefix
local_doi = local_doi[4:]

expected_doi_start = f'10.18112/openneuro.{dataset_id}.v'
if not local_json['DatasetDOI'].startswith(expected_doi_start):

if not local_doi.startswith(expected_doi_start):
raise RuntimeError(f'The existing dataset in the target directory '
f'appears to be different from the one you '
f'requested to download. "DatasetDOI" field in '
f'local "dataset_description.json": '
f'{local_json["DatasetDOI"]}. '
f'Requested dataset: {dataset_id}')

local_version = (local_json['DatasetDOI']
local_version = (local_doi
.replace(f'10.18112/openneuro.{dataset_id}.v', ''))
return local_version

Expand Down
31 changes: 31 additions & 0 deletions openneuro/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,34 @@ def test_ds000248(tmp_path: Path):
include=['participants.tsv'],
target_dir=tmp_path
)


def test_doi_handling(tmp_path: Path):
"""Test that we can handle DOIs that start with 'doi:`."""
dataset = 'ds000248'
download(
dataset=dataset,
include=['participants.tsv'],
target_dir=tmp_path
)

# Now inject a `doi:` prefix into the DOI
dataset_description_path = tmp_path / 'dataset_description.json'
dataset_description = json.loads(
dataset_description_path.read_text(encoding='utf-8')
)
assert not dataset_description['DatasetDOI'].startswith('doi:')
dataset_description['DatasetDOI'] = (
'doi:' + dataset_description['DatasetDOI']
)
dataset_description_path.write_text(
data=json.dumps(dataset_description, indent=2),
encoding='utf-8'
)

# Try to download again
download(
dataset=dataset,
include=['participants.tsv'],
target_dir=tmp_path
)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = openneuro-py
version = 2021.10.1
version = 2022.1.0
author = Richard Höchenberger <[email protected]>
author_email = [email protected]
url = https://github.com/hoechenberger/openneuro-py
Expand Down

0 comments on commit 584896d

Please sign in to comment.