Skip to content

Commit

Permalink
global: add support for Software Heritage identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
anlambert authored and slint committed Feb 26, 2020
1 parent d93ba65 commit efea022
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Authors
- Adrian Pawel Baran
- Alan Rubin
- Alexander Ioannidis
- Antoine Lambert
- Bruno Marmol
- Jiri Kuncar
- Lars Holm Nielsen
Expand Down
17 changes: 16 additions & 1 deletion idutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@
ascl_regexp = re.compile("^ascl:[0-9]{4}\.[0-9]{3,4}$", flags=re.I)
"""ASCL regular expression."""

swh_regexp = re.compile(
"swh:1:(cnt|dir|rel|rev|snp):[0-9a-f]{40}(;origin=\S+)?$"
)
"""Matches Software Heritage identifiers."""


def _convert_x_to_10(x):
"""Convert char to int with X being converted to 10."""
Expand Down Expand Up @@ -331,7 +336,7 @@ def is_handle(val):
Note, DOIs are also handles, and handle are very generic so they will also
match e.g. any URL your parse.
"""
return handle_regexp.match(val)
return handle_regexp.match(val) and not swh_regexp.match(val)


def is_ean8(val):
Expand Down Expand Up @@ -534,6 +539,14 @@ def is_ascl(val):
return ascl_regexp.match(val)


def is_swh(val):
"""Test if argument is a Software Heritage identifier.
https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html
"""
return swh_regexp.match(val)


PID_SCHEMES = [
('doi', is_doi),
('ark', is_ark),
Expand Down Expand Up @@ -563,6 +576,7 @@ def is_ascl(val):
('uniprot', is_uniprot),
('refseq', is_refseq),
('genome', is_genome),
('swh', is_swh),
]
"""Definition of scheme name and associated test function.
Expand Down Expand Up @@ -740,6 +754,7 @@ def normalize_pid(val, scheme):
'refseq': u'{scheme}://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?val={pid}',
'genome': u'{scheme}://www.ncbi.nlm.nih.gov/assembly/{pid}',
'hal': u'{scheme}://hal.archives-ouvertes.fr/{pid}',
'swh': u'{scheme}://archive.softwareheritage.org/{pid}',
}
"""URL generation configuration for the supported PID providers."""

Expand Down
29 changes: 28 additions & 1 deletion tests/test_idutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,34 @@
'http://hal.archives-ouvertes.fr/inserm-13102590'),
('mem_13102590', ['hal', ], 'mem_13102590',
'http://hal.archives-ouvertes.fr/mem_13102590'),
('ascl:1908.011', ['ascl', ], 'ascl:1908.011', 'http://ascl.net/1908.011')
('ascl:1908.011', ['ascl', ], 'ascl:1908.011', 'http://ascl.net/1908.011'),
('swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2', ['swh', ],
'swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2',
('http://archive.softwareheritage.org/'
'swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2')),
('swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505', ['swh', ],
'swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505',
('http://archive.softwareheritage.org/'
'swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505')),
('swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d', ['swh', ],
'swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d',
('http://archive.softwareheritage.org/'
'swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d')),
('swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f', ['swh', ],
'swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f',
('http://archive.softwareheritage.org/'
'swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f')),
('swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453', ['swh', ],
'swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453',
('http://archive.softwareheritage.org/'
'swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453')),
(('swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505'
';origin=https://github.com/user/repo'), ['swh', ],
('swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505'
';origin=https://github.com/user/repo'),
('http://archive.softwareheritage.org/'
'swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505'
';origin=https://github.com/user/repo')),
]


Expand Down

0 comments on commit efea022

Please sign in to comment.