Skip to content

Commit b2b7560

Browse files
committed
handle versions in Figshare properly
1 parent b2b6030 commit b2b7560

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

repo2docker/contentproviders/figshare.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Figshare(DoiProvider):
2020
2121
Examples:
2222
- https://doi.org/10.6084/m9.figshare.9782777
23+
- https://doi.org/10.6084/m9.figshare.9782777.v2
2324
- https://figshare.com/articles/binder-examples_requirements/9784088 (only one zipfile, no DOI)
2425
"""
2526

@@ -38,7 +39,7 @@ def __init__(self):
3839
}
3940
]
4041

41-
url_regex = re.compile(r"(.*)/articles/([^/]+)/(\d+)(/\d)?")
42+
url_regex = re.compile(r"(.*)/articles/([^/]+)/(\d+)(/)?(\d)?")
4243

4344
def detect(self, doi, ref=None, extra_args=None):
4445
"""Trigger this provider for things that resolve to a Figshare article"""
@@ -53,7 +54,14 @@ def detect(self, doi, ref=None, extra_args=None):
5354
match = self.url_regex.match(url)
5455
if match:
5556
self.article_id = match.groups()[2]
56-
return {"article": self.article_id, "host": host}
57+
self.article_version = match.groups()[4]
58+
if not self.article_version:
59+
self.article_version = "1"
60+
return {
61+
"article": self.article_id,
62+
"host": host,
63+
"version": self.article_version,
64+
}
5765
else:
5866
return None
5967

@@ -62,9 +70,13 @@ def fetch(self, spec, output_dir, yield_output=False):
6270
article_id = spec["article"]
6371
host = spec["host"]
6472

65-
yield "Fetching Figshare article {}.\n".format(article_id)
73+
yield "Fetching Figshare article {} in version {}.\n".format(
74+
self.article_id, self.article_version
75+
)
6676
req = Request(
67-
"{}{}".format(host["api"], article_id),
77+
"{}{}/versions/{}".format(
78+
host["api"], self.article_id, self.article_version
79+
),
6880
headers={"accept": "application/json"},
6981
)
7082
resp = self.urlopen(req)
@@ -84,3 +96,8 @@ def fetch(self, spec, output_dir, yield_output=False):
8496
def content_id(self):
8597
"""The Figshare article ID"""
8698
return self.article_id
99+
100+
@property
101+
def content_version(self):
102+
"""The Figshare article ID"""
103+
return self.article_version

tests/unit/contentproviders/test_figshare.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import re
34
import pytest
45

56
from contextlib import contextmanager
@@ -10,6 +11,7 @@
1011
from zipfile import ZipFile
1112

1213
from repo2docker.contentproviders import Figshare
14+
from repo2docker.__main__ import make_r2d
1315

1416

1517
def test_content_id():
@@ -21,31 +23,49 @@ def test_content_id():
2123

2224

2325
test_fig = Figshare()
26+
test_fig.article_id = "123456"
27+
test_fig.article_version = "42"
28+
2429
test_dois_links = [
25-
("10.6084/m9.figshare.9782777", {"host": test_fig.hosts[0], "article": "9782777"}),
30+
(
31+
"10.6084/m9.figshare.9782777",
32+
{"host": test_fig.hosts[0], "article": "9782777", "version": "1"},
33+
),
2634
(
2735
"10.6084/m9.figshare.9782777.v1",
28-
{"host": test_fig.hosts[0], "article": "9782777"},
36+
{"host": test_fig.hosts[0], "article": "9782777", "version": "1"},
37+
),
38+
(
39+
"10.6084/m9.figshare.9782777.v2",
40+
{"host": test_fig.hosts[0], "article": "9782777", "version": "2"},
2941
),
3042
(
31-
"https://doi.org/10.6084/m9.figshare.9782777",
32-
{"host": test_fig.hosts[0], "article": "9782777"},
43+
"https://doi.org/10.6084/m9.figshare.9782777.v1",
44+
{"host": test_fig.hosts[0], "article": "9782777", "version": "1"},
45+
),
46+
(
47+
"https://doi.org/10.6084/m9.figshare.9782777.v3",
48+
{"host": test_fig.hosts[0], "article": "9782777", "version": "3"},
3349
),
3450
(
3551
"https://figshare.com/articles/title/97827771234",
36-
{"host": test_fig.hosts[0], "article": "97827771234"},
52+
{"host": test_fig.hosts[0], "article": "97827771234", "version": "1"},
3753
),
3854
(
3955
"https://figshare.com/articles/title/9782777/1",
40-
{"host": test_fig.hosts[0], "article": "9782777"},
56+
{"host": test_fig.hosts[0], "article": "9782777", "version": "1"},
57+
),
58+
(
59+
"https://figshare.com/articles/title/9782777/2",
60+
{"host": test_fig.hosts[0], "article": "9782777", "version": "2"},
4161
),
4262
(
4363
"https://figshare.com/articles/title/9782777/",
44-
{"host": test_fig.hosts[0], "article": "9782777"},
64+
{"host": test_fig.hosts[0], "article": "9782777", "version": "1"},
4565
),
4666
]
4767

48-
test_spec = {"host": test_fig.hosts[0], "article": "1234"}
68+
test_spec = {"host": test_fig.hosts[0], "article": "1234", "version": "42"}
4969

5070

5171
@pytest.mark.parametrize("test_input,expected", test_dois_links)
@@ -96,9 +116,8 @@ def mock_urlopen(self, req):
96116

97117
with patch.object(Figshare, "urlopen", new=mock_urlopen):
98118
with TemporaryDirectory() as d:
99-
fig = Figshare()
100119
output = []
101-
for l in fig.fetch(test_spec, d):
120+
for l in test_fig.fetch(test_spec, d):
102121
output.append(l)
103122

104123
unpacked_files = set(os.listdir(d))
@@ -137,10 +156,8 @@ def mock_urlopen(self, req):
137156

138157
with patch.object(Figshare, "urlopen", new=mock_urlopen):
139158
with TemporaryDirectory() as d:
140-
fig = Figshare()
141-
142159
output = []
143-
for l in fig.fetch(test_spec, d):
160+
for l in test_fig.fetch(test_spec, d):
144161
output.append(l)
145162

146163
unpacked_files = set(os.listdir(d))

0 commit comments

Comments
 (0)