-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugin loading in `rdflib.plugins.sparql` will now happen similar to what is being done for `rdflib.plugins`. This also eliminates a warning that occurs `rdflib.plugins.sparql` and makes it possible to enable `warn_unused_ignores` without any exceptions or workarounds. Also: - Removed unused `type: ignore` statements.
- Loading branch information
Showing
11 changed files
with
252 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
test/plugins/parser/example/rdflib/plugin/parser/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import TYPE_CHECKING, Set, Tuple | ||
from rdflib.parser import Parser | ||
|
||
|
||
if TYPE_CHECKING: | ||
from rdflib.parser import InputSource | ||
from rdflib.graph import Graph | ||
from rdflib.namespace import Namespace | ||
from rdflib.term import Identifier | ||
|
||
|
||
class ExampleParser(Parser): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def parse(self, source: "InputSource", sink: "Graph"): | ||
for triple in self.constant_output(): | ||
sink.add(triple) | ||
|
||
@classmethod | ||
def namespace(cls) -> "Namespace": | ||
return Namespace("example:rdflib:plugin:parser:") | ||
|
||
@classmethod | ||
def constant_output(cls) -> Set[Tuple["Identifier", "Identifier", "Identifier"]]: | ||
return {(cls.namespace().subj, cls.namespace().pred, cls.namespace().obj)} | ||
|
||
from rdflib.namespace import Namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# https://setuptools.pypa.io/en/latest/userguide/declarative_config.html | ||
# https://setuptools.pypa.io/en/latest/references/keywords.html | ||
[metadata] | ||
name = example.rdflib.plugin.parser | ||
version = 0.0.0 | ||
|
||
[options] | ||
packages = find_namespace: | ||
|
||
[options.packages.find] | ||
include = | ||
example.rdflib.plugin.parser | ||
|
||
|
||
[options.entry_points] | ||
rdf.plugins.parser = | ||
example.rdflib.plugin.parser = example.rdflib.plugin.parser:ExampleParser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from setuptools import setup | ||
|
||
setup() |
34 changes: 34 additions & 0 deletions
34
test/plugins/sparqleval/example/rdflib/plugin/sparqleval/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Any | ||
|
||
|
||
def custom_eval_extended(ctx: Any, extend: Any) -> Any: | ||
for c in evalPart(ctx, extend.p): | ||
try: | ||
if hasattr(extend.expr, "iri") and extend.expr.iri == function_uri: | ||
evaluation = function_result | ||
else: | ||
evaluation = _eval(extend.expr, c.forget(ctx, _except=extend._vars)) | ||
if isinstance(evaluation, SPARQLError): | ||
raise evaluation | ||
|
||
yield c.merge({extend.var: evaluation}) | ||
|
||
except SPARQLError: | ||
yield c | ||
|
||
|
||
def custom_eval(ctx: Any, part: Any) -> Any: | ||
if part.name == "Extend": | ||
return custom_eval_extended(ctx, part) | ||
else: | ||
raise NotImplementedError() | ||
|
||
|
||
from rdflib import Namespace | ||
from rdflib.plugins.sparql.evaluate import evalPart | ||
from rdflib.plugins.sparql.evalutils import _eval | ||
from rdflib.plugins.sparql.sparql import SPARQLError | ||
|
||
namespace = Namespace("example:rdflib:plugin:sparqleval:") | ||
function_uri = namespace["function"] | ||
function_result = namespace["result"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# https://setuptools.pypa.io/en/latest/userguide/declarative_config.html | ||
# https://setuptools.pypa.io/en/latest/references/keywords.html | ||
[metadata] | ||
name = example.rdflib.plugin.sparqleval | ||
version = 0.0.0 | ||
|
||
[options] | ||
packages = find_namespace: | ||
|
||
[options.packages.find] | ||
include = | ||
example.rdflib.plugin.sparqleval | ||
|
||
|
||
[options.entry_points] | ||
rdf.plugins.sparqleval = | ||
example.rdflib.plugin.sparqleval = example.rdflib.plugin.sparqleval:custom_eval |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from setuptools import setup | ||
|
||
setup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import importlib | ||
import logging | ||
import shutil | ||
import subprocess | ||
import sys | ||
from contextlib import ExitStack, contextmanager | ||
from pathlib import Path | ||
from typing import Any, Callable, Dict, Generator, List | ||
|
||
from sphinx import warnings | ||
|
||
import rdflib.plugin | ||
import rdflib.plugins.sparql | ||
import rdflib.plugins.sparql.evaluate | ||
from rdflib import Graph | ||
from rdflib.parser import Parser | ||
|
||
TEST_DIR = Path(__file__).parent | ||
TEST_PLUGINS_DIR = TEST_DIR / "plugins" | ||
|
||
|
||
def del_key(d: Dict[Any, Any], key: Any) -> None: | ||
del d[key] | ||
|
||
|
||
@contextmanager | ||
def ctx_plugin(tmp_path: Path, plugin_src: Path) -> Generator[None, None, None]: | ||
base = tmp_path / f"{hash(plugin_src)}" | ||
pypath = (base / "pypath").absolute() | ||
plugpath = (base / "plugin").absolute() | ||
shutil.copytree(plugin_src, plugpath) | ||
logging.debug("Installing %s into %s", plugin_src, pypath) | ||
subprocess.run( | ||
[ | ||
sys.executable, | ||
"-m", | ||
"pip", | ||
"install", | ||
"--isolated", | ||
"--no-input", | ||
"--no-clean", | ||
"--no-index", | ||
"--disable-pip-version-check", | ||
"--target", | ||
f"{pypath}", | ||
f"{plugpath}", | ||
], | ||
check=True, | ||
) | ||
|
||
sys.path.append(f"{pypath}") | ||
|
||
yield None | ||
|
||
sys.path.remove(f"{pypath}") | ||
|
||
|
||
@contextmanager | ||
def ctx_cleaners() -> Generator[List[Callable[[], None]], None, None]: | ||
cleaners: List[Callable[[], None]] = [] | ||
yield cleaners | ||
for cleaner in cleaners: | ||
logging.debug("running cleaner %s", cleaner) | ||
cleaner() | ||
|
||
|
||
# Using no_cover as coverage freaks out and crashes because of what is happening here. | ||
def test_sparqleval(tmp_path: Path, no_cover: None) -> None: | ||
with ExitStack() as stack: | ||
stack.enter_context(ctx_plugin(tmp_path, TEST_PLUGINS_DIR / "sparqleval")) | ||
warnings_record = stack.enter_context(warnings.catch_warnings(record=True)) | ||
cleaners = stack.enter_context(ctx_cleaners()) | ||
|
||
ep_name = "example.rdflib.plugin.sparqleval" | ||
ep_ns = f'{ep_name.replace(".", ":")}:' | ||
plugin_module = importlib.import_module(ep_name) | ||
assert plugin_module.namespace == ep_ns | ||
|
||
importlib.reload(rdflib.plugins.sparql) | ||
importlib.reload(rdflib.plugins.sparql.evaluate) | ||
|
||
cleaners.insert(0, lambda: del_key(rdflib.plugins.sparql.CUSTOM_EVALS, ep_name)) | ||
|
||
logging.debug( | ||
"rdflib.plugins.sparql.CUSTOM_EVALS = %s", | ||
rdflib.plugins.sparql.CUSTOM_EVALS, | ||
) | ||
|
||
graph = Graph() | ||
query_string = ( | ||
"SELECT ?output1 WHERE { BIND(<" + ep_ns + "function>() AS ?output1) }" | ||
) | ||
logging.debug("query_string = %s", query_string) | ||
result = graph.query(query_string) | ||
assert result.type == "SELECT" | ||
rows = list(result) | ||
logging.debug("rows = %s", rows) | ||
assert len(rows) == 1 | ||
assert len(rows[0]) == 1 | ||
assert rows[0][0] == plugin_module.function_result | ||
assert [str(msg) for msg in warnings_record] == [] | ||
|
||
|
||
# Using no_cover as coverage freaks out and crashes because of what is happening here. | ||
def test_parser(tmp_path: Path, no_cover: None) -> None: | ||
with ExitStack() as stack: | ||
stack.enter_context(ctx_plugin(tmp_path, TEST_PLUGINS_DIR / "parser")) | ||
warnings_record = stack.enter_context(warnings.catch_warnings(record=True)) | ||
cleaners = stack.enter_context(ctx_cleaners()) | ||
|
||
ep_name = "example.rdflib.plugin.parser" | ||
ep_ns = f'{ep_name.replace(".", ":")}:' | ||
plugin_module = importlib.import_module(ep_name) | ||
assert plugin_module.ExampleParser.namespace() == ep_ns | ||
|
||
importlib.reload(rdflib.plugin) | ||
cleaners.insert(0, lambda: del_key(rdflib.plugin._plugins, (ep_name, Parser))) | ||
|
||
graph = Graph() | ||
assert len(graph) == 0 | ||
graph.parse(format=ep_name, data="") | ||
|
||
assert len(graph) > 0 | ||
triples = set(graph.triples((None, None, None))) | ||
logging.debug("triples = %s", triples) | ||
assert triples == plugin_module.ExampleParser.constant_output() | ||
assert [str(msg) for msg in warnings_record] == [] |