From a09381f6bebd122bafa6e0332fe1c10e13170ad4 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Fri, 8 Nov 2024 19:01:34 -0800 Subject: [PATCH] fix error in rdflib dumper where explicitly assigned prefixes are ignored --- linkml_runtime/dumpers/rdflib_dumper.py | 24 ++++++++++++------------ linkml_runtime/utils/namespaces.py | 11 +++++------ linkml_runtime/utils/schemaview.py | 4 ++-- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/linkml_runtime/dumpers/rdflib_dumper.py b/linkml_runtime/dumpers/rdflib_dumper.py index 4a3d1864..4eab8b04 100644 --- a/linkml_runtime/dumpers/rdflib_dumper.py +++ b/linkml_runtime/dumpers/rdflib_dumper.py @@ -47,26 +47,26 @@ def as_rdf_graph( # TODO replace with `prefix_map = prefix_map.bimap` after making minimum requirement on python 3.8 prefix_map = {record.prefix: record.uri_prefix for record in prefix_map.records} logger.debug(f'PREFIXMAP={prefix_map}') + namespaces = schemaview.namespaces() if prefix_map: for k, v in prefix_map.items(): if k == "@base": - schemaview.namespaces()._base = v + namespaces._base = v else: - schemaview.namespaces()[k] = v + namespaces[k] = v g.namespace_manager.bind(k, URIRef(v)) - for prefix in schemaview.namespaces(): - g.bind(prefix, URIRef(schemaview.namespaces()[prefix])) - else: - for prefix in schemaview.namespaces(): - g.bind(prefix, URIRef(schemaview.namespaces()[prefix])) + + for prefix in namespaces: + g.bind(prefix, URIRef(namespaces[prefix])) # user can pass in base in prefixmap using '_base'. This gets set # in namespaces as a plain dict assignment - explicitly call the setter # to set the underlying "@base" - if "_base" in schemaview.namespaces(): - schemaview.namespaces()._base = schemaview.namespaces()["_base"] - g.base = schemaview.namespaces()._base - if schemaview.namespaces()._base: - g.base = schemaview.namespaces()._base + if "_base" in namespaces: + namespaces._base = namespaces["_base"] + + if namespaces._base: + g.base = namespaces._base + self.inject_triples(element, schemaview, g) return g diff --git a/linkml_runtime/utils/namespaces.py b/linkml_runtime/utils/namespaces.py index a7232210..1dd7076b 100644 --- a/linkml_runtime/utils/namespaces.py +++ b/linkml_runtime/utils/namespaces.py @@ -78,12 +78,11 @@ def __setitem__(self, key, value): super().__setitem__(value, target_bnode) elif is_ncname(key): v = Namespace(str(value)) - if key in self: - if self[key] != v: - logging.getLogger('Namespaces').\ - warning(f"{key} namespace is already mapped to {self[key]} - Mapping to {v} ignored") - else: - super().__setitem__(key, v) + if key in self and self[key] != v: + logger = logging.getLogger('linkml_runtime.Namespaces') + logger.warning(f"{key} namespace is already mapped to {self[key]} - Overriding with mapping to {v}") + + super().__setitem__(key, v) else: raise ValueError(f"Invalid NCName: {key}") diff --git a/linkml_runtime/utils/schemaview.py b/linkml_runtime/utils/schemaview.py index b937964d..fc01bc34 100644 --- a/linkml_runtime/utils/schemaview.py +++ b/linkml_runtime/utils/schemaview.py @@ -179,10 +179,10 @@ def __hash__(self) -> int: def namespaces(self) -> Namespaces: namespaces = Namespaces() for s in self.schema_map.values(): - for prefix in s.prefixes.values(): - namespaces[prefix.prefix_prefix] = prefix.prefix_reference for cmap in self.schema.default_curi_maps: namespaces.add_prefixmap(cmap, include_defaults=False) + for prefix in s.prefixes.values(): + namespaces[prefix.prefix_prefix] = prefix.prefix_reference return namespaces def load_import(self, imp: str, from_schema: SchemaDefinition = None):