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

SPARQL result parsing #2796

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 11 additions & 12 deletions rdflib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,18 +579,6 @@ def plugins(
"rdflib.plugins.sparql.results.xmlresults",
"XMLResultParser",
)
register(
"application/sparql-results+xml; charset=UTF-8",
ResultParser,
"rdflib.plugins.sparql.results.xmlresults",
"XMLResultParser",
)
register(
"application/rdf+xml",
ResultParser,
"rdflib.plugins.sparql.results.graph",
"GraphResultParser",
)
register(
"json",
ResultParser,
Expand Down Expand Up @@ -627,3 +615,14 @@ def plugins(
"rdflib.plugins.sparql.results.tsvresults",
"TSVResultParser",
)

graph_parsers = {parser.name for parser in plugins(kind=Parser)}
result_parsers = {parser.name for parser in plugins(kind=ResultParser)}
graph_result_parsers = graph_parsers - result_parsers
for parser_name in graph_result_parsers:
register(
parser_name,
ResultParser,
"rdflib.plugins.sparql.results.graph",
"GraphResultParser",
)
42 changes: 28 additions & 14 deletions rdflib/plugins/stores/sparqlconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from urllib.parse import urlencode
from urllib.request import Request, urlopen

from rdflib.query import Result
from rdflib.plugin import plugins
from rdflib.query import Result, ResultParser
from rdflib.term import BNode
from rdflib.util import FORMAT_MIMETYPE_MAP, RESPONSE_TABLE_FORMAT_MIMETYPE_MAP

log = logging.getLogger(__name__)

Expand All @@ -22,16 +24,6 @@ class SPARQLConnectorException(Exception): # noqa: N818
pass


# TODO: Pull in these from the result implementation plugins?
_response_mime_types = {
"xml": "application/sparql-results+xml, application/rdf+xml",
"json": "application/sparql-results+json",
"csv": "text/csv",
"tsv": "text/tab-separated-values",
"application/rdf+xml": "application/rdf+xml",
}


class SPARQLConnector:
"""
this class deals with nitty gritty details of talking to a SPARQL server
Expand All @@ -41,7 +33,7 @@ def __init__(
self,
query_endpoint: Optional[str] = None,
update_endpoint: Optional[str] = None,
returnFormat: str = "xml", # noqa: N803
returnFormat: Optional[str] = None, # noqa: N803
method: te.Literal["GET", "POST", "POST_FORM"] = "GET",
auth: Optional[Tuple[str, str]] = None,
**kwargs,
Expand Down Expand Up @@ -95,7 +87,7 @@ def query(
if default_graph is not None and type(default_graph) is not BNode:
params["default-graph-uri"] = default_graph

headers = {"Accept": _response_mime_types[self.returnFormat]}
headers = {"Accept": self.response_mime_types()}

args = copy.deepcopy(self.kwargs)

Expand Down Expand Up @@ -170,7 +162,7 @@ def update(
params["using-named-graph-uri"] = named_graph

headers = {
"Accept": _response_mime_types[self.returnFormat],
"Accept": self.response_mime_types(),
"Content-Type": "application/sparql-update; charset=UTF-8",
}

Expand All @@ -188,5 +180,27 @@ def update(
)
)

def response_mime_types(self) -> str:
"""Construct a HTTP-Header Accept field to reflect the supported mime types.

If the return_format parameter is set, the mime types are restricted to these accordingly.
"""
sparql_format_mimetype_map = {
k: FORMAT_MIMETYPE_MAP.get(k, [])
+ RESPONSE_TABLE_FORMAT_MIMETYPE_MAP.get(k, [])
for k in list(FORMAT_MIMETYPE_MAP.keys())
+ list(RESPONSE_TABLE_FORMAT_MIMETYPE_MAP.keys())
}

supported_formats = set()
for plugin in plugins(name=self.returnFormat, kind=ResultParser):
if "/" not in plugin.name:
supported_formats.update(
sparql_format_mimetype_map.get(plugin.name, [])
)
else:
supported_formats.add(plugin.name)
return ", ".join(supported_formats)


__all__ = ["SPARQLConnector", "SPARQLConnectorException"]
2 changes: 1 addition & 1 deletion rdflib/plugins/stores/sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __init__(
sparql11: bool = True,
context_aware: bool = True,
node_to_sparql: _NodeToSparql = _node_to_sparql,
returnFormat: str = "xml", # noqa: N803
returnFormat: Optional[str] = None, # noqa: N803
auth: Optional[Tuple[str, str]] = None,
**sparqlconnector_kwargs,
):
Expand Down
61 changes: 41 additions & 20 deletions rdflib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,47 @@
_AnyT = TypeVar("_AnyT")


SUFFIX_FORMAT_MAP = {
"xml": "xml",
"rdf": "xml",
"owl": "xml",
"n3": "n3",
"ttl": "turtle",
"nt": "nt",
"trix": "trix",
"xhtml": "rdfa",
"html": "rdfa",
"svg": "rdfa",
"nq": "nquads",
"nquads": "nquads",
"trig": "trig",
"json": "json-ld",
"jsonld": "json-ld",
"json-ld": "json-ld",
}


FORMAT_MIMETYPE_MAP = {
"xml": ["application/rdf+xml"],
"n3": ["text/n3"],
"turtle": ["text/turtle"],
"nt": ["application/n-triples"],
"trix": ["application/trix"],
"rdfa": ["text/html", "application/xhtml+xml"],
"nquads": ["application/n-quads"],
"trig": ["application/trig"],
"json-ld": ["application/ld+json"],
}


RESPONSE_TABLE_FORMAT_MIMETYPE_MAP = {
"xml": ["application/sparql-results+xml"],
"json": ["application/sparql-results+json"],
"csv": ["text/csv"],
"tsv": ["text/tab-separated-values"],
}


def list2set(seq: Iterable[_HashableT]) -> List[_HashableT]:
"""
Return a new list without duplicates.
Expand Down Expand Up @@ -331,26 +372,6 @@ def parse_date_time(val: str) -> int:
return t


SUFFIX_FORMAT_MAP = {
"xml": "xml",
"rdf": "xml",
"owl": "xml",
"n3": "n3",
"ttl": "turtle",
"nt": "nt",
"trix": "trix",
"xhtml": "rdfa",
"html": "rdfa",
"svg": "rdfa",
"nq": "nquads",
"nquads": "nquads",
"trig": "trig",
"json": "json-ld",
"jsonld": "json-ld",
"json-ld": "json-ld",
}


def guess_format(fpath: str, fmap: Optional[Dict[str, str]] = None) -> Optional[str]:
"""
Guess RDF serialization based on file suffix. Uses
Expand Down
Loading