-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement query sanity checking
- Loading branch information
Showing
5 changed files
with
82 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""RDFProxy check runners.""" | ||
|
||
from collections.abc import Callable, Iterable | ||
from typing import Annotated, NoReturn, TypeVar | ||
|
||
from rdfproxy.checks.query_checks import ( | ||
check_parse_query, | ||
check_select_query, | ||
check_solution_modifiers, | ||
) | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
_TChecks = Iterable[Callable[[T], None | NoReturn]] | ||
|
||
|
||
def _check_factory(checks: _TChecks[T]) -> Callable[[T], T | NoReturn]: | ||
"""Produce an identity function that runs checks on its argument before returning.""" | ||
|
||
def _check(obj: T) -> T | NoReturn: | ||
for check in checks: | ||
check(obj) | ||
return obj | ||
|
||
return _check | ||
|
||
|
||
check_query: Annotated[ | ||
Callable[[str], str | NoReturn], | ||
"Run query checks and return the query unless an exception is raised.", | ||
] = _check_factory( | ||
checks=(check_parse_query, check_solution_modifiers, check_select_query) | ||
) |
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,24 @@ | ||
"""Query checks definitions.""" | ||
|
||
from typing import NoReturn | ||
|
||
from rdflib.plugins.sparql.parser import parseQuery | ||
from rdfproxy.utils._exceptions import UnsupportedQueryException | ||
from rdfproxy.utils.predicates import ( | ||
query_has_solution_modifiers, | ||
query_is_select_query, | ||
) | ||
|
||
|
||
def check_parse_query(query: str) -> None | NoReturn: | ||
parseQuery(query) | ||
|
||
|
||
def check_select_query(query: str) -> None | NoReturn: | ||
if not query_is_select_query(query): | ||
raise UnsupportedQueryException("Only SELECT queries are applicable.") | ||
|
||
|
||
def check_solution_modifiers(query: str) -> None | NoReturn: | ||
if query_has_solution_modifiers(query): | ||
raise UnsupportedQueryException("SPARQL solution modifieres are not supported.") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""RDFProxy predicate functions.""" | ||
|
||
import re | ||
|
||
from rdflib.plugins.sparql.parser import parseQuery | ||
|
||
|
||
def query_is_select_query(query: str) -> bool: | ||
"""Check if a SPARQL query is a SELECT query.""" | ||
_, query_type = parseQuery(query) | ||
return query_type.name == "SelectQuery" | ||
|
||
|
||
def query_has_solution_modifiers(query: str) -> bool: | ||
"""Predicate for checking if a SPARQL query has a solution modifier.""" | ||
pattern = r"}[^}]*\w+$" | ||
result = re.search(pattern, query) | ||
return bool(result) |