-
Notifications
You must be signed in to change notification settings - Fork 146
/
euro_dcat_ap_base.py
629 lines (540 loc) · 23.6 KB
/
euro_dcat_ap_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import json
from decimal import Decimal, DecimalException
from rdflib import term, URIRef, BNode, Literal
import ckantoolkit as toolkit
from ckan.lib.munge import munge_tag
from ckanext.dcat.utils import (
resource_uri,
DCAT_EXPOSE_SUBCATALOGS,
DCAT_CLEAN_TAGS,
publisher_uri_organization_fallback,
)
from .base import RDFProfile, URIRefOrLiteral, CleanedURIRef
from .base import (
RDF,
XSD,
SKOS,
RDFS,
DCAT,
DCT,
ADMS,
VCARD,
FOAF,
SCHEMA,
LOCN,
GSP,
OWL,
SPDX,
GEOJSON_IMT,
namespaces,
)
config = toolkit.config
DISTRIBUTION_LICENSE_FALLBACK_CONFIG = "ckanext.dcat.resource.inherit.license"
class BaseEuropeanDCATAPProfile(RDFProfile):
"""
A base profile with common RDF properties across the different DCAT-AP versions
"""
def _parse_dataset_base(self, dataset_dict, dataset_ref):
dataset_dict["extras"] = []
dataset_dict["resources"] = []
# Basic fields
for key, predicate in (
("title", DCT.title),
("notes", DCT.description),
("url", DCAT.landingPage),
("version", OWL.versionInfo),
):
value = self._object_value(dataset_ref, predicate)
if value:
dataset_dict[key] = value
if not dataset_dict.get("version"):
# adms:version was supported on the first version of the DCAT-AP
value = self._object_value(dataset_ref, ADMS.version)
if value:
dataset_dict["version"] = value
# Tags
# replace munge_tag to noop if there's no need to clean tags
do_clean = toolkit.asbool(config.get(DCAT_CLEAN_TAGS, False))
tags_val = [
munge_tag(tag) if do_clean else tag for tag in self._keywords(dataset_ref)
]
tags = [{"name": tag} for tag in tags_val]
dataset_dict["tags"] = tags
# Extras
# Simple values
for key, predicate in (
("issued", DCT.issued),
("modified", DCT.modified),
("identifier", DCT.identifier),
("version_notes", ADMS.versionNotes),
("frequency", DCT.accrualPeriodicity),
("provenance", DCT.provenance),
("dcat_type", DCT.type),
):
value = self._object_value(dataset_ref, predicate)
if value:
dataset_dict["extras"].append({"key": key, "value": value})
# Lists
for key, predicate, in (
("language", DCT.language),
("theme", DCAT.theme),
("alternate_identifier", ADMS.identifier),
("conforms_to", DCT.conformsTo),
("documentation", FOAF.page),
("related_resource", DCT.relation),
("has_version", DCT.hasVersion),
("is_version_of", DCT.isVersionOf),
("source", DCT.source),
("sample", ADMS.sample),
):
values = self._object_value_list(dataset_ref, predicate)
if values:
dataset_dict["extras"].append({"key": key, "value": json.dumps(values)})
# Contact details
contact = self._contact_details(dataset_ref, DCAT.contactPoint)
if not contact:
# adms:contactPoint was supported on the first version of DCAT-AP
contact = self._contact_details(dataset_ref, ADMS.contactPoint)
if contact:
for key in ("uri", "name", "email"):
if contact.get(key):
dataset_dict["extras"].append(
{"key": "contact_{0}".format(key), "value": contact.get(key)}
)
# Publisher
publisher = self._publisher(dataset_ref, DCT.publisher)
for key in ("uri", "name", "email", "url", "type"):
if publisher.get(key):
dataset_dict["extras"].append(
{"key": "publisher_{0}".format(key), "value": publisher.get(key)}
)
# Temporal
start, end = self._time_interval(dataset_ref, DCT.temporal)
if start:
dataset_dict["extras"].append({"key": "temporal_start", "value": start})
if end:
dataset_dict["extras"].append({"key": "temporal_end", "value": end})
# Spatial
spatial = self._spatial(dataset_ref, DCT.spatial)
for key in ("uri", "text", "geom"):
self._add_spatial_to_dict(dataset_dict, key, spatial)
# Dataset URI (explicitly show the missing ones)
dataset_uri = str(dataset_ref) if isinstance(dataset_ref, term.URIRef) else ""
dataset_dict["extras"].append({"key": "uri", "value": dataset_uri})
# access_rights
access_rights = self._access_rights(dataset_ref, DCT.accessRights)
if access_rights:
dataset_dict["extras"].append(
{"key": "access_rights", "value": access_rights}
)
# License
if "license_id" not in dataset_dict:
dataset_dict["license_id"] = self._license(dataset_ref)
# Source Catalog
if toolkit.asbool(config.get(DCAT_EXPOSE_SUBCATALOGS, False)):
catalog_src = self._get_source_catalog(dataset_ref)
if catalog_src is not None:
src_data = self._extract_catalog_dict(catalog_src)
dataset_dict["extras"].extend(src_data)
# Resources
for distribution in self._distributions(dataset_ref):
resource_dict = {}
# Simple values
for key, predicate in (
("name", DCT.title),
("description", DCT.description),
("access_url", DCAT.accessURL),
("download_url", DCAT.downloadURL),
("issued", DCT.issued),
("modified", DCT.modified),
("status", ADMS.status),
("license", DCT.license),
):
value = self._object_value(distribution, predicate)
if value:
resource_dict[key] = value
resource_dict["url"] = self._object_value(
distribution, DCAT.downloadURL
) or self._object_value(distribution, DCAT.accessURL)
# Lists
for key, predicate in (
("language", DCT.language),
("documentation", FOAF.page),
("conforms_to", DCT.conformsTo),
):
values = self._object_value_list(distribution, predicate)
if values:
resource_dict[key] = json.dumps(values)
# rights
rights = self._access_rights(distribution, DCT.rights)
if rights:
resource_dict["rights"] = rights
# Format and media type
normalize_ckan_format = toolkit.asbool(
config.get("ckanext.dcat.normalize_ckan_format", True)
)
imt, label = self._distribution_format(distribution, normalize_ckan_format)
if imt:
resource_dict["mimetype"] = imt
if label:
resource_dict["format"] = label
elif imt:
resource_dict["format"] = imt
# Size
size = self._object_value_int(distribution, DCAT.byteSize)
if size is not None:
resource_dict["size"] = size
# Checksum
for checksum in self.g.objects(distribution, SPDX.checksum):
algorithm = self._object_value(checksum, SPDX.algorithm)
checksum_value = self._object_value(checksum, SPDX.checksumValue)
if algorithm:
resource_dict["hash_algorithm"] = algorithm
if checksum_value:
resource_dict["hash"] = checksum_value
# Distribution URI (explicitly show the missing ones)
resource_dict["uri"] = (
str(distribution) if isinstance(distribution, term.URIRef) else ""
)
# Remember the (internal) distribution reference for referencing in
# further profiles, e.g. for adding more properties
resource_dict["distribution_ref"] = str(distribution)
dataset_dict["resources"].append(resource_dict)
if self.compatibility_mode:
# Tweak the resulting dict to make it compatible with previous
# versions of the ckanext-dcat parsers
for extra in dataset_dict["extras"]:
if extra["key"] in (
"issued",
"modified",
"publisher_name",
"publisher_email",
):
extra["key"] = "dcat_" + extra["key"]
if extra["key"] == "language":
extra["value"] = ",".join(sorted(json.loads(extra["value"])))
return dataset_dict
def _graph_from_dataset_base(self, dataset_dict, dataset_ref):
g = self.g
for prefix, namespace in namespaces.items():
g.bind(prefix, namespace)
g.add((dataset_ref, RDF.type, DCAT.Dataset))
# Basic fields
items = [
("title", DCT.title, None, Literal),
("notes", DCT.description, None, Literal),
("url", DCAT.landingPage, None, URIRef, FOAF.Document),
("identifier", DCT.identifier, ["guid", "id"], URIRefOrLiteral),
("version", OWL.versionInfo, ["dcat_version"], Literal),
("version_notes", ADMS.versionNotes, None, Literal),
("frequency", DCT.accrualPeriodicity, None, URIRefOrLiteral, DCT.Frequency),
("access_rights", DCT.accessRights, None, URIRefOrLiteral, DCT.AccessRights),
("dcat_type", DCT.type, None, URIRefOrLiteral),
("provenance", DCT.provenance, None, URIRefOrLiteral, DCT.ProvenanceStatement),
]
self._add_triples_from_dict(dataset_dict, dataset_ref, items)
# Tags
for tag in dataset_dict.get("tags", []):
g.add((dataset_ref, DCAT.keyword, Literal(tag["name"])))
# Dates
items = [
("issued", DCT.issued, ["metadata_created"], Literal),
("modified", DCT.modified, ["metadata_modified"], Literal),
]
self._add_date_triples_from_dict(dataset_dict, dataset_ref, items)
# Lists
items = [
("language", DCT.language, None, URIRefOrLiteral, DCT.LinguisticSystem),
("theme", DCAT.theme, None, URIRef),
("conforms_to", DCT.conformsTo, None, URIRefOrLiteral, DCT.Standard),
("documentation", FOAF.page, None, URIRefOrLiteral, FOAF.Document),
("related_resource", DCT.relation, None, URIRefOrLiteral, RDFS.Resource),
("has_version", DCT.hasVersion, None, URIRefOrLiteral),
("is_version_of", DCT.isVersionOf, None, URIRefOrLiteral),
("source", DCT.source, None, URIRefOrLiteral),
("sample", ADMS.sample, None, URIRefOrLiteral, DCAT.Distribution),
]
self._add_list_triples_from_dict(dataset_dict, dataset_ref, items)
# Contact details
if any(
[
self._get_dataset_value(dataset_dict, "contact_uri"),
self._get_dataset_value(dataset_dict, "contact_name"),
self._get_dataset_value(dataset_dict, "contact_email"),
self._get_dataset_value(dataset_dict, "maintainer"),
self._get_dataset_value(dataset_dict, "maintainer_email"),
self._get_dataset_value(dataset_dict, "author"),
self._get_dataset_value(dataset_dict, "author_email"),
]
):
contact_uri = self._get_dataset_value(dataset_dict, "contact_uri")
if contact_uri:
contact_details = CleanedURIRef(contact_uri)
else:
contact_details = BNode()
g.add((contact_details, RDF.type, VCARD.Organization))
g.add((dataset_ref, DCAT.contactPoint, contact_details))
self._add_triple_from_dict(
dataset_dict,
contact_details,
VCARD.fn,
"contact_name",
["maintainer", "author"],
)
# Add mail address as URIRef, and ensure it has a mailto: prefix
self._add_triple_from_dict(
dataset_dict,
contact_details,
VCARD.hasEmail,
"contact_email",
["maintainer_email", "author_email"],
_type=URIRef,
value_modifier=self._add_mailto,
)
# Publisher
publisher_ref = None
if dataset_dict.get("publisher"):
# Scheming publisher field: will be handled in a separate profile
pass
elif any(
[
self._get_dataset_value(dataset_dict, "publisher_uri"),
self._get_dataset_value(dataset_dict, "publisher_name"),
]
):
# Legacy publisher_* extras
publisher_uri = self._get_dataset_value(dataset_dict, "publisher_uri")
publisher_name = self._get_dataset_value(dataset_dict, "publisher_name")
if publisher_uri:
publisher_ref = CleanedURIRef(publisher_uri)
else:
# No publisher_uri
publisher_ref = BNode()
publisher_details = {
"name": publisher_name,
"email": self._get_dataset_value(dataset_dict, "publisher_email"),
"url": self._get_dataset_value(dataset_dict, "publisher_url"),
"type": self._get_dataset_value(dataset_dict, "publisher_type"),
}
elif dataset_dict.get("organization"):
# Fall back to dataset org
org_id = dataset_dict["organization"]["id"]
org_dict = None
if org_id in self._org_cache:
org_dict = self._org_cache[org_id]
else:
try:
org_dict = toolkit.get_action("organization_show")(
{"ignore_auth": True}, {"id": org_id}
)
self._org_cache[org_id] = org_dict
except toolkit.ObjectNotFound:
pass
if org_dict:
publisher_ref = CleanedURIRef(
publisher_uri_organization_fallback(dataset_dict)
)
publisher_details = {
"name": org_dict.get("title"),
"email": org_dict.get("email"),
"url": org_dict.get("url"),
"type": org_dict.get("dcat_type"),
}
# Add to graph
if publisher_ref:
g.add((publisher_ref, RDF.type, FOAF.Agent))
g.add((dataset_ref, DCT.publisher, publisher_ref))
items = [
("name", FOAF.name, None, Literal),
("email", FOAF.mbox, None, Literal),
("url", FOAF.homepage, None, URIRef),
("type", DCT.type, None, URIRefOrLiteral),
]
self._add_triples_from_dict(publisher_details, publisher_ref, items)
# Temporal
start = self._get_dataset_value(dataset_dict, "temporal_start")
end = self._get_dataset_value(dataset_dict, "temporal_end")
if start or end:
temporal_extent = BNode()
g.add((temporal_extent, RDF.type, DCT.PeriodOfTime))
if start:
self._add_date_triple(temporal_extent, SCHEMA.startDate, start)
if end:
self._add_date_triple(temporal_extent, SCHEMA.endDate, end)
g.add((dataset_ref, DCT.temporal, temporal_extent))
# Spatial
spatial_text = self._get_dataset_value(dataset_dict, "spatial_text")
spatial_geom = self._get_dataset_value(dataset_dict, "spatial")
if spatial_text or spatial_geom:
spatial_ref = self._get_or_create_spatial_ref(dataset_dict, dataset_ref)
if spatial_text:
g.add((spatial_ref, SKOS.prefLabel, Literal(spatial_text)))
if spatial_geom:
self._add_spatial_value_to_graph(
spatial_ref, LOCN.geometry, spatial_geom
)
# Use fallback license if set in config
resource_license_fallback = None
if toolkit.asbool(config.get(DISTRIBUTION_LICENSE_FALLBACK_CONFIG, False)):
if "license_id" in dataset_dict and isinstance(
URIRefOrLiteral(dataset_dict["license_id"]), URIRef
):
resource_license_fallback = dataset_dict["license_id"]
elif "license_url" in dataset_dict and isinstance(
URIRefOrLiteral(dataset_dict["license_url"]), URIRef
):
resource_license_fallback = dataset_dict["license_url"]
# Resources
for resource_dict in dataset_dict.get("resources", []):
distribution = CleanedURIRef(resource_uri(resource_dict))
g.add((dataset_ref, DCAT.distribution, distribution))
g.add((distribution, RDF.type, DCAT.Distribution))
# Simple values
items = [
("name", DCT.title, None, Literal),
("description", DCT.description, None, Literal),
("status", ADMS.status, None, URIRefOrLiteral),
("rights", DCT.rights, None, URIRefOrLiteral, DCT.RightsStatement),
("license", DCT.license, None, URIRefOrLiteral, DCT.LicenseDocument),
("access_url", DCAT.accessURL, None, URIRef, RDFS.Resource),
("download_url", DCAT.downloadURL, None, URIRef, RDFS.Resource),
]
self._add_triples_from_dict(resource_dict, distribution, items)
# Lists
items = [
("documentation", FOAF.page, None, URIRefOrLiteral, FOAF.Document),
("language", DCT.language, None, URIRefOrLiteral, DCT.LinguisticSystem),
("conforms_to", DCT.conformsTo, None, URIRefOrLiteral, DCT.Standard),
]
self._add_list_triples_from_dict(resource_dict, distribution, items)
# Set default license for distribution if needed and available
if resource_license_fallback and not (distribution, DCT.license, None) in g:
g.add(
(
distribution,
DCT.license,
URIRefOrLiteral(resource_license_fallback),
)
)
# TODO: add an actual field to manage this
if (distribution, DCT.license, None) in g:
g.add(
(
list(g.objects(distribution, DCT.license))[0],
DCT.type,
URIRef("http://purl.org/adms/licencetype/UnknownIPR")
)
)
# Format
mimetype = resource_dict.get("mimetype")
fmt = resource_dict.get("format")
# IANA media types (either URI or Literal) should be mapped as mediaType.
# In case format is available and mimetype is not set or identical to format,
# check which type is appropriate.
if fmt and (not mimetype or mimetype == fmt):
if (
"iana.org/assignments/media-types" in fmt
or not fmt.startswith("http")
and "/" in fmt
):
# output format value as dcat:mediaType instead of dct:format
mimetype = fmt
fmt = None
else:
# Use dct:format
mimetype = None
if mimetype:
mimetype = URIRefOrLiteral(mimetype)
g.add((distribution, DCAT.mediaType, mimetype))
if isinstance(mimetype, URIRef):
g.add((mimetype, RDF.type, DCT.MediaType))
if fmt:
fmt = URIRefOrLiteral(fmt)
g.add((distribution, DCT["format"], fmt))
if isinstance(fmt, URIRef):
g.add((fmt, RDF.type, DCT.MediaTypeOrExtent))
# URL fallback and old behavior
url = resource_dict.get("url")
download_url = resource_dict.get("download_url")
access_url = resource_dict.get("access_url")
# Use url as fallback for access_url if access_url is not set and download_url is not equal
if url and not access_url:
if (not download_url) or (download_url and url != download_url):
self._add_triple_from_dict(
resource_dict, distribution, DCAT.accessURL, "url", _type=URIRef
)
# Dates
items = [
("issued", DCT.issued, ["created"], Literal),
("modified", DCT.modified, ["metadata_modified"], Literal),
]
self._add_date_triples_from_dict(resource_dict, distribution, items)
# Numbers
if resource_dict.get("size"):
try:
g.add(
(
distribution,
DCAT.byteSize,
Literal(Decimal(resource_dict["size"]), datatype=XSD.decimal),
)
)
except (ValueError, TypeError, DecimalException):
g.add((distribution, DCAT.byteSize, Literal(resource_dict["size"])))
# Checksum
if resource_dict.get("hash"):
checksum = BNode()
g.add((checksum, RDF.type, SPDX.Checksum))
g.add(
(
checksum,
SPDX.checksumValue,
Literal(resource_dict["hash"], datatype=XSD.hexBinary),
)
)
if resource_dict.get("hash_algorithm"):
checksum_algo = URIRefOrLiteral(resource_dict["hash_algorithm"])
g.add(
(
checksum,
SPDX.algorithm,
checksum_algo,
)
)
if isinstance(checksum_algo, URIRef):
g.add((checksum_algo, RDF.type, SPDX.ChecksumAlgorithm))
g.add((distribution, SPDX.checksum, checksum))
def _graph_from_catalog_base(self, catalog_dict, catalog_ref):
g = self.g
for prefix, namespace in namespaces.items():
g.bind(prefix, namespace)
g.add((catalog_ref, RDF.type, DCAT.Catalog))
# Basic fields
items = [
("title", DCT.title, config.get("ckan.site_title"), Literal),
(
"description",
DCT.description,
config.get("ckan.site_description"),
Literal,
),
("homepage", FOAF.homepage, config.get("ckan.site_url"), URIRef),
(
"language",
DCT.language,
config.get("ckan.locale_default", "en"),
URIRefOrLiteral,
),
]
for item in items:
key, predicate, fallback, _type = item
if catalog_dict:
value = catalog_dict.get(key, fallback)
else:
value = fallback
if value:
g.add((catalog_ref, predicate, _type(value)))
# Dates
modified = self._last_catalog_modification()
if modified:
self._add_date_triple(catalog_ref, DCT.modified, modified)