From 072b921a800e155824c1cc347bb3aa41b5ecf6de Mon Sep 17 00:00:00 2001 From: Egil Date: Mon, 23 Aug 2021 23:53:38 +0200 Subject: [PATCH] SPARQL support for XPath --- ferenda/documentrepository.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/ferenda/documentrepository.py b/ferenda/documentrepository.py index 4f22a080..f97d9e8b 100644 --- a/ferenda/documentrepository.py +++ b/ferenda/documentrepository.py @@ -60,11 +60,39 @@ UnorderedList, ListItem, Paragraph) from ferenda.elements.html import elements_from_soup from ferenda.documentstore import RelateNeeded + +import contextlib + # establish two central RDF Namespaces at the top level DCTERMS = Namespace(util.ns['dcterms']) PROV = Namespace(util.ns['prov']) +class RDFQuery(object): + store = None + + @classmethod + @contextlib.contextmanager + def enable(cls, config): + cls.store = TripleStore.connect(config.storetype, + config.storelocation, + config.storerepository) + yield + cls.store.close() + + @classmethod + def rdf_query(cls, dummy, query, *arg): + res = json.loads(cls.store.select(query.replace("[", "<").replace("]", ">") % arg, format="json")) + + return [etree.Element("{http://lagen.nu/xslt}SparqlResult", + {key: value["value"] + for key, value in binding.items()}) + for binding in res["results"]["bindings"]] + +ns = etree.FunctionNamespace("http://lagen.nu/xslt") +ns['sparql'] = RDFQuery.rdf_query + + class DocumentRepository(object): """Base class for handling a repository of documents. @@ -2509,7 +2537,8 @@ def generate(self, basefile, version=None, otherrepos=[], needed=True): # constructed with the depth argument to # transform_file depth = urlparse(self.canonical_uri(basefile, version)).path[1:-1].count("/") - transformer.transform_file(infile, outfile, params, depth=depth) + with RDFQuery.enable(self.config): + transformer.transform_file(infile, outfile, params, depth=depth) # At this point, outfile may appear untouched if it already # existed and wasn't actually changed. But this will cause the