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 support for XPath #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion ferenda/documentrepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down