invenio-vocabularies 6.6.0__py2.py3-none-any.whl → 6.8.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of invenio-vocabularies might be problematic. Click here for more details.
- invenio_vocabularies/__init__.py +1 -1
- invenio_vocabularies/assets/semantic-ui/js/invenio_vocabularies/src/contrib/forms/Funding/FundingModal.js +3 -27
- invenio_vocabularies/cli.py +2 -0
- invenio_vocabularies/config.py +43 -1
- invenio_vocabularies/contrib/affiliations/config.py +21 -10
- invenio_vocabularies/contrib/affiliations/datastreams.py +103 -1
- invenio_vocabularies/contrib/awards/datastreams.py +7 -0
- invenio_vocabularies/contrib/awards/jsonschemas/awards/award-v1.0.0.json +9 -0
- invenio_vocabularies/contrib/awards/mappings/os-v1/awards/award-v1.0.0.json +22 -1
- invenio_vocabularies/contrib/awards/mappings/os-v2/awards/award-v1.0.0.json +22 -1
- invenio_vocabularies/contrib/awards/mappings/v7/awards/award-v1.0.0.json +22 -1
- invenio_vocabularies/contrib/awards/schema.py +9 -3
- invenio_vocabularies/contrib/funders/config.py +19 -12
- invenio_vocabularies/contrib/names/config.py +13 -10
- invenio_vocabularies/contrib/names/datastreams.py +182 -57
- invenio_vocabularies/contrib/names/mappings/os-v1/names/name-v2.0.0.json +11 -0
- invenio_vocabularies/contrib/names/mappings/os-v2/names/name-v2.0.0.json +11 -0
- invenio_vocabularies/contrib/names/names.py +1 -1
- invenio_vocabularies/contrib/names/schema.py +10 -2
- invenio_vocabularies/contrib/subjects/bodc/__init__.py +9 -0
- invenio_vocabularies/contrib/subjects/bodc/datastreams.py +111 -0
- invenio_vocabularies/contrib/subjects/config.py +19 -5
- invenio_vocabularies/contrib/subjects/datastreams.py +4 -2
- invenio_vocabularies/contrib/subjects/euroscivoc/datastreams.py +56 -126
- invenio_vocabularies/contrib/subjects/gemet/__init__.py +9 -0
- invenio_vocabularies/contrib/subjects/gemet/datastreams.py +140 -0
- invenio_vocabularies/contrib/subjects/jsonschemas/subjects/subject-v1.0.0.json +13 -2
- invenio_vocabularies/contrib/subjects/schema.py +18 -3
- invenio_vocabularies/datastreams/datastreams.py +18 -7
- invenio_vocabularies/datastreams/factories.py +3 -1
- invenio_vocabularies/datastreams/readers.py +99 -9
- invenio_vocabularies/datastreams/transformers.py +67 -0
- invenio_vocabularies/datastreams/writers.py +6 -2
- invenio_vocabularies/factories.py +56 -0
- invenio_vocabularies/fixtures.py +2 -0
- invenio_vocabularies/records/jsonschemas/vocabularies/definitions-v1.0.0.json +9 -0
- invenio_vocabularies/services/config.py +1 -7
- invenio_vocabularies/services/querystr.py +5 -0
- invenio_vocabularies/services/tasks.py +2 -0
- {invenio_vocabularies-6.6.0.dist-info → invenio_vocabularies-6.8.0.dist-info}/METADATA +28 -2
- {invenio_vocabularies-6.6.0.dist-info → invenio_vocabularies-6.8.0.dist-info}/RECORD +46 -42
- {invenio_vocabularies-6.6.0.dist-info → invenio_vocabularies-6.8.0.dist-info}/AUTHORS.rst +0 -0
- {invenio_vocabularies-6.6.0.dist-info → invenio_vocabularies-6.8.0.dist-info}/LICENSE +0 -0
- {invenio_vocabularies-6.6.0.dist-info → invenio_vocabularies-6.8.0.dist-info}/WHEEL +0 -0
- {invenio_vocabularies-6.6.0.dist-info → invenio_vocabularies-6.8.0.dist-info}/entry_points.txt +0 -0
- {invenio_vocabularies-6.6.0.dist-info → invenio_vocabularies-6.8.0.dist-info}/top_level.txt +0 -0
|
@@ -13,12 +13,14 @@ import io
|
|
|
13
13
|
import tarfile
|
|
14
14
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
15
15
|
from datetime import timedelta
|
|
16
|
+
from itertools import islice
|
|
17
|
+
from pathlib import Path
|
|
16
18
|
|
|
17
19
|
import arrow
|
|
18
20
|
import regex as re
|
|
19
21
|
from flask import current_app
|
|
20
22
|
from invenio_access.permissions import system_identity
|
|
21
|
-
from
|
|
23
|
+
from werkzeug.utils import cached_property
|
|
22
24
|
|
|
23
25
|
from invenio_vocabularies.contrib.names.s3client import S3OrcidClient
|
|
24
26
|
|
|
@@ -47,10 +49,11 @@ class OrcidDataSyncReader(BaseReader):
|
|
|
47
49
|
suffix = orcid_to_sync[-3:]
|
|
48
50
|
key = f"{suffix}/{orcid_to_sync}.xml"
|
|
49
51
|
try:
|
|
52
|
+
# Potential improvement: use the a XML jax parser to avoid loading the whole file in memory
|
|
53
|
+
# and choose the sections we need to read (probably the summary)
|
|
50
54
|
return self.s3_client.read_file(f"s3://{bucket}/{key}")
|
|
51
|
-
except Exception
|
|
52
|
-
|
|
53
|
-
return None
|
|
55
|
+
except Exception:
|
|
56
|
+
current_app.logger.exception("Failed to fetch ORCiD record.")
|
|
54
57
|
|
|
55
58
|
def _process_lambda_file(self, fileobj):
|
|
56
59
|
"""Process the ORCiD lambda file and returns a list of ORCiDs to sync.
|
|
@@ -67,42 +70,54 @@ class OrcidDataSyncReader(BaseReader):
|
|
|
67
70
|
if self.since:
|
|
68
71
|
time_shift = self.since
|
|
69
72
|
last_sync = arrow.now() - timedelta(**time_shift)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
73
|
+
try:
|
|
74
|
+
content = io.TextIOWrapper(fileobj, encoding="utf-8")
|
|
75
|
+
csv_reader = csv.DictReader(content)
|
|
76
|
+
|
|
77
|
+
for row in csv_reader: # Skip the header line
|
|
78
|
+
orcid = row["orcid"]
|
|
79
|
+
|
|
80
|
+
# Lambda file is ordered by last modified date
|
|
81
|
+
last_modified_str = row["last_modified"]
|
|
82
|
+
try:
|
|
83
|
+
last_modified_date = arrow.get(last_modified_str, date_format)
|
|
84
|
+
except arrow.parser.ParserError:
|
|
85
|
+
last_modified_date = arrow.get(
|
|
86
|
+
last_modified_str, date_format_no_millis
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
if last_modified_date < last_sync:
|
|
90
|
+
break
|
|
91
|
+
yield orcid
|
|
92
|
+
finally:
|
|
93
|
+
fileobj.close()
|
|
88
94
|
|
|
89
95
|
def _iter(self, orcids):
|
|
90
96
|
"""Iterates over the ORCiD records yielding each one."""
|
|
91
97
|
with ThreadPoolExecutor(
|
|
92
98
|
max_workers=current_app.config["VOCABULARIES_ORCID_SYNC_MAX_WORKERS"]
|
|
93
99
|
) as executor:
|
|
94
|
-
futures
|
|
95
|
-
|
|
100
|
+
# futures is a dictionary where the key is the ORCID value and the item is the Future object
|
|
101
|
+
futures = {
|
|
102
|
+
orcid: executor.submit(
|
|
96
103
|
self._fetch_orcid_data,
|
|
97
104
|
orcid,
|
|
98
105
|
current_app.config["VOCABULARIES_ORCID_SUMMARIES_BUCKET"],
|
|
99
106
|
)
|
|
100
107
|
for orcid in orcids
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
for orcid in list(futures.keys()):
|
|
111
|
+
try:
|
|
112
|
+
result = futures[orcid].result()
|
|
113
|
+
if result:
|
|
114
|
+
yield result
|
|
115
|
+
finally:
|
|
116
|
+
# Explicitly release memory, as we don't need the future anymore.
|
|
117
|
+
# This is mostly required because as long as we keep a reference to the future
|
|
118
|
+
# (in the above futures dict), the garbage collector won't collect it
|
|
119
|
+
# and it will keep the memory allocated.
|
|
120
|
+
del futures[orcid]
|
|
106
121
|
|
|
107
122
|
def read(self, item=None, *args, **kwargs):
|
|
108
123
|
"""Streams the ORCiD lambda file, process it to get the ORCiDS to sync and yields it's data."""
|
|
@@ -111,7 +126,6 @@ class OrcidDataSyncReader(BaseReader):
|
|
|
111
126
|
"s3://orcid-lambda-file/last_modified.csv.tar"
|
|
112
127
|
)
|
|
113
128
|
|
|
114
|
-
orcids_to_sync = []
|
|
115
129
|
# Opens tar file and process it
|
|
116
130
|
with tarfile.open(fileobj=io.BytesIO(tar_content)) as tar:
|
|
117
131
|
# Iterate over each member (file or directory) in the tar file
|
|
@@ -119,10 +133,24 @@ class OrcidDataSyncReader(BaseReader):
|
|
|
119
133
|
# Extract the file
|
|
120
134
|
extracted_file = tar.extractfile(member)
|
|
121
135
|
if extracted_file:
|
|
136
|
+
current_app.logger.info(f"[ORCID Reader] Processing lambda file...")
|
|
122
137
|
# Process the file and get the ORCiDs to sync
|
|
123
|
-
orcids_to_sync
|
|
138
|
+
orcids_to_sync = set(self._process_lambda_file(extracted_file))
|
|
139
|
+
|
|
140
|
+
# Close the file explicitly after processing
|
|
141
|
+
extracted_file.close()
|
|
142
|
+
|
|
143
|
+
# Process ORCIDs in smaller batches
|
|
144
|
+
for orcid_batch in self._chunked_iter(
|
|
145
|
+
orcids_to_sync, batch_size=100
|
|
146
|
+
):
|
|
147
|
+
yield from self._iter(orcid_batch)
|
|
124
148
|
|
|
125
|
-
|
|
149
|
+
def _chunked_iter(self, iterable, batch_size):
|
|
150
|
+
"""Yield successive chunks of a given size."""
|
|
151
|
+
it = iter(iterable)
|
|
152
|
+
while chunk := list(islice(it, batch_size)):
|
|
153
|
+
yield chunk
|
|
126
154
|
|
|
127
155
|
|
|
128
156
|
class OrcidHTTPReader(SimpleHTTPReader):
|
|
@@ -139,24 +167,75 @@ class OrcidHTTPReader(SimpleHTTPReader):
|
|
|
139
167
|
|
|
140
168
|
|
|
141
169
|
DEFAULT_NAMES_EXCLUDE_REGEX = r"[\p{P}\p{S}\p{Nd}\p{No}\p{Emoji}--,.()\-']"
|
|
142
|
-
"""Regex to filter out names with
|
|
170
|
+
"""Regex to filter out names with punctuation, symbols, numbers and emojis."""
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class OrcidOrgToAffiliationMapper:
|
|
174
|
+
"""Default ORCiD Org ID to affiliation ID mapper."""
|
|
175
|
+
|
|
176
|
+
def __init__(self, org_ids_mapping=None, org_ids_mapping_file=None):
|
|
177
|
+
"""Constructor."""
|
|
178
|
+
self._org_ids_mapping = org_ids_mapping
|
|
179
|
+
self._org_ids_mapping_file = org_ids_mapping_file
|
|
180
|
+
|
|
181
|
+
@cached_property
|
|
182
|
+
def org_ids_mapping(self):
|
|
183
|
+
"""Mapping of ORCiD org IDs to affiliation IDs."""
|
|
184
|
+
org_ids_mapping_file = self._org_ids_mapping_file or current_app.config.get(
|
|
185
|
+
"VOCABULARIES_ORCID_ORG_IDS_MAPPING_PATH"
|
|
186
|
+
)
|
|
187
|
+
if org_ids_mapping_file:
|
|
188
|
+
org_ids_mapping_file = Path(org_ids_mapping_file)
|
|
189
|
+
# If the path is relative, prepend the instance path
|
|
190
|
+
if not org_ids_mapping_file.is_absolute():
|
|
191
|
+
org_ids_mapping_file = (
|
|
192
|
+
Path(current_app.instance_path) / org_ids_mapping_file
|
|
193
|
+
)
|
|
194
|
+
with open(org_ids_mapping_file) as fin:
|
|
195
|
+
result = {}
|
|
196
|
+
reader = csv.reader(fin)
|
|
197
|
+
|
|
198
|
+
# Check if the first row is a header
|
|
199
|
+
org_scheme, org_id, aff_id = next(reader)
|
|
200
|
+
if org_scheme.lower() != "org_scheme":
|
|
201
|
+
result[(org_scheme, org_id)] = aff_id
|
|
202
|
+
|
|
203
|
+
for org_scheme, org_id, aff_id in reader:
|
|
204
|
+
result[(org_scheme, org_id)] = aff_id
|
|
205
|
+
|
|
206
|
+
return result
|
|
207
|
+
|
|
208
|
+
return self._org_ids_mapping or {}
|
|
209
|
+
|
|
210
|
+
def __call__(self, org_scheme, org_id):
|
|
211
|
+
"""Map an ORCiD org ID to an affiliation ID."""
|
|
212
|
+
# By default we know that ROR IDs are linkable
|
|
213
|
+
if org_scheme == "ROR":
|
|
214
|
+
return org_id.split("/")[-1]
|
|
215
|
+
# Otherwise see if we have a mapping from other schemes to an affiliation ID
|
|
216
|
+
return self.org_ids_mapping.get((org_scheme, org_id))
|
|
143
217
|
|
|
144
218
|
|
|
145
219
|
class OrcidTransformer(BaseTransformer):
|
|
146
220
|
"""Transforms an ORCiD record into a names record."""
|
|
147
221
|
|
|
148
222
|
def __init__(
|
|
149
|
-
self,
|
|
223
|
+
self,
|
|
224
|
+
*args,
|
|
225
|
+
names_exclude_regex=DEFAULT_NAMES_EXCLUDE_REGEX,
|
|
226
|
+
org_id_to_affiliation_id_func=None,
|
|
227
|
+
**kwargs,
|
|
150
228
|
) -> None:
|
|
151
229
|
"""Constructor."""
|
|
152
230
|
self._names_exclude_regex = names_exclude_regex
|
|
231
|
+
self._org_id_to_affiliation_id_func = (
|
|
232
|
+
org_id_to_affiliation_id_func or OrcidOrgToAffiliationMapper()
|
|
233
|
+
)
|
|
153
234
|
super().__init__()
|
|
154
235
|
|
|
155
|
-
def
|
|
156
|
-
"""
|
|
157
|
-
|
|
158
|
-
return True
|
|
159
|
-
return not bool(re.search(self._names_exclude_regex, name, re.UNICODE | re.V1))
|
|
236
|
+
def org_id_to_affiliation_id(self, org_scheme, org_id):
|
|
237
|
+
"""Convert and ORCiD org ID to a linkable affiliation ID."""
|
|
238
|
+
return self._org_id_to_affiliation_id_func(org_scheme, org_id)
|
|
160
239
|
|
|
161
240
|
def apply(self, stream_entry, **kwargs):
|
|
162
241
|
"""Applies the transformation to the stream entry."""
|
|
@@ -166,42 +245,88 @@ class OrcidTransformer(BaseTransformer):
|
|
|
166
245
|
|
|
167
246
|
name = person.get("name")
|
|
168
247
|
if name is None:
|
|
169
|
-
raise TransformerError(
|
|
248
|
+
raise TransformerError("Name not found in ORCiD entry.")
|
|
170
249
|
if name.get("family-name") is None:
|
|
171
|
-
raise TransformerError(
|
|
250
|
+
raise TransformerError("Family name not found in ORCiD entry.")
|
|
172
251
|
|
|
173
252
|
if not self._is_valid_name(name["given-names"] + name["family-name"]):
|
|
174
|
-
raise TransformerError(
|
|
253
|
+
raise TransformerError("Invalid characters in name.")
|
|
175
254
|
|
|
176
255
|
entry = {
|
|
177
256
|
"id": orcid_id,
|
|
178
257
|
"given_name": name.get("given-names"),
|
|
179
258
|
"family_name": name.get("family-name"),
|
|
180
259
|
"identifiers": [{"scheme": "orcid", "identifier": orcid_id}],
|
|
181
|
-
"affiliations":
|
|
260
|
+
"affiliations": self._extract_affiliations(record),
|
|
182
261
|
}
|
|
183
262
|
|
|
263
|
+
stream_entry.entry = entry
|
|
264
|
+
return stream_entry
|
|
265
|
+
|
|
266
|
+
def _is_valid_name(self, name):
|
|
267
|
+
"""Check whether the name passes the regex."""
|
|
268
|
+
if not self._names_exclude_regex:
|
|
269
|
+
return True
|
|
270
|
+
return not bool(re.search(self._names_exclude_regex, name, re.UNICODE | re.V1))
|
|
271
|
+
|
|
272
|
+
def _extract_affiliations(self, record):
|
|
273
|
+
"""Extract affiliations from the ORCiD record."""
|
|
274
|
+
result = []
|
|
184
275
|
try:
|
|
185
|
-
employments =
|
|
186
|
-
record
|
|
276
|
+
employments = (
|
|
277
|
+
record.get("activities-summary", {})
|
|
278
|
+
.get("employments", {})
|
|
279
|
+
.get("affiliation-group", [])
|
|
187
280
|
)
|
|
281
|
+
|
|
282
|
+
# If there are single values, the XML to dict, doesn't wrap them in a list
|
|
188
283
|
if isinstance(employments, dict):
|
|
189
284
|
employments = [employments]
|
|
190
|
-
|
|
285
|
+
|
|
286
|
+
# Remove the "employment-summary" nesting
|
|
287
|
+
employments = [
|
|
288
|
+
employment.get("employment-summary", {}) for employment in employments
|
|
289
|
+
]
|
|
290
|
+
|
|
191
291
|
for employment in employments:
|
|
192
|
-
terminated = employment
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
292
|
+
terminated = employment.get("end-date")
|
|
293
|
+
if terminated:
|
|
294
|
+
continue
|
|
295
|
+
|
|
296
|
+
org = employment["organization"]
|
|
297
|
+
aff_id = self._extract_affiliation_id(org)
|
|
298
|
+
|
|
299
|
+
# Skip adding if the ID already exists in result
|
|
300
|
+
if aff_id and any(aff.get("id") == aff_id for aff in result):
|
|
301
|
+
continue
|
|
302
|
+
|
|
303
|
+
# Skip adding if the name exists in result with no ID
|
|
304
|
+
if any(
|
|
305
|
+
aff.get("name") == org["name"] and "id" not in aff for aff in result
|
|
306
|
+
):
|
|
307
|
+
continue
|
|
308
|
+
|
|
309
|
+
aff = {"name": org["name"]}
|
|
310
|
+
if aff_id:
|
|
311
|
+
aff["id"] = aff_id
|
|
312
|
+
|
|
313
|
+
result.append(aff)
|
|
200
314
|
except Exception:
|
|
201
315
|
pass
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
316
|
+
return result
|
|
317
|
+
|
|
318
|
+
def _extract_affiliation_id(self, org):
|
|
319
|
+
"""Extract the affiliation ID from an ORCiD organization."""
|
|
320
|
+
dis_org = org.get("disambiguated-organization")
|
|
321
|
+
if not dis_org:
|
|
322
|
+
return
|
|
323
|
+
|
|
324
|
+
aff_id = None
|
|
325
|
+
org_id = dis_org.get("disambiguated-organization-identifier")
|
|
326
|
+
org_scheme = dis_org.get("disambiguation-source")
|
|
327
|
+
if org_id and org_scheme:
|
|
328
|
+
aff_id = self.org_id_to_affiliation_id(org_scheme, org_id)
|
|
329
|
+
return aff_id
|
|
205
330
|
|
|
206
331
|
|
|
207
332
|
class NamesServiceWriter(ServiceWriter):
|
|
@@ -125,6 +125,17 @@
|
|
|
125
125
|
"type": "text",
|
|
126
126
|
"analyzer": "accent_edge_analyzer",
|
|
127
127
|
"search_analyzer": "accent_analyzer"
|
|
128
|
+
},
|
|
129
|
+
"acronym": {
|
|
130
|
+
"type": "text",
|
|
131
|
+
"analyzer": "accent_edge_analyzer",
|
|
132
|
+
"search_analyzer": "accent_analyzer",
|
|
133
|
+
"fields": {
|
|
134
|
+
"keyword": {
|
|
135
|
+
"type": "keyword",
|
|
136
|
+
"normalizer": "accent_normalizer"
|
|
137
|
+
}
|
|
138
|
+
}
|
|
128
139
|
}
|
|
129
140
|
}
|
|
130
141
|
},
|
|
@@ -125,6 +125,17 @@
|
|
|
125
125
|
"type": "text",
|
|
126
126
|
"analyzer": "accent_edge_analyzer",
|
|
127
127
|
"search_analyzer": "accent_analyzer"
|
|
128
|
+
},
|
|
129
|
+
"acronym": {
|
|
130
|
+
"type": "text",
|
|
131
|
+
"analyzer": "accent_edge_analyzer",
|
|
132
|
+
"search_analyzer": "accent_analyzer",
|
|
133
|
+
"fields": {
|
|
134
|
+
"keyword": {
|
|
135
|
+
"type": "keyword",
|
|
136
|
+
"normalizer": "accent_normalizer"
|
|
137
|
+
}
|
|
138
|
+
}
|
|
128
139
|
}
|
|
129
140
|
}
|
|
130
141
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
2
|
#
|
|
3
|
-
# Copyright (C) 2021 CERN.
|
|
3
|
+
# Copyright (C) 2021-2024 CERN.
|
|
4
4
|
#
|
|
5
5
|
# Invenio-Vocabularies is free software; you can redistribute it and/or
|
|
6
6
|
# modify it under the terms of the MIT License; see LICENSE file for more
|
|
@@ -16,10 +16,18 @@ from marshmallow_utils.fields import IdentifierSet, SanitizedUnicode
|
|
|
16
16
|
from marshmallow_utils.schemas import IdentifierSchema
|
|
17
17
|
|
|
18
18
|
from ...services.schema import BaseVocabularySchema, ModePIDFieldVocabularyMixin
|
|
19
|
-
from ..affiliations.schema import
|
|
19
|
+
from ..affiliations.schema import (
|
|
20
|
+
AffiliationRelationSchema as BaseAffiliationRelationSchema,
|
|
21
|
+
)
|
|
20
22
|
from .config import names_schemes
|
|
21
23
|
|
|
22
24
|
|
|
25
|
+
class AffiliationRelationSchema(BaseAffiliationRelationSchema):
|
|
26
|
+
"""Affiliation relation schema."""
|
|
27
|
+
|
|
28
|
+
acronym = SanitizedUnicode(dump_only=True)
|
|
29
|
+
|
|
30
|
+
|
|
23
31
|
class NameSchema(BaseVocabularySchema, ModePIDFieldVocabularyMixin):
|
|
24
32
|
"""Service schema for names.
|
|
25
33
|
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
#
|
|
3
|
+
# Copyright (C) 2024 CERN.
|
|
4
|
+
#
|
|
5
|
+
# Invenio-Vocabularies is free software; you can redistribute it and/or
|
|
6
|
+
# modify it under the terms of the MIT License; see LICENSE file for more
|
|
7
|
+
# details.
|
|
8
|
+
|
|
9
|
+
"""BODC subjects datastreams, readers, transformers, and writers."""
|
|
10
|
+
|
|
11
|
+
from invenio_vocabularies.datastreams.errors import TransformerError
|
|
12
|
+
from invenio_vocabularies.datastreams.readers import RDFReader
|
|
13
|
+
from invenio_vocabularies.datastreams.transformers import RDFTransformer
|
|
14
|
+
|
|
15
|
+
from ..config import bodc_puv_file_url
|
|
16
|
+
|
|
17
|
+
# Available with the "rdf" extra
|
|
18
|
+
try:
|
|
19
|
+
import rdflib
|
|
20
|
+
except ImportError:
|
|
21
|
+
rdflib = None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class BODCPUVSubjectsTransformer(RDFTransformer):
|
|
25
|
+
"""
|
|
26
|
+
Transformer class to convert BODC-PUV RDF data to a dictionary format.
|
|
27
|
+
|
|
28
|
+
Input:
|
|
29
|
+
- Relevant fields:
|
|
30
|
+
- `skos:notation`: Primary identifier for the concept.
|
|
31
|
+
- `skos:prefLabel`: Preferred labels with language codes.
|
|
32
|
+
- `skos:altLabel`: Alternative labels (optional).
|
|
33
|
+
- `skos:definition`: Definitions of the concept.
|
|
34
|
+
- `owl:deprecated`: Boolean flag indicating if the concept is deprecated.
|
|
35
|
+
|
|
36
|
+
Output:
|
|
37
|
+
- A dictionary with the following structure:
|
|
38
|
+
{
|
|
39
|
+
"id": "SDN:P01::SAGEMSFM", # BODC-specific parameter ID (skos:notation).
|
|
40
|
+
"scheme": "BODC-PUV", # The scheme name indicating this is a BODC Parameter Usage Vocabulary concept.
|
|
41
|
+
"subject": "AMSSedAge", # The alternative label (skos:altLabel), if available, or None.
|
|
42
|
+
"title": {
|
|
43
|
+
"en": "14C age of Foraminiferida" # English preferred label (skos:prefLabel).
|
|
44
|
+
},
|
|
45
|
+
"props": {
|
|
46
|
+
"definitions": "Accelerated mass spectrometry on picked tests", # Definition of subject (skos:definition).
|
|
47
|
+
},
|
|
48
|
+
"identifiers": [
|
|
49
|
+
{
|
|
50
|
+
"scheme": "url", # Type of identifier (URL).
|
|
51
|
+
"identifier": "http://vocab.nerc.ac.uk/collection/P01/current/SAGEMSFM" # URI of the concept.
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
def _get_subject_data(self, rdf_graph, subject):
|
|
58
|
+
"""Fetch all triples for a subject and organize them into a dictionary."""
|
|
59
|
+
data = {}
|
|
60
|
+
for predicate, obj in rdf_graph.predicate_objects(subject=subject):
|
|
61
|
+
predicate_name = str(predicate)
|
|
62
|
+
if predicate_name not in data:
|
|
63
|
+
data[predicate_name] = []
|
|
64
|
+
data[predicate_name].append(obj)
|
|
65
|
+
return data
|
|
66
|
+
|
|
67
|
+
def _transform_entry(self, subject, rdf_graph):
|
|
68
|
+
"""Transform an entry to the required dictionary format."""
|
|
69
|
+
labels = self._get_labels(subject, rdf_graph)
|
|
70
|
+
subject_data = self._get_subject_data(rdf_graph, subject)
|
|
71
|
+
deprecated = subject_data.get(str(rdflib.namespace.OWL.deprecated), [False])
|
|
72
|
+
if deprecated and str(deprecated[0]).lower() == "true":
|
|
73
|
+
return None # Skip deprecated subjects
|
|
74
|
+
|
|
75
|
+
notation = subject_data.get(str(self.skos_core.notation), [])
|
|
76
|
+
if notation:
|
|
77
|
+
id = str(notation[0])
|
|
78
|
+
else:
|
|
79
|
+
raise TransformerError(f"No id found for: {subject}")
|
|
80
|
+
|
|
81
|
+
alt_labels = [obj for obj in subject_data.get(str(self.skos_core.altLabel), [])]
|
|
82
|
+
subject_text = str(alt_labels[0]) if alt_labels else ""
|
|
83
|
+
definition = str(subject_data.get(str(self.skos_core.definition), [None])[0])
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
"id": id,
|
|
87
|
+
"scheme": "BODC-PUV",
|
|
88
|
+
"subject": subject_text,
|
|
89
|
+
"title": labels,
|
|
90
|
+
"props": {"definition": definition} if definition else {},
|
|
91
|
+
"identifiers": self._get_identifiers(subject),
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# Configuration for datastream
|
|
96
|
+
|
|
97
|
+
VOCABULARIES_DATASTREAM_TRANSFORMERS = {"bodc-transformer": BODCPUVSubjectsTransformer}
|
|
98
|
+
|
|
99
|
+
DATASTREAM_CONFIG = {
|
|
100
|
+
"readers": [
|
|
101
|
+
{
|
|
102
|
+
"type": "http",
|
|
103
|
+
"args": {
|
|
104
|
+
"origin": bodc_puv_file_url,
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{"type": "rdf"},
|
|
108
|
+
],
|
|
109
|
+
"transformers": [{"type": "bodc-transformer"}],
|
|
110
|
+
"writers": [{"args": {"writer": {"type": "subjects-service"}}, "type": "async"}],
|
|
111
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
2
|
#
|
|
3
|
-
# Copyright (C) 2021 CERN.
|
|
3
|
+
# Copyright (C) 2021-2024 CERN.
|
|
4
4
|
# Copyright (C) 2021 Northwestern University.
|
|
5
5
|
# Copyright (C) 2024 University of Münster.
|
|
6
6
|
#
|
|
@@ -15,10 +15,12 @@ from invenio_i18n import get_locale
|
|
|
15
15
|
from invenio_i18n import lazy_gettext as _
|
|
16
16
|
from invenio_records_resources.services import SearchOptions
|
|
17
17
|
from invenio_records_resources.services.records.components import DataComponent
|
|
18
|
+
from invenio_records_resources.services.records.queryparser import (
|
|
19
|
+
CompositeSuggestQueryParser,
|
|
20
|
+
)
|
|
18
21
|
from werkzeug.local import LocalProxy
|
|
19
22
|
|
|
20
23
|
from ...services.components import PIDComponent
|
|
21
|
-
from ...services.querystr import FilteredSuggestQueryParser
|
|
22
24
|
|
|
23
25
|
subject_schemes = LocalProxy(
|
|
24
26
|
lambda: current_app.config["VOCABULARIES_SUBJECTS_SCHEMES"]
|
|
@@ -26,12 +28,24 @@ subject_schemes = LocalProxy(
|
|
|
26
28
|
localized_title = LocalProxy(lambda: f"title.{get_locale()}^20")
|
|
27
29
|
|
|
28
30
|
|
|
31
|
+
gemet_file_url = LocalProxy(
|
|
32
|
+
lambda: current_app.config["VOCABULARIES_SUBJECTS_GEMET_FILE_URL"]
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
euroscivoc_file_url = LocalProxy(
|
|
36
|
+
lambda: current_app.config["VOCABULARIES_SUBJECTS_EUROSCIVOC_FILE_URL"]
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
bodc_puv_file_url = LocalProxy(
|
|
40
|
+
lambda: current_app.config["VOCABULARIES_SUBJECTS_BODC_PUV_FILE_URL"]
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
29
44
|
class SubjectsSearchOptions(SearchOptions):
|
|
30
45
|
"""Search options."""
|
|
31
46
|
|
|
32
|
-
suggest_parser_cls =
|
|
33
|
-
|
|
34
|
-
fields=[ # suggest fields
|
|
47
|
+
suggest_parser_cls = CompositeSuggestQueryParser.factory(
|
|
48
|
+
fields=[
|
|
35
49
|
"subject^100",
|
|
36
50
|
localized_title,
|
|
37
51
|
"synonyms^20",
|
|
@@ -12,7 +12,9 @@ from invenio_access.permissions import system_identity
|
|
|
12
12
|
from invenio_i18n import lazy_gettext as _
|
|
13
13
|
|
|
14
14
|
from ...datastreams.writers import ServiceWriter
|
|
15
|
+
from .bodc import datastreams as bodc_datastreams
|
|
15
16
|
from .euroscivoc import datastreams as euroscivoc_datastreams
|
|
17
|
+
from .gemet import datastreams as gemet_datastreams
|
|
16
18
|
from .mesh import datastreams as mesh_datastreams
|
|
17
19
|
|
|
18
20
|
|
|
@@ -31,20 +33,20 @@ class SubjectsServiceWriter(ServiceWriter):
|
|
|
31
33
|
|
|
32
34
|
VOCABULARIES_DATASTREAM_READERS = {
|
|
33
35
|
**mesh_datastreams.VOCABULARIES_DATASTREAM_READERS,
|
|
34
|
-
**euroscivoc_datastreams.VOCABULARIES_DATASTREAM_READERS,
|
|
35
36
|
}
|
|
36
37
|
"""Subjects Data Streams readers."""
|
|
37
38
|
|
|
38
39
|
VOCABULARIES_DATASTREAM_TRANSFORMERS = {
|
|
39
40
|
**mesh_datastreams.VOCABULARIES_DATASTREAM_TRANSFORMERS,
|
|
40
41
|
**euroscivoc_datastreams.VOCABULARIES_DATASTREAM_TRANSFORMERS,
|
|
42
|
+
**gemet_datastreams.VOCABULARIES_DATASTREAM_TRANSFORMERS,
|
|
43
|
+
**bodc_datastreams.VOCABULARIES_DATASTREAM_TRANSFORMERS,
|
|
41
44
|
}
|
|
42
45
|
"""Subjects Data Streams transformers."""
|
|
43
46
|
|
|
44
47
|
VOCABULARIES_DATASTREAM_WRITERS = {
|
|
45
48
|
"subjects-service": SubjectsServiceWriter,
|
|
46
49
|
**mesh_datastreams.VOCABULARIES_DATASTREAM_WRITERS,
|
|
47
|
-
**euroscivoc_datastreams.VOCABULARIES_DATASTREAM_WRITERS,
|
|
48
50
|
}
|
|
49
51
|
"""Subjects Data Streams writers."""
|
|
50
52
|
|