# -*- coding: utf-8 -*- # """ This is an RDFLib store around Ivan Herman et al.'s SPARQL service wrapper. This was first done in layer-cake, and then ported to RDFLib """ # Defines some SPARQL keywords LIMIT = 'LIMIT' OFFSET = 'OFFSET' ORDERBY = 'ORDER BY' import re # import warnings try: from SPARQLWrapper import SPARQLWrapper, XML, POST, GET, URLENCODED, POSTDIRECTLY except ImportError: raise Exception( "SPARQLWrapper not found! SPARQL Store will not work." + "Install with 'easy_install SPARQLWrapper'") import sys if getattr(sys, 'pypy_version_info', None) is not None \ or sys.platform.startswith('java') \ or sys.version_info[:2] < (2, 6): # import elementtree as etree from elementtree import ElementTree assert ElementTree else: try: from xml.etree import ElementTree assert ElementTree except ImportError: from elementtree import ElementTree from rdflib.plugins.stores.regexmatching import NATIVE_REGEX from rdflib.store import Store from rdflib.query import Result from rdflib import Variable, Namespace, BNode, URIRef, Literal from rdflib.graph import DATASET_DEFAULT_GRAPH_ID import httplib import urlparse class NSSPARQLWrapper(SPARQLWrapper): nsBindings = {} def setNamespaceBindings(self, bindings): """ A shortcut for setting namespace bindings that will be added to the prolog of the query @param bindings: A dictionary of prefixs to URIs """ self.nsBindings.update(bindings) def setQuery(self, query): """ Set the SPARQL query text. Note: no check is done on the validity of the query (syntax or otherwise) by this module, except for testing the query type (SELECT, ASK, etc). Syntax and validity checking is done by the SPARQL service itself. @param query: query text @type query: string @bug: #2320024 """ self.queryType = self._parseQueryType(query) self.queryString = self.injectPrefixes(query) def injectPrefixes(self, query): return '\n'.join( ['\n'.join(['PREFIX %s: <%s>' % (key, val) for key, val in self.nsBindings.items()]), query]) BNODE_IDENT_PATTERN = re.compile('(?P