Coverage for src/api/indexapi_v2.py: 100%
35 statements
« prev ^ index » next coverage.py v7.10.0, created at 2026-11-03 10:27 +0000
« prev ^ index » next coverage.py v7.10.0, created at 2026-11-03 10:27 +0000
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023, Silvio Peroni <essepuntato@gmail.com>
4#
5# Permission to use, copy, modify, and/or distribute this software for any purpose
6# with or without fee is hereby granted, provided that the above copyright notice
7# 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, INDIRECT,
12# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
13# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15# SOFTWARE.
17__author__ = "Arcangelo Massari & Ivan Heibi"
19import re
21from indexapi_common import (
22 lower, # noqa: F401 - used by ramose via getattr
23 count_unique_cits, # noqa: F401 - used by ramose via getattr
24 count_groups, # noqa: F401 - used by ramose via getattr
25 oci_search, # noqa: F401 - used by ramose via getattr
26 literal_search,
27 unique_citations,
28 citation_row,
29 get_id_val,
30)
33def cited_search(s):
34 return (__br_search(s, "cited_br"),)
37def citing_search(s):
38 return (__br_search(s, "citing_br"),)
41def issn_value(s):
42 value = re.sub("[^X0-9]", "", s.removeprefix("issn:").upper())
43 return (f"{value[:4]}-{value[4:8]}",)
46def isbn_value(s):
47 value = re.sub("[^X0-9]", "", s.removeprefix("isbn:").upper())
48 return (value,)
51def citations_info(res):
52 f_res = [
53 ["oci", "citing", "cited", "creation", "timespan", "journal_sc", "author_sc"]
54 ]
55 for citation in unique_citations(res):
56 f_res.append(
57 citation_row(
58 citation,
59 __get_all_pids(citation["citing_br"], citation["citing_ids"]),
60 __get_all_pids(citation["cited_br"], citation["cited_ids"]),
61 )
62 )
63 return f_res, True
66def venue_count(res):
67 header = res[0]
68 count = res[1][header.index("count")][1] if len(res) > 1 else "0"
69 return [["count"], [count]], True
72def __br_search(s, variable):
73 scheme, value = s.split(":", 1)
74 if scheme == "omid":
75 return f"VALUES ?{variable} {{ <https://w3id.org/oc/meta/{value}> }}"
76 if scheme == "issn":
77 value = issn_value(s)[0]
78 elif scheme == "isbn":
79 value = isbn_value(s)[0]
80 if scheme in {"issn", "isbn"}:
81 return (
82 literal_search(value, variable)
83 + f" ?resolved_identifier datacite:usesIdentifierScheme datacite:{scheme} ."
84 )
85 return literal_search(value, variable)
88def __get_all_pids(br, ids):
89 return " ".join(["omid:br/" + get_id_val(br), *ids.split(" __ ")])