Skip to content

Commit

Permalink
fix error in rdflib dumper where explicitly assigned prefixes are ign…
Browse files Browse the repository at this point in the history
…ored
  • Loading branch information
sneakers-the-rat committed Nov 9, 2024
1 parent ad74966 commit a09381f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
24 changes: 12 additions & 12 deletions linkml_runtime/dumpers/rdflib_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 5 additions & 6 deletions linkml_runtime/utils/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
4 changes: 2 additions & 2 deletions linkml_runtime/utils/schemaview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a09381f

Please sign in to comment.