Skip to content

Commit 594449e

Browse files
refactor(serializers.dcat): extend checksum with spdx clauses
* add spdx clauses as parents classes and properties to checksum. * add XML namespace prefix spdx and binds it to the SPDX RDF vocabulary at that base IRI.
1 parent d2fbc1a commit 594449e

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

invenio_rdm_records/resources/serializers/dcat/__init__.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,31 +75,50 @@ def access_url(file):
7575
url = file.get("access_url")
7676
return {"{{{rdf}}}resource".format(**ns): url} if url else None
7777

78-
def checksum(file):
79-
return file["checksum"] if file.get("checksum") else None
80-
8178
files_fields = {
8279
"{{{dcat}}}downloadURL": download_url,
8380
"{{{dcat}}}mediaType": media_type,
8481
"{{{dcat}}}byteSize": byte_size,
8582
"{{{dcat}}}accessURL": access_url,
86-
"{{{dcat}}}checksum": checksum,
8783
}
8884

89-
for f in files:
85+
for file in files:
9086
dist_wrapper = ET.SubElement(root[0], "{{{dcat}}}distribution".format(**ns))
9187
dist = ET.SubElement(dist_wrapper, "{{{dcat}}}Distribution".format(**ns))
9288

9389
for tag, func in files_fields.items():
94-
tag_value = func(f)
95-
90+
tag_value = func(file)
9691
if tag_value:
9792
el = ET.SubElement(dist, tag.format(**ns))
9893
if isinstance(tag_value, str):
9994
el.text = tag_value
10095
if isinstance(tag_value, dict):
10196
el.attrib.update(tag_value)
10297

98+
checksum = file.get("checksum") if file.get("checksum") else None
99+
if checksum:
100+
value = checksum.split(":")[1]
101+
spdx_checksum_el = ET.SubElement(
102+
dist, "{{{spdx}}}checksum".format(**ns)
103+
)
104+
spdx_checksum_obj = ET.SubElement(
105+
spdx_checksum_el, "{{{spdx}}}Checksum".format(**ns)
106+
)
107+
108+
algo_el = ET.SubElement(
109+
spdx_checksum_obj,
110+
"{{{spdx}}}algorithm".format(**ns),
111+
)
112+
algo_el.attrib["{{{rdf}}}resource".format(**ns)] = (
113+
f"http://spdx.org/rdf/terms#checksumAlgorithm_md5"
114+
)
115+
116+
value_el = ET.SubElement(
117+
spdx_checksum_obj,
118+
"{{{spdx}}}checksumValue".format(**ns),
119+
)
120+
value_el.text = value
121+
103122
def add_missing_creatibutor_links(self, rdf_tree):
104123
"""Add missing `rdf:about` attributes to <rdf:Description> within <dct:creator> and <dct:contributor> and <foaf:Organization> within <org:memberOf>."""
105124
namespaces = rdf_tree.nsmap

invenio_rdm_records/resources/serializers/dcat/datacite-to-dcat-ap.xsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
xmlns:dct = "http://purl.org/dc/terms/"
5454
xmlns:dctype = "http://purl.org/dc/dcmitype/"
5555
xmlns:dcat = "http://www.w3.org/ns/dcat#"
56+
xmlns:spdx = "http://spdx.org/rdf/terms#"
5657
xmlns:foaf = "http://xmlns.com/foaf/0.1/"
5758
xmlns:gsp = "http://www.opengis.net/ont/geosparql#"
5859
xmlns:locn = "http://www.w3.org/ns/locn#"

tests/resources/serializers/test_dcat_serializer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def test_dcat_serializer(running_app, full_record_to_dict):
4040
'xmlns:dct="http://purl.org/dc/terms/" '
4141
'xmlns:dctype="http://purl.org/dc/dcmitype/" '
4242
'xmlns:dcat="http://www.w3.org/ns/dcat#" '
43+
'xmlns:spdx="http://spdx.org/rdf/terms#" '
4344
'xmlns:foaf="http://xmlns.com/foaf/0.1/" '
4445
'xmlns:gsp="http://www.opengis.net/ont/geosparql#" '
4546
'xmlns:locn="http://www.w3.org/ns/locn#" '
@@ -253,7 +254,13 @@ def test_dcat_serializer(running_app, full_record_to_dict):
253254
" <dcat:byteSize>9</dcat:byteSize>\n"
254255
" <dcat:accessURL "
255256
'rdf:resource="https://doi.org/10.1234/12345-abcde"/>\n'
256-
" <dcat:checksum>md5:e795abeef2c38de2b064be9f6364ceae</dcat:checksum>\n"
257+
" <spdx:checksum>\n"
258+
" <spdx:Checksum>\n"
259+
" <spdx:algorithm "
260+
'rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_md5"/>\n'
261+
" <spdx:checksumValue>e795abeef2c38de2b064be9f6364ceae</spdx:checksumValue>\n"
262+
" </spdx:Checksum>\n"
263+
" </spdx:checksum>\n"
257264
" </dcat:Distribution>\n"
258265
" </dcat:distribution>\n"
259266
" </rdf:Description>\n"

0 commit comments

Comments
 (0)