Define basic architecture and pytest fixtures for SPARQL querying

This commit is contained in:
Val Lorentz 2023-05-21 14:04:44 +02:00
parent 5ab41939f2
commit 3a6d306fbf
3 changed files with 85 additions and 0 deletions

45
glowtables/sparql.py Normal file
View File

@ -0,0 +1,45 @@
# This file is part of the Glowtables software
# Copyright (C) 2023 Valentin Lorentz
#
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License version 3, as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
"""Abstraction over SPARQL backends, primarily meant to be mocked by tests."""
import abc
from typing import Iterable
import SPARQLWrapper
class SparqlBackend(abc.ABC):
"""Abstract class for SPARQL clients"""
@abc.abstractmethod
def query(self, query: str) -> Iterable[tuple]:
"""Sends a SPARQL query, and returns an iterable of results."""
class RemoteSparqlBackend(SparqlBackend):
"""Queries a SPARQL API over HTTP."""
def __init__(self, url: str, agent: str):
"""
:param url: Base URL of the endpoint
:param agent: User-Agent to use in HTTP requests
"""
self._url = url
self._agent = agent
def query(self, query: str) -> Iterable[tuple]:
sparql = SPARQLWrapper.SPARQLWrapper(self._url, agent=self._agent)
sparql.setQuery(query)
return sparql.query()

View File

@ -0,0 +1,39 @@
# This file is part of the Glowtables software
# Copyright (C) 2023 Valentin Lorentz
#
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License version 3, as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
from typing import Iterable
import pytest
import rdflib
from glowtables.sparql import SparqlBackend
class RdflibSparqlBackend(SparqlBackend):
def __init__(self, graph: rdflib.Graph):
self._graph = graph
def query(self, query: str) -> Iterable[tuple]:
ret = self._graph.query(query)
return ret
@pytest.fixture()
def rdflib_graph():
return rdflib.Graph()
@pytest.fixture()
def rdflib_sparql(rdflib_graph: rdflib.Graph):
return RdflibSparqlBackend(rdflib_graph)

View File

@ -29,6 +29,7 @@ python_version = "3.9"
[[tool.mypy.overrides]]
module = [
"requests_mock",
"SPARQLWrapper",
]
ignore_missing_imports = true