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

refactor: implement inject_subquery with re.search and str slicing #137

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions rdfproxy/utils/_exceptions.py
Original file line number Diff line number Diff line change
@@ -7,3 +7,7 @@ class MissingModelConfigException(Exception):

class UnboundGroupingKeyException(Exception):
"""Exception for indicating that no SPARQL binding corresponds to the requested grouping key."""


class QueryConstructionException(Exception):
"""Exception for indicating failed SPARQL query construction."""
19 changes: 8 additions & 11 deletions rdfproxy/utils/sparql_utils.py
Original file line number Diff line number Diff line change
@@ -4,10 +4,10 @@
from contextlib import contextmanager
from functools import partial
import re
from textwrap import indent
from typing import cast

from SPARQLWrapper import QueryResult, SPARQLWrapper
from rdfproxy.utils._exceptions import QueryConstructionException
from rdfproxy.utils._types import ItemsQueryConstructor, _TModelInstance


@@ -35,17 +35,14 @@ def replace_query_select_clause(query: str, repl: str) -> str:
return modified_query


def inject_subquery(
query: str, subquery: str, indent_depth: int = 4, indent_char: str = " "
) -> str:
"""Inject a SPARQL query with a subquery.
def inject_subquery(query: str, subquery: str) -> str:
"""Inject a SPARQL query with a subquery."""
if (tail := re.search(r"}[^}]*\Z", query)) is None:
raise QueryConstructionException("Unable to inject subquery.")

Also apply some basic indentation.
"""
indent_value = indent_char * indent_depth
indented_subquery = indent(f"\n{subquery}\n", indent_value)
indented_subclause = indent(f"\n{{{indented_subquery}}}", indent_value)
return re.sub(r".*\}$", f"{indented_subclause}\n}}", query)
tail_index: int = tail.start()
injected: str = f"{query[:tail_index]} {{{subquery}}} {query[tail_index:]}"
return injected


def construct_grouped_pagination_query(
Loading