Skip to content

Commit 1503dae

Browse files
authored
Merge pull request #872 from RDFLib/sparqlstore_improvements
rename new SPARQLWrapper to SPARQLConnector
2 parents f2d8931 + 990fd4d commit 1503dae

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

rdflib/plugins/stores/sparqlwrapper.py renamed to rdflib/plugins/stores/sparqlconnector.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
log = logging.getLogger(__name__)
1212

1313

14-
class SPARQLWrapperException(Exception):
14+
class SPARQLConnectorException(Exception):
1515
pass
1616

17-
17+
# TODO: Pull in these from the result implementation plugins?
1818
_response_mime_types = {
1919
'xml': 'application/sparql-results+xml, application/rdf+xml',
20+
'json': 'application/sparql-results+json',
21+
'csv': 'text/csv',
22+
'tsv': 'text/tab-separated-values',
23+
'application/rdf+xml': 'application/rdf+xml',
2024
}
2125

2226

23-
class SPARQLWrapper(object):
27+
class SPARQLConnector(object):
2428

2529
"""
2630
this class deals with nitty gritty details of talking to a SPARQL server
@@ -56,14 +60,14 @@ def method(self):
5660
@method.setter
5761
def method(self, method):
5862
if method not in ('GET', 'POST'):
59-
raise SPARQLWrapperException('Method must be "GET" or "POST"')
63+
raise SPARQLConnectorException('Method must be "GET" or "POST"')
6064

6165
self._method = method
6266

6367
def query(self, query, default_graph=None):
6468

6569
if not self.query_endpoint:
66-
raise SPARQLWrapperException("Query endpoint not set!")
70+
raise SPARQLConnectorException("Query endpoint not set!")
6771

6872
params = {'query': query}
6973
if default_graph:
@@ -85,7 +89,7 @@ def query(self, query, default_graph=None):
8589
elif self.method == 'POST':
8690
args['data'] = params
8791
else:
88-
raise SPARQLWrapperException("Unknown method %s" % self.method)
92+
raise SPARQLConnectorException("Unknown method %s" % self.method)
8993

9094
res = self.session.request(self.method, **args)
9195

@@ -95,7 +99,7 @@ def query(self, query, default_graph=None):
9599

96100
def update(self, update, default_graph=None):
97101
if not self.update_endpoint:
98-
raise SPARQLWrapperException("Query endpoint not set!")
102+
raise SPARQLConnectorException("Query endpoint not set!")
99103

100104
params = {}
101105

rdflib/plugins/stores/sparqlstore.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import re
1515
import collections
1616

17-
from .sparqlwrapper import SPARQLWrapper
17+
from .sparqlconnector import SPARQLConnector
1818

1919
from rdflib.plugins.stores.regexmatching import NATIVE_REGEX
2020

@@ -37,11 +37,10 @@ def _node_to_sparql(node):
3737
return node.n3()
3838

3939

40-
class SPARQLStore(SPARQLWrapper, Store):
41-
"""
42-
An RDFLib store around a SPARQL endpoint
40+
class SPARQLStore(SPARQLConnector, Store):
41+
"""An RDFLib store around a SPARQL endpoint
4342
44-
This is in theory context-aware and should work as expected
43+
This is context-aware and should work as expected
4544
when a context is specified.
4645
4746
For ConjunctiveGraphs, reading is done from the "default graph". Exactly
@@ -51,7 +50,7 @@ class SPARQLStore(SPARQLWrapper, Store):
5150
motivated by the SPARQL 1.1.
5251
5352
Fuseki/TDB has a flag for specifying that the default graph
54-
is the union of all graphs (tdb:unionDefaultGraph in the Fuseki config).
53+
is the union of all graphs (``tdb:unionDefaultGraph`` in the Fuseki config).
5554
5655
.. warning:: By default the SPARQL Store does not support blank-nodes!
5756
@@ -61,9 +60,9 @@ class SPARQLStore(SPARQLWrapper, Store):
6160
6261
See http://www.w3.org/TR/sparql11-query/#BGPsparqlBNodes
6362
64-
You can make use of such extensions through the node_to_sparql and
65-
node_from_result arguments. For example if you want to transform
66-
BNode('0001') into "<bnode:b0001>", you can use a function like this:
63+
You can make use of such extensions through the ``node_to_sparql``
64+
argument. For example if you want to transform BNode('0001') into
65+
"<bnode:b0001>", you can use a function like this:
6766
6867
>>> def my_bnode_ext(node):
6968
... if isinstance(node, BNode):
@@ -72,6 +71,22 @@ class SPARQLStore(SPARQLWrapper, Store):
7271
>>> store = SPARQLStore('http://dbpedia.org/sparql',
7372
... node_to_sparql=my_bnode_ext)
7473
74+
You can request a particular result serialization with the
75+
``returnFormat`` parameter. This is a string that must have a
76+
matching plugin registered. Built in is support for ``xml``,
77+
``json``, ``csv``, ``tsv`` and ``application/rdf+xml``.
78+
79+
The underlying SPARQLConnector builds in the requests library.
80+
Any extra kwargs passed to the SPARQLStore connector are passed to
81+
requests when doing HTTP calls. I.e. you have full control of
82+
cookies/auth/headers.
83+
84+
Form example:
85+
86+
>>> store = SPARQLStore('...my endpoint ...', auth=('user','pass'))
87+
88+
will use HTTP basic auth.
89+
7590
"""
7691
formula_aware = False
7792
transaction_aware = False
@@ -83,11 +98,11 @@ def __init__(self,
8398
sparql11=True, context_aware=True,
8499
node_to_sparql=_node_to_sparql,
85100
returnFormat='xml',
86-
**sparqlwrapper_kwargs):
101+
**sparqlconnector_kwargs):
87102
"""
88103
"""
89104
super(SPARQLStore, self).__init__(
90-
endpoint, returnFormat=returnFormat, **sparqlwrapper_kwargs)
105+
endpoint, returnFormat=returnFormat, **sparqlconnector_kwargs)
91106

92107
self.node_to_sparql = node_to_sparql
93108
self.nsBindings = {}
@@ -353,7 +368,7 @@ def _is_contextual(self, graph):
353368
return graph.identifier != DATASET_DEFAULT_GRAPH_ID
354369

355370
def close(self, commit_pending_transaction=None):
356-
SPARQLWrapper.close(self)
371+
SPARQLConnector.close(self)
357372

358373

359374
class SPARQLUpdateStore(SPARQLStore):
@@ -588,7 +603,7 @@ def _update(self, update):
588603

589604
self._updates += 1
590605

591-
SPARQLWrapper.update(self, update)
606+
SPARQLConnector.update(self, update)
592607

593608
def update(self, query,
594609
initNs={},

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ flake8
22
html5lib
33
isodate
44
pyparsing
5+
requests
56
six
67
doctest-ignore-unicode

0 commit comments

Comments
 (0)