Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/metadata_dictionary/base.py
Show First 20 Lines • Show All 244 Lines • ▼ Show 20 Lines | def _translate_dict(self, content_dict: Dict) -> Dict[str, Any]: | |||||||||
for item in v: | for item in v: | |||||||||
if isinstance(item, str): | if isinstance(item, str): | |||||||||
graph.add((root, codemeta_key, rdflib.URIRef(item))) | graph.add((root, codemeta_key, rdflib.URIRef(item))) | |||||||||
else: | else: | |||||||||
continue | continue | |||||||||
self.extra_translation(graph, root, content_dict) | self.extra_translation(graph, root, content_dict) | |||||||||
self.sanitize(graph) | ||||||||||
# Convert from rdflib's internal graph representation to JSON | # Convert from rdflib's internal graph representation to JSON | |||||||||
s = graph.serialize(format="application/ld+json") | s = graph.serialize(format="application/ld+json") | |||||||||
# Load from JSON to a list of Python objects | # Load from JSON to a list of Python objects | |||||||||
jsonld_graph = json.loads(s) | jsonld_graph = json.loads(s) | |||||||||
# Use JSON-LD framing to turn the graph into a rooted tree | # Use JSON-LD framing to turn the graph into a rooted tree | |||||||||
# frame = {"@type": str(SCHEMA.SoftwareSourceCode)} | # frame = {"@type": str(SCHEMA.SoftwareSourceCode)} | |||||||||
Show All 9 Lines | def _translate_dict(self, content_dict: Dict) -> Dict[str, Any]: | |||||||||
# Remove the temporary id we added at the beginning | # Remove the temporary id we added at the beginning | |||||||||
if isinstance(translated_metadata["@id"], list): | if isinstance(translated_metadata["@id"], list): | |||||||||
translated_metadata["@id"].remove(root_id) | translated_metadata["@id"].remove(root_id) | |||||||||
else: | else: | |||||||||
del translated_metadata["@id"] | del translated_metadata["@id"] | |||||||||
return self.normalize_translation(translated_metadata) | return self.normalize_translation(translated_metadata) | |||||||||
def sanitize(self, graph: rdflib.Graph) -> None: | ||||||||||
# Remove triples that make PyLD crash | ||||||||||
for (subject, predicate, _) in graph.triples((None, None, rdflib.URIRef(""))): | ||||||||||
graph.remove((subject, predicate, rdflib.URIRef(""))) | ||||||||||
ardumontUnsubmitted Not Done Inline Actions
ardumont: | ||||||||||
# Should not happen, but we's better check as this may lead to incorrect data | ||||||||||
invalid = False | ||||||||||
for triple in graph.triples((rdflib.URIRef(""), None, None)): | ||||||||||
invalid = True | ||||||||||
logging.error("Empty triple subject URI: %r", triple) | ||||||||||
if invalid: | ||||||||||
raise ValueError("Empty triple subject(s)") | ||||||||||
def extra_translation( | def extra_translation( | |||||||||
self, graph: rdflib.Graph, root: rdflib.term.Node, d: Dict[str, Any] | self, graph: rdflib.Graph, root: rdflib.term.Node, d: Dict[str, Any] | |||||||||
): | ) -> None: | |||||||||
"""Called at the end of the translation process, and may add arbitrary triples | """Called at the end of the translation process, and may add arbitrary triples | |||||||||
to ``graph`` based on the input dictionary (passed as ``d``). | to ``graph`` based on the input dictionary (passed as ``d``). | |||||||||
""" | """ | |||||||||
pass | pass | |||||||||
class JsonMapping(DictMapping): | class JsonMapping(DictMapping): | |||||||||
"""Base class for all mappings that use JSON data as input.""" | """Base class for all mappings that use JSON data as input.""" | |||||||||
▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines |