Coverage for oc_validator / id_syntax.py: 100%
59 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-30 15:46 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-30 15:46 +0000
1# ISC License
2#
3# Copyright (c) 2023-2026, Elia Rizzetto, Silvio Peroni
4#
5# Permission to use, copy, modify, and/or distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15# PERFORMANCE OF THIS SOFTWARE.
17from oc_ds_converter.oc_idmanager import doi, isbn, issn, orcid, pmcid, pmid, ror, url, viaf, wikidata, wikipedia, openalex, crossref, jid, arxiv
18from re import match
20class IdSyntax:
21 """
22 Validates the external syntax of identifiers by delegating to the
23 appropriate ``oc_ds_converter`` ID manager for each recognised scheme.
24 """
26 def __init__(self) -> None:
27 """
28 Initialise ID managers for all supported identifier schemes.
30 :rtype: None
31 """
32 self.doi_mngr = doi.DOIManager()
33 self.isbn_mngr = isbn.ISBNManager()
34 self.issn_mngr = issn.ISSNManager()
35 self.orcid_mngr = orcid.ORCIDManager()
36 self.pmcid_mngr = pmcid.PMCIDManager()
37 self.pmid_mngr = pmid.PMIDManager()
38 self.ror_mngr = ror.RORManager()
39 self.url_mngr = url.URLManager()
40 self.viaf_mngr = viaf.ViafManager()
41 self.wikidata_mngr = wikidata.WikidataManager()
42 self.wikipedia_mngr = wikipedia.WikipediaManager()
43 self.openalex_mngr = openalex.OpenAlexManager()
44 self.crossref_mngr = crossref.CrossrefManager()
45 self.jid_mngr = jid.JIDManager()
46 self.arxiv_mngr = arxiv.ArXivManager()
48 def check_id_syntax(self, id: str) -> bool:
49 """
50 Validate the external syntax of an identifier according to its scheme.
52 Dispatches to the appropriate manager's ``syntax_ok()`` method based on
53 the identifier prefix. ``temp:`` and ``local:`` identifiers always pass.
55 :param id: The identifier string, including its prefix (e.g. ``"doi:10.1234/abc"``).
56 :type id: str
57 :return: ``True`` if the identifier's syntax is valid, ``False`` otherwise.
58 :rtype: bool
59 """
60 oc_prefix = id[:(id.index(':') + 1)]
62 if oc_prefix == 'omid:':
63 return bool(match(r'^(?:br|ra)\/06[1-9]*0[1-9][0-9]*$', id.strip(oc_prefix))) # only supports br and ra entities
64 if oc_prefix == 'doi:':
65 vldt = self.doi_mngr
66 elif oc_prefix == 'isbn:':
67 vldt = self.isbn_mngr
68 elif oc_prefix == 'issn:':
69 vldt = self.issn_mngr
70 elif oc_prefix == 'orcid:':
71 vldt = self.orcid_mngr
72 elif oc_prefix == 'pmcid:':
73 vldt = self.pmcid_mngr
74 elif oc_prefix == 'pmid:':
75 vldt = self.pmid_mngr
76 elif oc_prefix == 'ror:':
77 vldt = self.ror_mngr
78 elif oc_prefix == 'url:':
79 vldt = self.url_mngr
80 elif oc_prefix == 'viaf:':
81 vldt = self.viaf_mngr
82 elif oc_prefix == 'wikidata:':
83 vldt = self.wikidata_mngr
84 elif oc_prefix == 'wikipedia:':
85 vldt = self.wikipedia_mngr
86 elif oc_prefix == 'openalex:':
87 vldt = self.openalex_mngr
88 elif oc_prefix == 'crossref:':
89 vldt = self.crossref_mngr
90 elif oc_prefix == 'jid:':
91 vldt = self.jid_mngr
92 elif oc_prefix == 'arxiv:':
93 vldt = self.arxiv_mngr
94 elif oc_prefix == 'temp:':
95 return True
96 elif oc_prefix == 'local:':
97 return True
98 else:
99 return False
100 return vldt.syntax_ok(id)