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

Enhanced the use of namespaces #195

Merged
merged 24 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
01d82dd
Added persistent cache to Namespace and simplified the implementation.
jesper-friis Mar 10, 2024
16b5a4b
Updated triplestore interface
jesper-friis Mar 10, 2024
1c2d399
Added SPARQL test and improved tutorial
jesper-friis Mar 11, 2024
7491bf6
Added some pytest.importskip() statements
jesper-friis Mar 11, 2024
412125c
Also added pytest.importorskip("rdflib") to test_extend_namespace()
jesper-friis Mar 11, 2024
427821a
Improved wording
jesper-friis Mar 11, 2024
b74e757
Merge branch 'master' into 194-namespace-cache-and-extension
jesper-friis Mar 11, 2024
b4cc429
Fix CI pylint E1101 (no-member)
Mar 11, 2024
e74a6c8
Try to fix no-member by initializing Namespace
Mar 11, 2024
e8e147f
Fix private attribute
Mar 11, 2024
e676846
Readded `cachemode` and `triplestore_url` arguments with a deprecatio…
jesper-friis Mar 11, 2024
1f75ff6
Merge branch 'master' into 194-namespace-cache-and-extension
jesper-friis Mar 11, 2024
18702de
Update docs/tutorial.md
jesper-friis Mar 12, 2024
081f168
Update docs/tutorial.md
jesper-friis Mar 12, 2024
ae3b298
Update tests/test_namespace.py
jesper-friis Mar 12, 2024
56f7783
Merge branch 'master' into 194-namespace-cache-and-extension
jesper-friis Mar 12, 2024
7996158
As suggested by reviewer, readded the removed comment about the `trip…
jesper-friis Mar 13, 2024
0c91afb
Merge branch '194-namespace-cache-and-extension' of github.com:EMMC-A…
jesper-friis Mar 13, 2024
26b3931
Merge branch 'master' into 194-namespace-cache-and-extension
jesper-friis Mar 13, 2024
0863555
Improved formulation
jesper-friis Mar 13, 2024
e124866
Added more tests as required by reviewer
jesper-friis Mar 14, 2024
58e06d1
Updated new tests
jesper-friis Mar 14, 2024
d9bfece
Merge branch 'master' into 194-namespace-cache-and-extension
jesper-friis Mar 16, 2024
d3bc81a
Merge branch '194-namespace-cache-and-extension' of github.com:EMMC-A…
jesper-friis Mar 16, 2024
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
40 changes: 40 additions & 0 deletions tests/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Callable


# if True:
def test_namespaces() -> None:
"""Test namespaces."""
pytest.importorskip("rdflib")
Expand Down Expand Up @@ -56,6 +57,45 @@ def test_namespaces() -> None:

assert FOOD2.NonExisting == FOOD2 + "NonExisting"

# Save and reuse the cache
FOOD2._save_cache() # pylint: disable=protected-access
FOOD3 = Namespace(
"http://onto-ns.com/ontologies/examples/food#", check=True
)
assert FOOD3[name] == FOOD3 + name
assert FOOD3.Vegetable == FOOD3 + name
with pytest.raises(NoSuchIRIError):
FOOD3.NonExisting # pylint: disable=pointless-statement


# if True:
def test_triplestore_arg() -> None:
"""Test triplestore argument of Namespace.__init__()."""
pytest.importorskip("rdflib")
from tripper import RDF, Namespace, Triplestore
from tripper.errors import NamespaceError, NoSuchIRIError
from tripper.testutils import ontodir

assert str(RDF) == "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
assert RDF.type == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"

ts = Triplestore("rdflib")
ts.parse(ontodir / "family.ttl")

FAM = Namespace(
"http://onto-ns.com/ontologies/examples/family#",
check=True,
triplestore=ts,
)
assert FAM.Son == "http://onto-ns.com/ontologies/examples/family#Son"
assert FAM["Son"] == "http://onto-ns.com/ontologies/examples/family#Son"
assert FAM + "Son" == "http://onto-ns.com/ontologies/examples/family#Son"
with pytest.raises(NoSuchIRIError):
FAM.NonExisting # pylint: disable=pointless-statement

with pytest.raises(NamespaceError):
Namespace("http://example.com", check=True, triplestore=Ellipsis)


# if True:
def test_namespace_emmo():
Expand Down
22 changes: 22 additions & 0 deletions tests/test_triplestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,25 @@ def test_find_literal_triples() -> None:
(FAM.Per, FAM.hasName, Literal("Per")),
]
)


# if True:
def test_bind_errors():
"""Test for errors in Triplestore.bind()."""
pytest.importorskip("rdflib")

from tripper import Triplestore

ts = Triplestore(backend="rdflib", base_iri="http://example.com#")
EX = ts.bind("ex")
assert EX == "http://example.com#"
assert "ex" in ts.namespaces

ts.bind("ex", None)
assert "ex" not in ts.namespaces

ts2 = Triplestore(backend="rdflib")
with pytest.raises(ValueError):
ts2.bind("ex")
with pytest.raises(TypeError):
ts2.bind("ex", Ellipsis)
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,7 @@ def test_extend_namespace():

extend_namespace(FOOD, ontodir / "food-more.ttl")
assert FOOD.Fish == FOOD + "FOOD_90f5dd54_9e5c_46c9_824f_e10625a90c26"

EX = Namespace("http://example.com#")
with pytest.raises(TypeError):
extend_namespace(EX, {"Item": EX + "Item"})
4 changes: 1 addition & 3 deletions tripper/backends/rdflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@ class RdflibStrategy:

def __init__(
self,
base_iri: "Optional[str]" = None,
base_iri: "Optional[str]" = None, # pylint: disable=unused-argument
database: "Optional[str]" = None,
triplestore_url: "Optional[str]" = None,
format: "Optional[str]" = None, # pylint: disable=redefined-builtin
graph: "Graph" = None,
) -> None:
if base_iri:
warnings.warn("base_iri", UnusedArgumentWarning, stacklevel=3)
Comment on lines -75 to -76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this warning?

if database:
warnings.warn("database", UnusedArgumentWarning, stacklevel=3)

Expand Down
2 changes: 1 addition & 1 deletion tripper/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
):
# pylint: disable=redefined-builtin
if cachemode != -1:
warnings.warn(

Check warning on line 69 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L69

Added line #L69 was not covered by tests
"The `cachemode` argument of Triplestore.__init__() is "
"deprecated. Use `reload` instead (with `cachemode=NO_CACHE` "
"corresponding to `reload=True`).\n\n"
Expand All @@ -74,11 +74,11 @@
DeprecationWarning,
stacklevel=2,
)
if reload is None and cachemode == 0:
reload = True

Check warning on line 78 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L77-L78

Added lines #L77 - L78 were not covered by tests

if triplestore_url:
warnings.warn(

Check warning on line 81 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L81

Added line #L81 was not covered by tests
"The `triplestore_url` argument of Triplestore.__init__() is "
"deprecated. Use the `triplestore` argument instead (which "
"now accepts a string argument with the URL).\n\n"
Expand All @@ -86,8 +86,8 @@
DeprecationWarning,
stacklevel=2,
)
if triplestore is None:
triplestore = triplestore_url

Check warning on line 90 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L89-L90

Added lines #L89 - L90 were not covered by tests

if label_annotations is True:
label_annotations = (SKOS.prefLabel, RDF.label, SKOS.altLabel)
Expand Down Expand Up @@ -129,7 +129,7 @@
ts = Triplestore("rdflib")
ts.parse(triplestore, format=format)
elif isinstance(triplestore, Triplestore):
ts = triplestore

Check warning on line 132 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L132

Added line #L132 was not covered by tests
elif not isinstance(triplestore, Triplestore):
raise NamespaceError(
"If given, `triplestore` argument must be either a URL "
Expand Down Expand Up @@ -174,7 +174,7 @@
# pylint: disable=invalid-name
cachefile = self._get_cachefile()
if self._iris is None:
self._iris = {}

Check warning on line 177 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L177

Added line #L177 was not covered by tests
if cachefile.exists():
with open(cachefile, "rb") as f:
self._iris.update(pickle.load(f)) # nosec
Expand All @@ -192,7 +192,7 @@
return self.__getattr__(key)

def __repr__(self):
return f"Namespace({self._iri})"
return f"Namespace('{self._iri}')"

Check warning on line 195 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L195

Added line #L195 was not covered by tests

def __str__(self):
return self._iri
Expand Down Expand Up @@ -222,15 +222,15 @@
finaldir = None
if not site_cachedir:
if sys.platform.startswith("win32"):
site_cachedir = Path.home() / "AppData" / "Local"
finaldir = "Cache"

Check warning on line 226 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L225-L226

Added lines #L225 - L226 were not covered by tests
elif sys.platform.startswith("darwin"):
site_cachedir = Path.home() / "Library" / "Caches"

Check warning on line 228 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L228

Added line #L228 was not covered by tests
else: # Default to UNIX
site_cachedir = Path.home() / ".cache" # type: ignore
cachedir = Path(site_cachedir) / "tripper" # type: ignore
if finaldir:
cachedir /= finaldir

Check warning on line 233 in tripper/namespace.py

View check run for this annotation

Codecov / codecov/patch

tripper/namespace.py#L233

Added line #L233 was not covered by tests

if create:
path = Path(cachedir.root)
Expand Down
Loading