Skip to content

Commit 4541efb

Browse files
committed
style: fix flake8 issues
1 parent 46290c0 commit 4541efb

File tree

2 files changed

+64
-72
lines changed

2 files changed

+64
-72
lines changed

src/doi/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Optional
66

77

8-
__version__ = '0.2.0'
8+
__version__ = "0.2.0"
99
logger = logging.getLogger("doi") # type: logging.Logger
1010

1111

@@ -24,9 +24,9 @@ def pdf_to_doi(filepath: str, maxlines: Optional[int] = None) -> Optional[str]:
2424
if maxlines is None:
2525
maxlines = sys.maxsize
2626

27-
with open(filepath, 'rb') as fd:
27+
with open(filepath, "rb") as fd:
2828
for j, line in enumerate(fd):
29-
doi = find_doi_in_text(line.decode('ascii', errors='ignore'))
29+
doi = find_doi_in_text(line.decode("ascii", errors="ignore"))
3030
if doi:
3131
return doi
3232
if j > maxlines:
@@ -48,16 +48,16 @@ def validate_doi(doi: str) -> Optional[str]:
4848
import urllib.parse
4949
import json
5050
url = "https://doi.org/api/handles/{doi}".format(doi=doi)
51-
logger.debug('handle url %s', url)
51+
logger.debug("handle url %s", url)
5252
request = urllib.request.Request(url)
5353

5454
try:
5555
result = json.loads(urllib.request.urlopen(request).read().decode())
5656
except HTTPError:
57-
raise ValueError('HTTP 404: DOI not found')
57+
raise ValueError("HTTP 404: DOI not found")
5858
else:
59-
urls = [v['data']['value']
60-
for v in result['values'] if v.get('type') == 'URL']
59+
urls = [v["data"]["value"]
60+
for v in result["values"] if v.get("type") == "URL"]
6161
return urls[0] if urls else None
6262

6363

@@ -68,12 +68,12 @@ def get_clean_doi(doi: str) -> str:
6868
:param doi: String containing a DOI.
6969
:returns: The extracted DOI.
7070
"""
71-
doi = re.sub(r'%2F', '/', doi)
71+
doi = re.sub(r"%2F", "/", doi)
7272
# For pdfs
73-
doi = re.sub(r'\)>', ' ', doi)
74-
doi = re.sub(r'\)/S/URI', ' ', doi)
75-
doi = re.sub(r'(/abstract)', '', doi)
76-
doi = re.sub(r'\)$', '', doi)
73+
doi = re.sub(r"\)>", " ", doi)
74+
doi = re.sub(r"\)/S/URI", " ", doi)
75+
doi = re.sub(r"(/abstract)", "", doi)
76+
doi = re.sub(r"\)$", "", doi)
7777
return doi
7878

7979

@@ -87,11 +87,11 @@ def find_doi_in_text(text: str) -> Optional[str]:
8787
forbidden_doi_characters = r'"\s%$^\'<>@,;:#?&'
8888
# Sometimes it is in the javascript defined
8989
var_doi = re.compile(
90-
r'doi(.org)?'
91-
r'\s*(=|:|/|\()\s*'
92-
r'("|\')?'
93-
r'(?P<doi>[^{fc}]+)'
94-
r'("|\'|\))?'
90+
r"doi(.org)?"
91+
r"\s*(=|:|/|\()\s*"
92+
r"(\"|')?"
93+
r"(?P<doi>[^{fc}]+)"
94+
r"(\"|'|\))?"
9595
.format(
9696
fc=forbidden_doi_characters
9797
), re.I
@@ -102,7 +102,7 @@ def find_doi_in_text(text: str) -> Optional[str]:
102102
try:
103103
m = next(miter)
104104
if m:
105-
doi = m.group('doi')
105+
doi = m.group("doi")
106106
return get_clean_doi(doi)
107107
except StopIteration:
108108
pass
@@ -119,8 +119,8 @@ def get_real_url_from_doi(doi: str) -> Optional[str]:
119119
if url is None:
120120
return url
121121

122-
m = re.match(r'.*linkinghub\.elsevier.*/pii/([A-Z0-9]+).*', url, re.I)
122+
m = re.match(r".*linkinghub\.elsevier.*/pii/([A-Z0-9]+).*", url, re.I)
123123
if m:
124-
return ('https://www.sciencedirect.com/science/article/abs/pii/{pii}'
124+
return ("https://www.sciencedirect.com/science/article/abs/pii/{pii}"
125125
.format(pii=m.group(1)))
126126
return url

tests/test_doi.py

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,85 @@
1-
"""Tests for `doi` package."""
2-
31
import os
4-
from pkg_resources import parse_version
52

63
import pytest
74

85
from doi import (
9-
validate_doi, find_doi_in_text, __version__, pdf_to_doi,
6+
validate_doi, find_doi_in_text, pdf_to_doi,
107
get_real_url_from_doi
118
)
129

1310

14-
def test_valid_version() -> None:
15-
"""Check that the package defines a valid __version__"""
16-
assert parse_version(__version__) >= parse_version("0.1.0")
17-
18-
1911
@pytest.mark.net
2012
def test_validate_doi() -> None:
2113
data = [
22-
('10.1063/1.5081715',
23-
'http://aip.scitation.org/doi/10.1063/1.5081715'),
24-
('10.1007%2FBF01451751',
25-
'http://link.springer.com/10.1007/BF01451751'),
26-
('10.1103/PhysRevLett.49.57',
27-
'https://link.aps.org/doi/10.1103/PhysRevLett.49.57'),
28-
('10.1080/14786442408634457',
29-
'https://www.tandfonline.com/doi/full/10.1080/14786442408634457'),
30-
('10.1021/jp003647e', 'https://pubs.acs.org/doi/10.1021/jp003647e'),
31-
('10.1016/S0009-2614(97)04014-1',
32-
'https://linkinghub.elsevier.com/retrieve/pii/S0009261497040141'),
14+
("10.1063/1.5081715",
15+
"http://aip.scitation.org/doi/10.1063/1.5081715"),
16+
("10.1007%2FBF01451751",
17+
"http://link.springer.com/10.1007/BF01451751"),
18+
("10.1103/PhysRevLett.49.57",
19+
"https://link.aps.org/doi/10.1103/PhysRevLett.49.57"),
20+
("10.1080/14786442408634457",
21+
"https://www.tandfonline.com/doi/full/10.1080/14786442408634457"),
22+
("10.1021/jp003647e", "https://pubs.acs.org/doi/10.1021/jp003647e"),
23+
("10.1016/S0009-2614(97)04014-1",
24+
"https://linkinghub.elsevier.com/retrieve/pii/S0009261497040141"),
3325
]
3426
for doi, url in data:
3527
assert url == validate_doi(doi)
3628

37-
for doi in ['', 'asdf']:
29+
for doi in ["", "asdf"]:
3830
try:
3931
validate_doi(doi)
4032
except ValueError as e:
41-
assert str(e) == 'HTTP 404: DOI not found'
33+
assert str(e) == "HTTP 404: DOI not found"
4234

4335

4436
@pytest.mark.net
4537
def test_get_real_url_from_doi() -> None:
4638
data = [
47-
('10.1016/S0009-2614(97)04014-1',
48-
'https://www.sciencedirect.com/science/'
49-
'article/abs/pii/S0009261497040141'),
39+
("10.1016/S0009-2614(97)04014-1",
40+
"https://www.sciencedirect.com/science/"
41+
"article/abs/pii/S0009261497040141"),
5042
]
5143
for doi, url in data:
5244
assert url == get_real_url_from_doi(doi)
5345

5446

5547
def test_find_doi_in_line() -> None:
5648
test_data = [
57-
('http://dx.doi.org/10.1063/1.881498', '10.1063/1.881498'),
58-
('http://dx.doi.org/10.1063%2F1.881498', '10.1063/1.881498'),
59-
(2*'qer '+'var doi = "12345/12345.3"', '12345/12345.3'),
60-
(2*'qer '+"var doi = '12345/12345.3';fas", '12345/12345.3'),
61-
(2*'qer '+"var DoI = 12345%2F12345.3", '12345/12345.3'),
62-
(2*'qer '+"var DoI : 12345%2F12345.3", '12345/12345.3'),
63-
('http://scitation.org/doi/10.1063/1.881498', '10.1063/1.881498'),
64-
('org/doi(10.1063/1.881498)', '10.1063/1.881498'),
65-
('/scitation.org/doi/10.1063/1.881498?234saf=34', '10.1063/1.881498'),
66-
('/scitation.org/doi/10.1063/1.88149 8?234saf=34', '10.1063/1.88149'),
67-
('/scitation.org/doi/10.1063/1.uniau12?as=234',
68-
'10.1063/1.uniau12'),
69-
('https://doi.org/10.1093/analys/anw053', '10.1093/analys/anw053'),
70-
('http://.scitation.org/doi/10.1063/1.mart(88)1498?asdfwer',
71-
'10.1063/1.mart(88)1498'),
72-
('@ibook{doi:10.1002/9780470125915.ch2,', '10.1002/9780470125915.ch2'),
49+
("http://dx.doi.org/10.1063/1.881498", "10.1063/1.881498"),
50+
("http://dx.doi.org/10.1063%2F1.881498", "10.1063/1.881498"),
51+
(2 * "qer " + "var doi = '12345/12345.3'", "12345/12345.3"),
52+
(2 * "qer " + "var doi = '12345/12345.3';fas", "12345/12345.3"),
53+
(2 * "qer " + "var DoI = 12345%2F12345.3", "12345/12345.3"),
54+
(2 * "qer " + "var DoI : 12345%2F12345.3", "12345/12345.3"),
55+
("http://scitation.org/doi/10.1063/1.881498", "10.1063/1.881498"),
56+
("org/doi(10.1063/1.881498)", "10.1063/1.881498"),
57+
("/scitation.org/doi/10.1063/1.881498?234saf=34", "10.1063/1.881498"),
58+
("/scitation.org/doi/10.1063/1.88149 8?234saf=34", "10.1063/1.88149"),
59+
("/scitation.org/doi/10.1063/1.uniau12?as=234",
60+
"10.1063/1.uniau12"),
61+
("https://doi.org/10.1093/analys/anw053", "10.1093/analys/anw053"),
62+
("http://.scitation.org/doi/10.1063/1.mart(88)1498?asdfwer",
63+
"10.1063/1.mart(88)1498"),
64+
("@ibook{doi:10.1002/9780470125915.ch2,", "10.1002/9780470125915.ch2"),
7365
('<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements'
7466
'.1/"><dc:format>application/pdf</dc:format><dc:identifier>'
75-
'doi:10.1063/1.5079474</dc:identifier></rdf:Description>',
76-
'10.1063/1.5079474'),
77-
('<(DOI:10.1002/9780470915.CH2)/S/URI,', '10.1002/9780470915.CH2'),
78-
('URL<(DOI:10.1002/9780470125915.CH2,', '10.1002/9780470125915.CH2'),
79-
(r'A<</S/URI/URI(https://doi.org/10.1016/j.comptc.2018.10.004)>>/'
80-
r'Border[0 0 0]/M(D:20181022082356+0530)/Rect[147.40158 594.36926'
81-
r'347.24957 605.36926]/Subtype/Link/Type/A',
82-
'10.1016/j.comptc.2018.10.004'),
83-
('doi(10.1038/s41535-018-0103-6;)', '10.1038/s41535-018-0103-6'),
67+
"doi:10.1063/1.5079474</dc:identifier></rdf:Description>",
68+
"10.1063/1.5079474"),
69+
("<(DOI:10.1002/9780470915.CH2)/S/URI,", "10.1002/9780470915.CH2"),
70+
("URL<(DOI:10.1002/9780470125915.CH2,", "10.1002/9780470125915.CH2"),
71+
(r"A<</S/URI/URI(https://doi.org/10.1016/j.comptc.2018.10.004)>>/"
72+
r"Border[0 0 0]/M(D:20181022082356+0530)/Rect[147.40158 594.36926"
73+
r"347.24957 605.36926]/Subtype/Link/Type/A",
74+
"10.1016/j.comptc.2018.10.004"),
75+
("doi(10.1038/s41535-018-0103-6;)", "10.1038/s41535-018-0103-6"),
8476
]
8577
for url, doi in test_data:
8678
assert find_doi_in_text(url) == doi
8779

8880

8981
def test_doi_from_pdf() -> None:
90-
f = os.path.join(os.path.dirname(__file__), 'resources', 'doc.pdf')
82+
f = os.path.join(os.path.dirname(__file__), "resources", "doc.pdf")
9183

9284
assert os.path.exists(f)
93-
assert pdf_to_doi(f) == '10.1103/PhysRevLett.50.1998'
85+
assert pdf_to_doi(f) == "10.1103/PhysRevLett.50.1998"

0 commit comments

Comments
 (0)