midpoint - go
This commit is contained in:
@@ -2,11 +2,9 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
from typing import Any, Protocol
|
||||
|
||||
import httpx
|
||||
from rdflib import Graph
|
||||
|
||||
from .settings import Settings
|
||||
|
||||
@@ -21,35 +19,6 @@ class SparqlEngine(Protocol):
|
||||
async def query_json(self, query: str) -> dict[str, Any]: ...
|
||||
|
||||
|
||||
class RdflibEngine:
|
||||
name = "rdflib"
|
||||
|
||||
def __init__(self, *, ttl_path: str, graph: Graph | None = None):
|
||||
self.ttl_path = ttl_path
|
||||
self.graph: Graph | None = graph
|
||||
|
||||
async def startup(self) -> None:
|
||||
if self.graph is not None:
|
||||
return
|
||||
g = Graph()
|
||||
g.parse(self.ttl_path, format="turtle")
|
||||
self.graph = g
|
||||
|
||||
async def shutdown(self) -> None:
|
||||
# Nothing to close for in-memory rdflib graph.
|
||||
return None
|
||||
|
||||
async def query_json(self, query: str) -> dict[str, Any]:
|
||||
if self.graph is None:
|
||||
raise RuntimeError("RdflibEngine not started")
|
||||
|
||||
result = self.graph.query(query)
|
||||
payload = result.serialize(format="json")
|
||||
if isinstance(payload, bytes):
|
||||
payload = payload.decode("utf-8")
|
||||
return json.loads(payload)
|
||||
|
||||
|
||||
class AnzoGraphEngine:
|
||||
name = "anzograph"
|
||||
|
||||
@@ -169,9 +138,5 @@ class AnzoGraphEngine:
|
||||
raise RuntimeError(f"AnzoGraph not ready at {self.endpoint}") from last_err
|
||||
|
||||
|
||||
def create_sparql_engine(settings: Settings, *, rdflib_graph: Graph | None = None) -> SparqlEngine:
|
||||
if settings.graph_backend == "rdflib":
|
||||
return RdflibEngine(ttl_path=settings.ttl_path, graph=rdflib_graph)
|
||||
if settings.graph_backend == "anzograph":
|
||||
return AnzoGraphEngine(settings=settings)
|
||||
raise RuntimeError(f"Unsupported GRAPH_BACKEND={settings.graph_backend!r}")
|
||||
def create_sparql_engine(settings: Settings) -> SparqlEngine:
|
||||
return AnzoGraphEngine(settings=settings)
|
||||
|
||||
Reference in New Issue
Block a user