glowtables/glowtables/sparql.py

46 lines
1.5 KiB
Python

# 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()