Page MenuHomeSoftware Heritage
Paste P1432

cff.py with rdflib
ActivePublic

Authored by vlorentz on Aug 19 2022, 4:32 PM.
from typing import Dict, List, Optional, Union
from rdflib import URIRef, XSD, Literal, Graph, BNode
from swh.indexer.codemeta import CROSSWALK_TABLE, add_list
from swh.indexer.namespaces import SCHEMA, RDF
from .base import YamlMapping
DOI = URIRef("https://doi.org/")
SPDX = URIRef("https://spdx.org/licenses/")
class CffMapping(YamlMapping):
"""Dedicated class for Citation (CITATION.cff) mapping and translation"""
name = "cff"
filename = b"CITATION.cff"
mapping = CROSSWALK_TABLE["Citation File Format Core (CFF-Core) 1.0.2"]
string_fields = ["keywords", "license", "abstract", "version", "doi"]
def translate_authors(self, graph: Graph, root: URIRef, d: List[dict]) -> None:
authors = []
for author in d:
if "orcid" in author and isinstance(author["orcid"], str):
author_node = URIRef(author["orcid"])
else:
author_node = BNode()
graph.add((author_node, RDF.type, SCHEMA.Person))
authors.append(author_node)
if "affiliation" in author and isinstance(author["affiliation"], str):
affiliation = BNode()
graph.add((author_node, SCHEMA.affiliation, affiliation))
graph.add((affiliation, RDF.type, SCHEMA.Organization))
graph.add((affiliation, SCHEMA.name, Literal(author["affiliation"])))
if "family-names" in author and isinstance(author["family-names"], str):
graph.add(
(author_node, SCHEMA.familyName, Literal(author["family-names"]))
)
if "given-names" in author and isinstance(author["given-names"], str):
graph.add(
(author_node, SCHEMA.givenName, Literal(author["given-names"]))
)
add_list(graph, root, SCHEMA.author, authors)
def normalize_doi(self, s: str) -> URIRef:
if isinstance(s, str):
return DOI + s
def normalize_license(self, s: str) -> Dict[str, str]:
if isinstance(s, str):
return SPDX + s
def normalize_repository_code(self, s: str) -> Dict[str, str]:
if isinstance(s, str):
return URIRef(s)
def normalize_date_released(self, s: str) -> Dict[str, str]:
if isinstance(s, str):
return Literal(s, datatype=SCHEMA.Date)