Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: rdflib dumper ignores explicitly defined prefixes #348

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading