Skip to content

Commit

Permalink
Merge pull request #441 from MJedr/fix-oricd
Browse files Browse the repository at this point in the history
move is_orcid from idutils to inspire-schemas
  • Loading branch information
MJedr authored Mar 20, 2023
2 parents 32d9168 + 31e2962 commit e2a9dfb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
23 changes: 22 additions & 1 deletion inspire_schemas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import six
from bleach.linkifier import LinkifyFilter
from bleach.sanitizer import Cleaner
from idutils import is_orcid
from idutils import is_isni
from inspire_utils.date import PartialDate
from isbn import ISBN
from jsonschema import Draft4Validator, RefResolver, draft4_format_checker
Expand Down Expand Up @@ -122,7 +122,12 @@
'econf',
]

ORCID_ISNI_RANGES = [
(15000000, 35000000),
(900000000000, 900100000000),
]

ORCID_URLS = ["http://orcid.org/", "https://orcid.org/"]
# list produced from https://arxiv.org/archive/
_NEW_CATEGORIES = {
'acc-phys': 'physics.acc-ph',
Expand Down Expand Up @@ -437,6 +442,22 @@ def func_wrapper(self, *args, **kwargs):
return func_wrapper


def is_orcid(val):
"""Test if argument is an ORCID ID.
See http://support.orcid.org/knowledgebase/
articles/116780-structure-of-the-orcid-identifier
"""
for orcid_url in ORCID_URLS:
if val.startswith(orcid_url):
val = val[len(orcid_url):]
break
val = val.replace("-", "").replace(" ", "")
if is_isni(val):
val = int(val[:-1], 10) # Remove check digit and convert to int.
return any(start <= val <= end for start, end in ORCID_ISNI_RANGES)
return False


def author_id_normalize_and_schema(uid, schema=None):
"""Detect and normalize an author UID schema.
Expand Down
19 changes: 18 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from inspire_schemas import errors, utils
from inspire_utils.query import ordered

from inspire_schemas.utils import normalize_collaboration_name
from inspire_schemas.utils import is_orcid, normalize_collaboration_name


def test_classify_field_returns_none_on_falsy_value():
Expand Down Expand Up @@ -1443,3 +1443,20 @@ def test_get_references_for_schema_returns_proper_schemas():
)
def test_normalize_collaboration_name(collaboration_name, expected_normalized_name):
assert normalize_collaboration_name(collaboration_name) == expected_normalized_name


def test_is_orcid():
orcids = [
"http://orcid.org/0000-0002-1825-0097",
"0000-0002-1825-0097",
"http://orcid.org/0000-0002-1825-0097",
"0000-0002-1694-233X",
"http://orcid.org/0000-0002-1694-233X",
"0009-0005-6000-7479",
"http://orcid.org/0009-0005-6000-7479",
"https://orcid.org/0009-0002-4767-9017",
"0009-0002-4767-9017",
"http://orcid.org/0009-0002-4767-9017",
]
for orcid in orcids:
assert is_orcid(orcid)

0 comments on commit e2a9dfb

Please sign in to comment.