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

Namespace unbind #1985

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion docs/sphinx-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
git+https://github.com/gniezen/n3pygments.git
myst-parser
sphinx<5
sphinx<6
sphinxcontrib-apidoc
sphinxcontrib-kroki
sphinx-autodoc-typehints
12 changes: 11 additions & 1 deletion rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,13 @@ def qname(self, uri):
def compute_qname(self, uri, generate=True):
return self.namespace_manager.compute_qname(uri, generate)

def bind(self, prefix, namespace, override=True, replace=False) -> None:
def bind(
self,
prefix: str,
namespace: str, # noqa: F811
override=True,
replace=False,
) -> None:
"""Bind prefix to namespace

If override is True will bind namespace to given prefix even
Expand All @@ -1069,6 +1075,10 @@ def bind(self, prefix, namespace, override=True, replace=False) -> None:
prefix, namespace, override=override, replace=replace
)

def unbind(self, prefix: str) -> None:
"""Delete binding of prefix to namespace"""
return self.namespace_manager.unbind(prefix)

def namespaces(self):
"""Generator over all the prefix, namespace tuples"""
for prefix, namespace in self.namespace_manager.namespaces():
Expand Down
8 changes: 7 additions & 1 deletion rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,16 @@ def expand_curie(self, curie: str) -> Union[URIRef, None]:
f"Prefix \"{curie.split(':')[0]}\" not bound to any namespace."
)

def unbind(self, prefix: str) -> None:
for ns in self.namespaces():
if ns[0] == prefix:
del self.__trie[str(ns[1])]
self.store.unbind(prefix)

def bind(
self,
prefix: Optional[str],
namespace: Any,
namespace: str,
override: bool = True,
replace: bool = False,
) -> None:
Expand Down
3 changes: 3 additions & 0 deletions rdflib/plugins/stores/auditable.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def contexts(self, triple=None):
def bind(self, prefix, namespace, override=True):
self.store.bind(prefix, namespace, override=override)

def unbind(self, prefix):
self.store.unbind(prefix)

def prefix(self, namespace):
return self.store.prefix(namespace)

Expand Down
44 changes: 29 additions & 15 deletions rdflib/plugins/stores/berkeleydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from os import mkdir
from os.path import abspath, exists
from threading import Thread
from typing import Optional
from urllib.request import pathname2url

from rdflib.store import NO_STORE, VALID_STORE, Store
Expand Down Expand Up @@ -467,34 +468,47 @@ def __len__(self, context=None):
cursor.close()
return count

def bind(self, prefix, namespace, override=True):
prefix = prefix.encode("utf-8")
namespace = namespace.encode("utf-8")
bound_prefix = self.__prefix.get(namespace)
bound_namespace = self.__namespace.get(prefix)
def bind(
self,
prefix: Optional[str],
namespace: str,
override: bool = True,
) -> None:
pfx = prefix.encode("utf-8") if prefix is not None else "".encode("utf-8")
ns = namespace.encode("utf-8")

bound_prefix = self.__prefix.get(ns)
bound_namespace = self.__namespace.get(pfx)
if override:
if bound_prefix:
self.__namespace.delete(bound_prefix)
if bound_namespace:
self.__prefix.delete(bound_namespace)
self.__prefix[namespace] = prefix
self.__namespace[prefix] = namespace
self.__prefix[ns] = pfx
self.__namespace[pfx] = ns
else:
self.__prefix[bound_namespace or namespace] = bound_prefix or prefix
self.__namespace[bound_prefix or prefix] = bound_namespace or namespace
self.__prefix[bound_namespace or ns] = bound_prefix or pfx
self.__namespace[bound_prefix or pfx] = bound_namespace or ns

def unbind(self, prefix: str) -> None:
pfx = prefix.encode("utf-8") if prefix is not None else "".encode("utf-8")
ns = self.__namespace.get(pfx, None)
if ns is not None:
self.__namespace.delete(pfx)
self.__prefix.delete(ns)

def namespace(self, prefix):
prefix = prefix.encode("utf-8")
ns = self.__namespace.get(prefix, None)
pfx = prefix.encode("utf-8")
ns = self.__namespace.get(pfx, None)
if ns is not None:
return URIRef(ns.decode("utf-8"))
return None

def prefix(self, namespace):
namespace = namespace.encode("utf-8")
prefix = self.__prefix.get(namespace, None)
if prefix is not None:
return prefix.decode("utf-8")
ns = namespace.encode("utf-8")
pfx = self.__prefix.get(ns, None)
if pfx is not None:
return pfx.decode("utf-8")
return None

def namespaces(self):
Expand Down
12 changes: 12 additions & 0 deletions rdflib/plugins/stores/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ def bind(self, prefix, namespace, override=True):
bound_namespace, namespace
)

def unbind(self, prefix: str) -> None:
namespace = self.__namespace.get(prefix, None)
if namespace is not None:
del self.__prefix[namespace]
del self.__namespace[prefix]

def namespace(self, prefix):
return self.__namespace.get(prefix, None)

Expand Down Expand Up @@ -440,6 +446,12 @@ def bind(self, prefix, namespace, override=True):
bound_namespace, namespace
)

def unbind(self, prefix):
namespace = self.__namespace.get(prefix, None)
if namespace is not None:
del self.__prefix[namespace]
del self.__namespace[prefix]

def namespace(self, prefix):
return self.__namespace.get(prefix, None)

Expand Down
3 changes: 3 additions & 0 deletions rdflib/plugins/stores/regexmatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ def remove_context(self, identifier):
def bind(self, prefix, namespace, override=True):
self.storage.bind(prefix, namespace, override=override)

def unbind(self, prefix):
self.storage.unbind(prefix)

def prefix(self, namespace):
return self.storage.prefix(namespace)

Expand Down
5 changes: 5 additions & 0 deletions rdflib/plugins/stores/sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ def bind(self, prefix, namespace, override=True):
del self.nsBindings[bound_prefix]
self.nsBindings[prefix] = namespace

def unbind(self, prefix):
ns = self.nsBindings.get(prefix, None)
if ns is not None:
del self.nsBindings[prefix]

def prefix(self, namespace):
""" """
return dict([(v, k) for k, v in self.nsBindings.items()]).get(namespace)
Expand Down
12 changes: 10 additions & 2 deletions rdflib/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,20 @@ def update(self, update, initNs, initBindings, queryGraph, **kwargs): # noqa: N

# Optional Namespace methods

def bind(self, prefix: str, namespace: "URIRef", override: bool = True) -> None:
def bind(
self,
prefix: Optional[str],
namespace: str,
override: bool = True,
) -> None:
"""
:param override: rebind, even if the given namespace is already bound to another prefix.
"""

def prefix(self, namespace: "URIRef") -> Optional["str"]:
def unbind(self, prefix: str) -> None:
""""""

def prefix(self, namespace: str) -> Optional["str"]:
""""""

def namespace(self, prefix: str) -> Optional["URIRef"]:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"tests": kwargs["tests_require"],
"docs": [
"myst-parser",
"sphinx < 5",
"sphinx < 6",
"sphinxcontrib-apidoc",
"sphinxcontrib-kroki",
"sphinx-autodoc-typehints",
Expand Down
79 changes: 79 additions & 0 deletions test/test_store/test_namespace_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,82 @@ def test_rebind_prefix_reuse_uri_replace(
graph.bind("egsub", EGNSSUB_V1, override=True, replace=True)
graph.bind("egsubv0", EGNSSUB_V0, override=reuse_override, replace=reuse_replace)
check_ns(graph, {"egsub": EGNSSUB_V1, "egsubv0": EGNSSUB_V0})


def test_graph_bind_zero_args(tmp_path: Path, store_name: str) -> None:
graph = make_graph(tmp_path, store_name)
with pytest.raises(
TypeError,
match=r"bind\(\) missing 2 required positional arguments: 'prefix' and 'namespace'",
):
# Missing positional arguments "prefix", "namespace" in call to "bind" of "Graph"
graph.bind() # type: ignore[call-arg]


def test_graph_bind_to_none(tmp_path: Path, store_name: str) -> None:
graph = make_graph(tmp_path, store_name)
graph.bind("", EGNSSUB_V0)
check_ns(graph, {"": EGNSSUB_V0})

# Argument 2 to "bind" of "Graph" has incompatible type "None"; expected "Union[URIRef, Namespace]"
graph.bind("", None) # type: ignore[arg-type]

assert list(graph.namespaces()) == [
("", URIRef("http://example.com/sub/v0")),
("default1", URIRef("None")),
]


def test_graph_bind_prefix_to_none(tmp_path: Path, store_name: str) -> None:
graph = make_graph(tmp_path, store_name)
# Argument 2 to "bind" of "Graph" has incompatible type "None"; expected "Union[URIRef, Namespace]"
graph.bind("egsub", None) # type: ignore[arg-type]
check_ns(graph, {"egsub": URIRef("None")})


def test_unbind_arity(tmp_path: Path, store_name: str) -> None:
"""
Check TypeError raised on missing prefix
"""
graph = make_graph(tmp_path, store_name)
graph.bind("egsub", EGNSSUB_V0)
check_ns(graph, {"egsub": EGNSSUB_V0})
with pytest.raises(TypeError):
graph.unbind() # type: ignore[call-arg]
graph.unbind(None) # type: ignore[arg-type]
check_ns(graph, {"egsub": EGNSSUB_V0})


def test_simple_unbind(tmp_path: Path, store_name: str) -> None:
"""
Straightforward bind / unbind.
"""
graph = make_graph(tmp_path, store_name)
graph.bind("egsub", EGNSSUB_V0)
check_ns(graph, {"egsub": EGNSSUB_V0})
graph.unbind("egsub")
check_ns(graph, {})


def test_owl_unbind(tmp_path: Path, store_name: str) -> None:
"""
Unbind core prefix=>namespace
"""
graph = make_graph(tmp_path, store_name)
graph.unbind("owl")
assert ("owl", URIRef("http://www.w3.org/2002/07/owl#")) not in graph.namespaces()
assert (
"owl",
URIRef("http://www.w3.org/2002/07/owl#"),
) not in graph.store.namespaces()


def test_default_unbind(tmp_path: Path, store_name: str) -> None:
"""
Bind / unbind default
"""
graph = make_graph(tmp_path, store_name)
graph.bind("", EGNSSUB_V0)
check_ns(graph, {"": EGNSSUB_V0})
graph.unbind("")
check_ns(graph, {})