-
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
7 changed files
with
97 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""RDFProxy check runners.""" | ||
|
||
from collections.abc import Callable | ||
from typing import Annotated, NoReturn, TypeVar | ||
|
||
from rdfproxy.checks.query_checks import ( | ||
check_parse_query, | ||
check_select_query, | ||
check_solution_modifiers, | ||
) | ||
|
||
|
||
T = TypeVar("T") | ||
_TCheck = Callable[[T], T | NoReturn] | ||
|
||
|
||
def compose_left(*fns: Callable) -> Callable: | ||
def _left_wrapper(*fns): | ||
fn, *rest_fns = fns | ||
|
||
if rest_fns: | ||
return lambda *args, **kwargs: fn(_left_wrapper(*rest_fns)(*args, **kwargs)) | ||
return fn | ||
|
||
return _left_wrapper(*reversed(fns)) | ||
|
||
|
||
def compose_checker(*checkers: _TCheck) -> _TCheck: | ||
return compose_left(*checkers) | ||
|
||
|
||
check_query: Annotated[ | ||
_TCheck, "Run a series of checks on a query and return the query." | ||
] = compose_checker(check_parse_query, check_select_query, check_solution_modifiers) |
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,29 @@ | ||
"""Query checks definitions.""" | ||
|
||
from typing import NoReturn, TypeVar | ||
|
||
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, | ||
) | ||
|
||
TQuery = TypeVar("TQuery", bound=str) | ||
|
||
|
||
def check_parse_query(query: TQuery) -> TQuery | NoReturn: | ||
parseQuery(query) | ||
return query | ||
|
||
|
||
def check_select_query(query: TQuery) -> TQuery | NoReturn: | ||
if not query_is_select_query(query): | ||
raise UnsupportedQueryException("Only SELECT queries are applicable.") | ||
return query | ||
|
||
|
||
def check_solution_modifiers(query: TQuery) -> TQuery | NoReturn: | ||
if query_has_solution_modifiers(query): | ||
raise UnsupportedQueryException("SPARQL solution modifieres are not supported.") | ||
return 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
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) |