sparql-wrapper-python-1.4.1/0000755000175000017500000000000011361100677014760 5ustar nachonachosparql-wrapper-python-1.4.1/PKG-INFO0000644000175000017500000000155311335213040016047 0ustar nachonachoMetadata-Version: 1.1 Name: SPARQLWrapper Version: 1.4.1 Summary: SPARQL Endpoint interface to Python Home-page: http://sparql-wrapper.sourceforge.net/ Author: Ivan Herman, Sergio Fernandez, Carlos Tejo Alonso Author-email: ivan at ivan-herman net, sergio.fernandez at fundacionctic org, carlos.tejo at fundacionctic org License: W3C SOFTWARE NOTICE AND LICENSE Download-URL: http://sourceforge.net/projects/sparql-wrapper/files Description: This is a wrapper around a SPARQL service. It helps in creating the query URI and, possibly, convert the result into a more manageable format. Keywords: python SPARQL Platform: any Classifier: Development Status :: 4 - Beta Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: W3C License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 2.5 Requires: rdflib sparql-wrapper-python-1.4.1/AUTHORS.txt0000644000175000017500000000065011335210152016636 0ustar nachonacho Authors ------- Ivan Herman Sergio Fernández Carlos Tejo Contributors ------------ Obey Arthur Liu Christopher Lenz www.cmlenz.net - Feature to allow developers to choose the json module PÄ“teris Caune - Great feedback and patches sparql-wrapper-python-1.4.1/scripts/0000755000175000017500000000000011361100677016447 5ustar nachonachosparql-wrapper-python-1.4.1/scripts/sparql.py0000755000175000017500000000352211207744210020324 0ustar nachonacho#!/usr/bin/python # -*- coding: utf-8 -*- from SPARQLWrapper import SPARQLWrapper2, SPARQLWrapper, TURTLE import sys, getopt localSparqler = "http://localhost:2020/sparql" localVirtuoso = "http://localhost:8890/sparql" def main(server,query,sponge=False) : sparql = SPARQLWrapper2(server) if sponge : sparql.addExtraURITag("should-sponge","grab-everything") sparql.setQuery(query) res = sparql.query() variables = res.variables print "Variables:" print variables print print "Bindings:" for b in res.bindings : for v in res.variables : try : val = b[v] if val.lang : str = "%s: %s@%s" % (v,val.value,val.lang) elif val.datatype : str = "%s: %s^^%s" % (v,val.value,val.datatype) else : str = "%s: %s" % (v,val.value) except KeyError : # no binding to that one... str = "%s: <>" % v print str.encode('utf-8') print # ------------------------------------------------------------------------------------------------------------- server = localSparqler query = "" sponge = False usagetxt="""%s [-s] [-u url] [file] -s: use local sparqler (default) -v: use local virtuoso -u url: server url -p: issue an extra sponge for virtuoso file: sparql query file """ def usage() : print usagetxt % sys.argv[0] sys.exit(1) if __name__ == '__main__' : if len(sys.argv) == 1: usage() try : opts,args = getopt.getopt(sys.argv[1:],"shu:pv") for o,a in opts : if o == "-s" : server = localSparqler elif o == "-v" : server = localVirtuoso sponge = True elif o == "-h" : print usage sys.exit(0) elif o == "-u" : server = a elif o == "-p" : sponge = True if query == "" and len(args) > 0 : inp = file(args[0]) query = "" for l in inp : query += l except : usage() if query == "" : usage() main(server,query,sponge) sparql-wrapper-python-1.4.1/scripts/example.py0000644000175000017500000000157711274322666020475 0ustar nachonacho#!/usr/bin/python # -*- coding: utf-8 -*- from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF sparql = SPARQLWrapper("http://dbpedia.org/sparql") sparql.setQuery(""" PREFIX rdfs: SELECT ?label WHERE { rdfs:label ?label } """) # JSON example print '\n\n*** JSON Example' sparql.setReturnFormat(JSON) results = sparql.query().convert() for result in results["results"]["bindings"]: print result["label"]["value"] # XML example print '\n\n*** XML Example' sparql.setReturnFormat(XML) results = sparql.query().convert() print results.toxml() # N3 example print '\n\n*** N3 Example' sparql.setReturnFormat(N3) results = sparql.query().convert() print results # RDF example print '\n\n*** RDF Example' sparql.setReturnFormat(RDF) results = sparql.query().convert() print results.serialize() sparql-wrapper-python-1.4.1/SPARQLWrapper/0000755000175000017500000000000011361100677017323 5ustar nachonachosparql-wrapper-python-1.4.1/SPARQLWrapper/SPARQLExceptions.py0000644000175000017500000000236011207744210022736 0ustar nachonacho# -*- coding: utf-8 -*- """ SPARQL Wrapper exceptions @authors: U{Ivan Herman}, U{Sergio Fernández}, U{Carlos Tejo Alonso} @organization: U{World Wide Web Consortium} and U{Foundation CTIC}. @license: U{W3C® SOFTWARE NOTICE AND LICENSE} """ import exceptions class SPARQLWrapperException(exceptions.Exception): """ Base class for SPARQL Wrapper exceptions """ def __init__(self): Exception.__init__(self) def __str__(self): return "SPARQLWrapperException: an exception has occured" class QueryBadFormed(SPARQLWrapperException): """ Query Bad Formed exceptions """ def __init__(self): SPARQLWrapperException.__init__(self) def __str__(self): return "QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed" class EndPointNotFound(SPARQLWrapperException): """ End Point Not Found exceptions """ def __init__(self): SPARQLWrapperException.__init__(self) def __str__(self): return "EndPointNotFound: it was impossible to connect with the endpoint in that address, check if it is correct" sparql-wrapper-python-1.4.1/SPARQLWrapper/KeyInsensitiveDict.py0000644000175000017500000000230511311420412023435 0ustar nachonacho# -*- coding: utf-8 -*- """ A simple implementation of a key-insensitive dictionary (implemented by using the pattern decorator). @authors: U{Ivan Herman}, U{Sergio Fernández}, U{Carlos Tejo Alonso} @organization: U{World Wide Web Consortium} and U{Foundation CTIC}. @license: U{W3C® SOFTWARE NOTICE AND LICENSE} """ class KeyInsensitiveDict: """ A simple implementation of a key-insensitive dictionary (implemented by using the pattern decorator). """ def __init__(self, d={}): self.__dict__["d"] = {} for k, v in d.items(): self[k] = v def __getattr__(self, attr): return getattr(self.__dict__["d"], attr) def __setattr__(self, attr, value): setattr(self.__dict__["d"], attr, value) def __setitem__(self, key, value): if (hasattr(key, "lower")): key = key.lower() self.__dict__["d"][key] = value def __getitem__(self, key): if (hasattr(key, "lower")): key = key.lower() return self.__dict__["d"][key] sparql-wrapper-python-1.4.1/SPARQLWrapper/jsonlayer.py0000644000175000017500000001303311253426246021706 0ustar nachonacho# -*- coding: utf-8 -*- # # Copyright (C) 2009 Christopher Lenz # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # 3. The name of the author may not be used to endorse or promote # products derived from this software without specific prior # written permission. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """Thin abstraction layer over the different available modules for decoding and encoding JSON data. This module currently supports the following JSON modules: - ``simplejson``: http://code.google.com/p/simplejson/ - ``cjson``: http://pypi.python.org/pypi/python-cjson - ``json``: This is the version of ``simplejson`` that is bundled with the Python standard library since version 2.6 (see http://docs.python.org/library/json.html) The default behavior is to use ``simplejson`` if installed, and otherwise fallback to the standard library module. To explicitly tell SPARQLWrapper which module to use, invoke the `use()` function with the module name:: import jsonlayer jsonlayer.use('cjson') In addition to choosing one of the above modules, you can also configure SPARQLWrapper to use custom decoding and encoding functions:: import jsonlayer jsonlayer.use(decode=my_decode, encode=my_encode) """ __all__ = ['decode', 'encode', 'use'] _initialized = False _using = None _decode = None _encode = None def decode(string): """Decode the given JSON string. :param string: the JSON string to decode :type string: basestring :return: the corresponding Python data structure :rtype: object """ if not _initialized: _initialize() return _decode(string) def encode(obj): """Encode the given object as a JSON string. :param obj: the Python data structure to encode :type obj: object :return: the corresponding JSON string :rtype: basestring """ if not _initialized: _initialize() return _encode(obj) def use(module=None, decode=None, encode=None): """Set the JSON library that should be used, either by specifying a known module name, or by providing a decode and encode function. The modules "simplejson", "cjson", and "json" are currently supported for the ``module`` parameter. If provided, the ``decode`` parameter must be a callable that accepts a JSON string and returns a corresponding Python data structure. The ``encode`` callable must accept a Python data structure and return the corresponding JSON string. Exceptions raised by decoding and encoding should be propagated up unaltered. :param module: the name of the JSON library module to use, or the module object itself :type module: str or module :param decode: a function for decoding JSON strings :type decode: callable :param encode: a function for encoding objects as JSON strings :type encode: callable """ global _decode, _encode, _initialized, _using if module is not None: if not isinstance(module, basestring): module = module.__name__ if module not in ('cjson', 'json', 'simplejson'): raise ValueError('Unsupported JSON module %s' % module) _using = module _initialized = False else: assert decode is not None and encode is not None _using = 'custom' _decode = decode _encode = encode _initialized = True def _initialize(): global _initialized def _init_simplejson(): global _decode, _encode import simplejson _decode = lambda string, loads=simplejson.loads: loads(string) _encode = lambda obj, dumps=simplejson.dumps: \ dumps(obj, allow_nan=False, ensure_ascii=False) def _init_cjson(): global _decode, _encode import cjson _decode = lambda string, decode=cjson.decode: decode(string) _encode = lambda obj, encode=cjson.encode: encode(obj) def _init_stdlib(): global _decode, _encode json = __import__('json', {}, {}) _decode = lambda string, loads=json.loads: loads(string) _encode = lambda obj, dumps=json.dumps: \ dumps(obj, allow_nan=False, ensure_ascii=False) if _using == 'simplejson': _init_simplejson() elif _using == 'cjson': _init_cjson() elif _using == 'json': _init_stdlib() elif _using != 'custom': try: _init_simplejson() except ImportError: _init_stdlib() _initialized = True sparql-wrapper-python-1.4.1/SPARQLWrapper/SPARQLUtils.py0000644000175000017500000000171211207744210021715 0ustar nachonacho# -*- coding: utf8 -*- """ SPARQL Wrapper Utils @authors: U{Ivan Herman}, U{Sergio Fernández}, U{Carlos Tejo Alonso} @organization: U{World Wide Web Consortium} and U{Foundation CTIC}. @license: U{W3C SOFTWARE NOTICE AND LICENSE} """ import warnings def deprecated(func): """ This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emmitted when the function is used. @see: http://code.activestate.com/recipes/391367/ """ def newFunc(*args, **kwargs): warnings.warn("Call to deprecated function %s." % func.__name__, category=DeprecationWarning, stacklevel=2) return func(*args, **kwargs) newFunc.__name__ = func.__name__ newFunc.__doc__ = func.__doc__ newFunc.__dict__.update(func.__dict__) return newFunc sparql-wrapper-python-1.4.1/SPARQLWrapper/SmartWrapper.py0000644000175000017500000002610311311410740022314 0ustar nachonacho# -*- coding: utf-8 -*- """ @see: U{SPARQL Specification} @authors: U{Ivan Herman}, U{Sergio Fernández}, U{Carlos Tejo Alonso} @organization: U{World Wide Web Consortium} and U{Foundation CTIC}. @license: U{W3C® SOFTWARE NOTICE AND LICENSE} @requires: U{RDFLib} package. """ import SPARQLWrapper from SPARQLWrapper.Wrapper import JSON, SELECT import urllib2 from types import * ###################################################################################### class Value : """ Class encapsulating a single binding for a variable. @cvar URI: the string denoting a URI variable @cvar Literal: the string denoting a Literal variable @cvar TypedLiteral: the string denoting a typed literal variable @cvar BNODE: the string denoting a blank node variable @ivar variable: The original variable, stored for an easier reference @type variable: string @ivar value: Value of the binding @type value: string @ivar type: Type of the binding @type type: string; one of L{Value.URI}, L{Value.Literal}, L{Value.TypedLiteral}, or L{Value.BNODE} @ivar lang: Language tag of the binding, or C{None} if not set @type lang: string @ivar datatype: Datatype of the binding, or C{None} if not set @type datatype: string (URI) """ URI = "uri" Literal = "literal" TypedLiteral = "typed-literal" BNODE = "bnode" def __init__(self,variable,binding) : """ @param variable: the variable for that binding. Stored for an easier reference @param binding: the binding dictionary part of the return result for a specific binding """ self.variable = variable self.value = binding['value'] self.type = binding['type'] self.lang = None self.datatype = None try : self.lang = binding['xml:lang'] except : # no lang is set pass try : self.datatype = binding['datatype'] except : pass ###################################################################################### class Bindings : """ Class encapsulating one query result, based on the JSON return format. It decodes the return values to make it a bit more usable for a standard usage. The class consumes the return value and instantiates a number of attributes that can be consulted directly. See the list of variables. The U{Serializing SPARQL Query Results in JSON} explains the details of the JSON return structures. Very succintly: the return data has "bindings", which means a list of dictionaries. Each dictionary is a possible binding of the SELECT variables to L{Value} instances. This structure is made a bit more usable by this class. @ivar fullResult: The original dictionary of the results, stored for an easier reference @ivar head: Header part of the return, see the JSON return format document for details @ivar variables: List of unbounds (variables) of the original query. It is an array of strings. None in the case of an ASK query @ivar bindings: The final bindings: array of dictionaries, mapping variables to L{Value} instances. (If unbound, then no value is set in the dictionary; that can be easily checked with C{var in res.bindings[..]}, for example.) @ivar askResult: by default, set to False; in case of an ASK query, the result of the query @type askResult: Boolean """ def __init__(self,retval) : """ @param retval: the query result, instance of a L{Wrapper.QueryResult} """ self.fullResult = retval._convertJSON() self.head = self.fullResult['head'] self.variables = None try : self.variables = self.fullResult['head']['vars'] except : pass self.bindings = [] try : for b in self.fullResult['results']['bindings'] : # this is a single binding. It is a dictionary per variable; each value is a dictionary again that has to be # converted into a Value instance newBind = {} for key in self.variables : if key in b : # there is a real binding for this key newBind[key] = Value(key,b[key]) self.bindings.append(newBind) except : pass self.askResult = False try : self.askResult = self.fullResult["boolean"] except : pass def getValues(self,key) : """A shorthand for the retrieval of all bindings for a single key. It is equivalent to "C{[b[key] for b in self[key]]}" @param key: possible variable @return: list of L{Value} instances """ try : return [b[key] for b in self[key]] except : return [] def __contains__(self,key) : """Emulation of the "C{key in obj}" operator. Key can be a string for a variable or an array/tuple of strings. If C{key} is a variable, the return value is C{True} if there is at least one binding where C{key} is bound. If C{key} is an array or tuple, the return value is C{True} if there is at least one binding where I{all} variables in C{key} are bound. @param key: possible variable, or array/tuple of variables @return: whether there is a binding of the variable in the return @rtype: Boolean """ if len(self.bindings) == 0 : return False if type(key) is ListType or type(key) is TupleType : # check first whether they are all really variables if False in [ k in self.variables for k in key ]: return False for b in self.bindings : # try to find a binding where all key elements are present if False in [ k in b for k in key ] : # this is not a binding for the key combination, move on... continue else : # yep, this one is good! return True return False else : if key not in self.variables : return False for b in self.bindings : if key in b : return True return False def __getitem__(self,key) : """Emulation of the C{obj[key]} operator. Slice notation is also available. The goal is to choose the right bindings among the available ones. The return values are always arrays of bindings, ie, arrays of dictionaries mapping variable keys to L{Value} instances. The different value settings mean the followings: - C{obj[key]} returns the bindings where C{key} has a valid value - C{obj[key1,key2,...]} returns the bindings where I{all} C{key1,key2,...} have valid values - C{obj[(key1,key2,...):(nkey1,nkey2,...)]} returns the bindings where all C{key1,key2,...} have valid values and I{none} of the C{nkey1,nkey2,...} have valid values - C{obj[:(nkey1,nkey2,...)]} returns the bindings where I{none} of the C{nkey1,nkey2,...} have valid values In all cases complete bindings are returned, ie, the values for other variables, not present among the keys in the call, may or may not be present depending on the query results. @param key: possible variable or array/tuple of keys with possible slice notation @return: list of bindings @rtype: array of variable -> L{Value} dictionaries """ def _checkKeys(keys) : if len(keys) == 0 : return False for k in keys : if not isinstance(k,StringTypes) or not k in self.variables: return False return True def _nonSliceCase(key) : if isinstance(key,StringTypes) and key != "" and key in self.variables : # unicode or string: return [key] elif type(key) is ListType or type(key) is TupleType : if _checkKeys(key) : return key return False # The arguments should be reduced to arrays of variables, ie, unicode strings yes_keys = [] no_keys = [] if type(key) is SliceType : # Note: None for start or stop is all right if key.start : yes_keys = _nonSliceCase(key.start) if not yes_keys: raise TypeError if key.stop : no_keys = _nonSliceCase(key.stop) if not no_keys: raise TypeError else : yes_keys = _nonSliceCase(key) # got it right, now get the right binding line with the constraints retval = [] for b in self.bindings : # first check whether the 'yes' part is all there: if False in [k in b for k in yes_keys] : continue if True in [k in b for k in no_keys] : continue # if we got that far, we shouild be all right! retval.append(b) # if retval is of zero length, no hit; an exception should be raised to stay within the python style if len(retval) == 0 : raise IndexError return retval def convert(self) : """This is just a convenience method, returns C{self}. Although C{Binding} is not a subclass of L{QueryResult}, it is returned as a result by L{SPARQLWrapper2.query}, just like L{QueryResult} is returned by L{SPARQLWrapper.SPARQLWrapper.query}. Consequently, having an empty C{convert} method to imitate L{QueryResult's convert method} may avoid unnecessary problems. """ return self ############################################################################################################## class SPARQLWrapper2(SPARQLWrapper.SPARQLWrapper) : """Subclass of L{Wrapper} that works with a JSON SELECT return result only. The query result is automatically set to a L{Bindings} instance. Makes the average query processing a bit simpler...""" def __init__(self,baseURI,defaultGraph=None) : """ Class encapsulating a full SPARQL call. In contrast to the L{SPARQLWrapper} superclass, the return format cannot be set (it is defaulted to L{JSON}). @param baseURI: string of the SPARQL endpoint's URI @type baseURI: string @keyword defaultGraph: URI for the default graph. Default is None, can be set via an explicit call, too @type defaultGraph: string """ SPARQLWrapper.SPARQLWrapper.__init__(self,baseURI,returnFormat=JSON,defaultGraph=defaultGraph) def setReturnFormat(self,format) : """Set the return format (overriding the L{inherited method}). This method does nothing; this class instance should work with JSON only. The method is defined just to avoid possible errors by erronously setting the return format. When using this class, the user can safely ignore this call. @param format: return format """ pass def query(self) : """ Execute the query and do an automatic conversion. Exceptions can be raised if either the URI is wrong or the HTTP sends back an error. The usual urllib2 exceptions are raised, which cover possible SPARQL errors, too. If the query type is I{not} SELECT, the method falls back to the L{corresponding method in the superclass}. @return: query result @rtype: L{Bindings} instance """ res = SPARQLWrapper.SPARQLWrapper.query(self) if self.queryType == SELECT : return Bindings(res) else : return res def queryAndConvert(self) : """This is here to override the inherited method; it is equivalent to L{query}. If the query type is I{not} SELECT, the method falls back to the L{corresponding method in the superclass}. @return: the converted query result. """ if self.queryType == SELECT : return self.query() else : return SPARQLWrapper.SPARQLWrapper.queryAndConvert(self) sparql-wrapper-python-1.4.1/SPARQLWrapper/__init__.py0000644000175000017500000002043711335212660021437 0ustar nachonacho# -*- coding: utf8 -*- """ This is a wrapper around a SPARQL service. It helps in creating the query URI and, possibly, convert the result into a more managable format. The following packages are used: - for JSON, the U{simplejson} package: C{http://cheeseshop.python.org/pypi/simplejson} - for RDF/XML, the U{RDFLib}: C{http://rdflib.net} These packages are imported in a lazy fashion, ie, only when needed. Ie, if the user never intends to use the JSON format, the C{simplejson} package is not imported and the user does not have to install it. The package can be downloaded in C{zip} and C{.tar.gz} formats from U{http://www.ivan-herman.net/Misc/PythonStuff/SPARQL/}. It is also available from U{Sourceforge} under the project named "C{sparql-wrapper}". Documentation is included in the distribution. Basic QUERY Usage ================= Simple query ------------ The simplest usage of this module looks as follows (using the default, ie, U{XML return format}, and special URI for the SPARQL Service):: from SPARQLWrapper import SPARQLWrapper queryString = "SELECT * WHERE { ?s ?p ?o. }" sparql = SPARQLWrapper("http://localhost:2020/sparql") # add a default graph, though that can also be part of the query string sparql.addDefaultGraph("http://www.example.com/data.rdf") sparql.setQuery(queryString) try : ret = sparql.query() # ret is a stream with the results in XML, see except : deal_with_the_exception() If C{SPARQLWrapper("http://localhost:2020/sparql",returnFormat=SPARQLWrapper.JSON)} was used, the result would be in U{JSON format} instead of XML (provided the sparql processor can return JSON). Automatic conversion of the results ----------------------------------- To make processing somewhat easier, the package can do some conversions automatically from the return result. These are: - for XML, the U{xml.dom.minidom} (C{http://docs.python.org/library/xml.dom.minidom.html}) is used to convert the result stream into a Python representation of a DOM tree - for JSON, the U{simplejson} package (C{http://cheeseshop.python.org/pypi/simplejson}) to generate a Python dictionary There are two ways to generate this conversion: - use C{ret.convert()} in the return result from C{sparql.query()} in the code above - use C{sparql.queryAndConvert()} to get the converted result right away if the intermediate stream is not used For example, in the code below:: try : sparql.setReturnFormat(SPARQLWrapper.JSON) ret = sparql.query() dict = ret.convert() except: deal_with_the_exception() the value of C{dict} is a Python dictionary of the query result, based on the U{JSON format}. The L{SPARQLWrapper} class can be subclassed by overriding the conversion routines if the user wants to use something else. Partial interpretation of the results ------------------------------------- A further help is to offer an extra, partial interpretation of the results, again to cover most of the practical use cases. Based on the U{JSON format}, the L{SmartWrapper.Bindings} class can perform some simple steps in decoding the JSON return results. If L{SPARQLWrapper2} is used instead of L{SPARQLWrapper}, this result format is generated. Note that this relies on a JSON format only, ie, it has to be checked whether the SPARQL service can return JSON or not. Here is a simple code that makes use of this feature:: from SPARQLWrapper import SPARQLWrapper2 queryString = "SELECT ?subj ?prop WHERE { ?subj ?prop ?o. }" sparql = SPARQLWrapper2("http://localhost:2020/sparql") # add a default graph, though that can also be in the query string sparql.addDefaultGraph("http://www.example.com/data.rdf") sparql.setQuery(queryString) try : ret = sparql.query() print ret.variables # this is an array consisting of "subj" and "prop" for binding in ret.bindings : # each binding is a dictionary. Let us just print the results print "%s: %s (of type %s)" % ("s",binding[u"subj"].value,binding[u"subj"].type) print "%s: %s (of type %s)" % ("p",binding[u"prop"].value,binding[u"prop"].type) except: deal_with_the_exception() To make this type of code even easier to realize, the C{[]} and C{in} operators are also implemented on the result of L{SmartWrapper.Bindings}. This can be used to check and find a particular binding (ie, particular row in the return value). This features becomes particularly useful when the C{OPTIONAL} feature of SPARQL is used. For example:: from SPARQLWrapper import SPARQLWrapper2 queryString = "SELECT ?subj ?o ?opt WHERE { ?subj ?o. OPTIONAL { ?subj ?opt }}" sparql = SPARQLWrapper2("http://localhost:2020/sparql") # add a default graph, though that can also be in the query string sparql.addDefaultGraph("http://www.example.com/data.rdf") sparql.setQuery(queryString) try : ret = sparql.query() print ret.variables # this is an array consisting of "subj", "o", "opt" if (u"subj",u"prop",u"opt") in ret : # there is at least one binding covering the optional "opt", too bindings = ret[u"subj",u"o",u"opt"] # bindings is an array of dictionaries with the full bindings for b in bindings : subj = b[u"subj"].value o = b[u"o"].value opt = b[u"opt"].value # do something nice with subj, o, and opt # another way of accessing to values for a single variable: # take all the bindings of the "subj" subjbind = ret.getValues(u"subj") # an array of Value instances ... except: deal_with_the_exception() CONSTRUCT, ASK, DESCRIBE ======================== All the examples so far were based on the SELECT queries. If the query includes, eg, the C{CONSTRUCT} keyword then the accepted return formats should be different: eg, C{SPARQLWrapper.XML} means C{RDF/XML} and most of the SPARQL engines can also return the results in C{Turtle}. The package, though it does not contain a full SPARQL parser, makes an attempt to determine the query type when the query is set. This should work in most of the cases (but there is a possibility to set this manually, in case something goes wrong). For RDF/XML, the U{RDFLib} (C{http://rdflib.net}) package is used to convert the result into a C{Graph} instance. GET or POST =========== By default, all SPARQL services are invoked using HTTP GET. However, POST might be useful if the size of the query extends a reasonable size; this can be set in the query instance. Note that some combination may not work yet with all SPARQL processors (eg, there are implementations where POST+JSON return does not work). Hopefully, this problem will eventually disappear. Acknowledgement =============== The package was greatly inspired by U{Lee Feigenbaum's similar package for Javascript}. @summary: Python interface to SPARQL services @see: U{SPARQL Specification} @authors: U{Ivan Herman}, U{Sergio Fernández}, U{Carlos Tejo Alonso} @organization: U{World Wide Web Consortium} and U{Foundation CTIC}. @license: U{W3C® SOFTWARE NOTICE AND LICENSE} @requires: U{simplejson} package. @requires: U{RDFLib} package. """ __version__ = "1.4.1" __authors__ = u"Ivan Herman, Sergio Fernández, Carlos Tejo Alonso" __license__ = u'W3C® SOFTWARE NOTICE AND LICENSE, http://www.w3.org/Consortium/Legal/copyright-software' __contact__ = 'sparql-wrapper-devel@lists.sourceforge.net' __date__ = "2010-02-12" __agent__ = "sparqlwrapper %s (http://sparql-wrapper.sourceforge.net/)" % __version__ from Wrapper import SPARQLWrapper, XML, JSON, TURTLE, N3, RDF, GET, POST, SELECT, CONSTRUCT, ASK, DESCRIBE from SmartWrapper import SPARQLWrapper2 sparql-wrapper-python-1.4.1/SPARQLWrapper/Wrapper.py0000644000175000017500000004732311335210534021321 0ustar nachonacho# -*- coding: utf-8 -*- # epydoc # """ @var JSON: to be used to set the return format to JSON @var XML: to be used to set the return format to XML (SPARQL XML format or RDF/XML, depending on the query type). This is the default. @var TURTLE: to be used to set the return format to Turtle @var N3: to be used to set the return format to N3 (for most of the SPARQL services this is equivalent to Turtle) @var RDF: to be used to set the return RDF Graph @var POST: to be used to set HTTP POST @var GET: to be used to set HTTP GET. This is the default. @var SELECT: to be used to set the query type to SELECT. This is, usually, determined automatically. @var CONSTRUCT: to be used to set the query type to CONSTRUCT. This is, usually, determined automatically. @var ASK: to be used to set the query type to ASK. This is, usually, determined automatically. @var DESCRIBE: to be used to set the query type to DESCRIBE. This is, usually, determined automatically. @see: U{SPARQL Specification} @authors: U{Ivan Herman}, U{Sergio Fernández}, U{Carlos Tejo Alonso} @organization: U{World Wide Web Consortium} and U{Foundation CTIC}. @license: U{W3C® SOFTWARE NOTICE AND LICENSE} @requires: U{RDFLib} package. """ import sys import urllib, urllib2 import re import jsonlayer import warnings from SPARQLWrapper import __agent__ from SPARQLExceptions import QueryBadFormed, EndPointNotFound from SPARQLUtils import deprecated from KeyInsensitiveDict import KeyInsensitiveDict # Possible output format keys... JSON = "json" XML = "xml" TURTLE = "n3" N3 = "n3" RDF = "rdf" _allowedFormats = [JSON, XML, TURTLE, N3, RDF] # Possible HTTP methods POST = "POST" GET = "GET" _allowedRequests = [POST, GET] # Possible SPARQL/SPARUL query type SELECT = "SELECT" CONSTRUCT = "CONSTRUCT" ASK = "ASK" DESCRIBE = "DESCRIBE" INSERT = "INSERT" DELETE = "DELETE" MODIFY = "MODIFY" _allowedQueryTypes = [SELECT, CONSTRUCT, ASK, DESCRIBE, INSERT, DELETE, MODIFY] # Possible output format (mime types) that can be converted by the local script. Unfortunately, # it does not work by simply setting the return format, because there is still a certain level of confusion # among implementations. # For example, Joseki returns application/javascript and not the sparql-results+json thing that is required... # Ie, alternatives should be given... # Andy Seaborne told me (June 2007) that the right return format is now added to his CVS, ie, future releases of # joseki will be o.k., too. The situation with turtle and n3 is even more confusing because the text/n3 and text/turtle # mime types have just been proposed and not yet widely used... _SPARQL_DEFAULT = ["application/sparql-results+xml", "application/rdf+xml", "*/*"] _SPARQL_XML = ["application/sparql-results+xml"] _SPARQL_JSON = ["application/sparql-results+json", "text/javascript", "application/json"] _RDF_XML = ["application/rdf+xml"] _RDF_N3 = ["text/rdf+n3","application/n-triples","application/turtle","application/n3","text/n3","text/turtle"] _ALL = ["*/*"] _RDF_POSSIBLE = _RDF_XML + _RDF_N3 _SPARQL_POSSIBLE = _SPARQL_XML + _SPARQL_JSON + _RDF_XML + _RDF_N3 _SPARQL_PARAMS = ["query"] # This is very ugly. The fact is that the key for the choice of the output format is not defined. # Virtuoso uses 'format', joseki uses 'output', rasqual seems to use "results", etc. Lee Feigenbaum # told me that virtuoso also understand 'output' these days, so I removed 'format'. I do not have # info about the others yet, ie, for the time being I keep the general mechanism. Hopefully, in a # future release, I can get rid of that. However, these processors are (hopefully) oblivious to the # parameters they do not understand. So: just repeat all possibilities in the final URI. UGLY!!!!!!! _returnFormatSetting = ["output","results"] ####################################################################################################### class SPARQLWrapper : """ Wrapper around an online access to a SPARQL Web entry point. The same class instance can be reused for subsequent queries. The values of the base Graph URI, return formats, etc, are retained from one query to the next (in other words, only the query string changes). The instance can also be reset to its initial values using the L{resetQuery} method. @cvar pattern: regular expression used to determine whether a query is of type L{CONSTRUCT}, L{SELECT}, L{ASK}, or L{DESCRIBE}. @type pattern: compiled regular expression (see the C{re} module of Python) @ivar baseURI: the URI of the SPARQL service """ pattern = re.compile(r""" (?P(\s*BASE\s*[<].*[>])\s*)* (?P(\s*PREFIX\s*.*[:]\s*[<].*[>])\s*)* (?P(CONSTRUCT|SELECT|ASK|DESCRIBE|INSERT|DELETE|MODIFY)) """, re.VERBOSE | re.IGNORECASE) def __init__(self,baseURI,returnFormat=XML,defaultGraph=None,agent=__agent__) : """ Class encapsulating a full SPARQL call. @param baseURI: string of the SPARQL endpoint's URI @type baseURI: string @keyword returnFormat: Default: L{XML}. Can be set to JSON or Turtle/N3 No local check is done, the parameter is simply sent to the endpoint. Eg, if the value is set to JSON and a construct query is issued, it is up to the endpoint to react or not, this wrapper does not check. Possible values: L{JSON}, L{XML}, L{TURTLE}, L{N3} (constants in this module). The value can also be set via explicit call, see below. @type returnFormat: string @keyword defaultGraph: URI for the default graph. Default is None, the value can be set either via an L{explicit call} or as part of the query string. @type defaultGraph: string """ self.baseURI = baseURI self.agent = agent self.customParameters = {} self._defaultGraph = defaultGraph if defaultGraph : self.customParameters["default-graph-uri"] = defaultGraph if returnFormat in _allowedFormats : self.returnFormat = returnFormat else : self.returnFormat = XML self._defaultReturnFormat = self.returnFormat self.queryString = """SELECT * WHERE{ ?s ?p ?o }""" self.method = GET self.queryType = SELECT def resetQuery(self) : """Reset the query, ie, return format, query, default or named graph settings, etc, are reset to their default values.""" self.customParameters = {} if self._defaultGraph : self.customParameters["default-graph-uri"] = self._defaultGraph self.returnFormat = self._defaultReturnFormat self.method = GET self.queryType = SELECT self.queryString = """SELECT * WHERE{ ?s ?p ?o }""" def setReturnFormat(self,format) : """Set the return format. If not an allowed value, the setting is ignored. @param format: Possible values: are L{JSON}, L{XML}, L{TURTLE}, L{N3}, L{RDF} (constants in this module). All other cases are ignored. @type format: string """ if format in _allowedFormats : self.returnFormat = format @deprecated def addDefaultGraph(self,uri) : """ Add a default graph URI. @param uri: URI of the graph @type uri: string @deprecated: use addCustomParameter("default-graph-uri", uri) instead of this method """ self.addCustomParameter("default-graph-uri",uri) @deprecated def addNamedGraph(self,uri) : """ Add a named graph URI. @param uri: URI of the graph @type uri: string @deprecated: use addCustomParameter("named-graph-uri", uri) instead of this method """ self.addCustomParameter("named-graph-uri",uri) @deprecated def addExtraURITag(self,key,value) : """ Some SPARQL endpoints require extra key value pairs. E.g., in virtuoso, one would add C{should-sponge=soft} to the query forcing virtuoso to retrieve graphs that are not stored in its local database. @param key: key of the query part @type key: string @param value: value of the query part @type value: string @deprecated: use addCustomParameter(key, value) instead of this method """ self.addCustomParameter(key,value) def addCustomParameter(self,name,value): """ Some SPARQL endpoints allow extra key value pairs. E.g., in virtuoso, one would add C{should-sponge=soft} to the query forcing virtuoso to retrieve graphs that are not stored in its local database. @param name: name @type name: string @param value: value @type value: string @rtype: bool """ if (name in _SPARQL_PARAMS): return False; else: self.customParameters[name] = value return True 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.queryString = query self.queryType = self._parseQueryType(query) def _parseQueryType(self,query) : """ Parse the SPARQL query and return its type (ie, L{SELECT}, L{ASK}, etc). Note that the method returns L{SELECT} if nothing is specified. This is just to get all other methods running; in fact, this means that the query is erronous, because the query must be, according to the SPARQL specification, one of Select, Ask, Describe, or Construct. The SPARQL endpoint should raise an exception (via urllib) for such syntax error. @param query: query text @type query: string @rtype: string """ try: r_queryType = self.pattern.search(query).group("queryType").upper() except AttributeError: r_queryType = None if r_queryType in _allowedQueryTypes : return r_queryType else : #raise Exception("Illegal SPARQL Query; must be one of SELECT, ASK, DESCRIBE, or CONSTRUCT") warnings.warn("unknown query type", RuntimeWarning) return SELECT def setMethod(self,method) : """Set the invocation method. By default, this is L{GET}, but can be set to L{POST}. @param method: should be either L{GET} or L{POST}. Other cases are ignored. """ if method in _allowedRequests : self.method = method def setUseKeepAlive(self): """Make urllib2 use keep-alive. @raise ImportError: when could not be imported urlgrabber.keepalive.HTTPHandler """ try: from urlgrabber.keepalive import HTTPHandler keepalive_handler = HTTPHandler() opener = urllib2.build_opener(keepalive_handler) urllib2.install_opener(opener) except ImportError: warnings.warn("urlgrabber not installed in the system. The execution of this method has no effect.") def _getURI(self) : """Return the URI as sent (or to be sent) to the SPARQL endpoint. The URI is constructed with the base URI given at initialization, plus all the other parameters set. @return: URI @rtype: string """ finalQueryParameters = self.customParameters.copy() finalQueryParameters["query"] = self.queryString if self.returnFormat != XML : # This is very ugly. The fact is that the key for the choice of the output format is not defined. # Virtuoso uses 'format',sparqler uses 'output' # However, these processors are (hopefully) oblivious to the parameters they do not understand. # So: just repeat all possibilities in the final URI. UGLY!!!!!!! for f in _returnFormatSetting: finalQueryParameters[f] = self.returnFormat return self.baseURI + "?" + urllib.urlencode(dict([k, v.encode("utf-8")] for k, v in finalQueryParameters.items())) def _createRequest(self) : """Internal method to create request according a HTTP method. Returns a C{urllib2.Request} object of the urllib2 Python library @return: request """ if self.queryType == SELECT or self.queryType == ASK or self.queryType == INSERT or self.queryType == DELETE or self.queryType == MODIFY: if self.returnFormat == XML: acceptHeader = ",".join(_SPARQL_XML) elif self.returnFormat == JSON: acceptHeader = ",".join(_SPARQL_JSON) ## elif self.returnFormat == N3 or self.returnFormat == TURTLE : ## acceptHeader = ",".join(_SPARQL_XML) else : acceptHeader = ",".join(_ALL) else : if self.returnFormat == N3 or self.returnFormat == TURTLE : acceptHeader = ",".join(_RDF_N3) elif self.returnFormat == XML : acceptHeader = ",".join(_RDF_XML) ## elif self.returnFormat == JSON : ## acceptHeader = ",".join(_RDF_XML) else : acceptHeader = ",".join(_ALL) if self.method == POST : # by POST request = urllib2.Request(self.baseURI) request.add_header("Content-Type", "application/x-www-form-urlencoded") request.add_header("User-Agent", self.agent) request.add_header("Accept", acceptHeader) values = { "query" : self.queryString.encode("utf-8") } data = urllib.urlencode(values) request.add_data(data) else: # by GET # Some versions of Joseki do not work well if no Accept header is given. # Although it is probably o.k. in newer versions, it does not harm to have that set once and for all... request = urllib2.Request(self._getURI()) request.add_header("Accept",acceptHeader) return request def _query(self): """Internal method to execute the query. Returns the output of the C{urllib2.urlopen} method of the standard Python library """ request = self._createRequest() try: response = urllib2.urlopen(request) return response except urllib2.HTTPError, e: if e.code == 400: raise QueryBadFormed elif e.code == 404: raise EndPointNotFound else: raise e return None def query(self) : """ Execute the query. Exceptions can be raised if either the URI is wrong or the HTTP sends back an error (this is also the case when the query is syntactically incorrect, leading to an HTTP error sent back by the SPARQL endpoint). The usual urllib2 exceptions are raised, which therefore cover possible SPARQL errors, too. Note that some combinations of return formats and query types may not make sense. For example, a SELECT query with Turtle response is meaningless (the output of a SELECT is not a Graph), or a CONSTRUCT query with JSON output may be a problem because, at the moment, there is no accepted JSON serialization of RDF (let alone one implemented by SPARQL endpoints). In such cases the returned media type of the result is unpredictable and may differ from one SPARQL endpoint implementation to the other. (Endpoints usually fall back to one of the "meaningful" formats, but it is up to the specific implementation to choose which one that is.) @return: query result @rtype: L{QueryResult} instance """ return QueryResult(self._query()) def queryAndConvert(self) : """Macro like method: issue a query and return the converted results. @return: the converted query result. See the conversion methods for more details. """ res = self.query() return res.convert() ####################################################################################################### class QueryResult : """ Wrapper around an a query result. Users should not create instances of this class, it is generated by a L{SPARQLWrapper.query} call. The results can be converted to various formats, or used directly. If used directly: the class gives access to the direct http request results L{self.response}: it is a file-like object with two additional methods: C{geturl()} to return the URL of the resource retrieved and C{info()} that returns the meta-information of the HTTP result as a dictionary-like object (see the urllib2 standard library module of Python). For convenience, these methods are also available on the instance. The C{__iter__} and C{next} methods are also implemented (by mapping them to L{self.response}). This means that the common idiom:: for l in obj : do_something_with_line(l) would work, too. @ivar response: the direct HTTP response; a file-like object, as return by the C{urllib2.urlopen} library call. """ def __init__(self,response) : """ @param response: HTTP response stemming from a L{SPARQLWrapper.query} call """ self.response = response """Direct response, see class comments for details""" def geturl(self) : """Return the URI of the original call. @return: URI @rtype: string """ return self.response.geturl() def info(self) : """Return the meta-information of the HTTP result. @return: meta information @rtype: dictionary """ return KeyInsensitiveDict(self.response.info()) def __iter__(self) : """Return an iterator object. This method is expected for the inclusion of the object in a standard C{for} loop. """ return self.response.__iter__() def next(self) : """Method for the standard iterator.""" return self.response.next() def setJSONModule(self,module) : """Set the Python module for encoding JSON data. If not an allowed value, the setting is ignored. JSON modules supported: - ``simplejson``: http://code.google.com/p/simplejson/ - ``cjson``: http://pypi.python.org/pypi/python-cjson - ``json``: This is the version of ``simplejson`` that is bundled with the Python standard library since version 2.6 (see http://docs.python.org/library/json.html) @param module: Possible values: are L{simplejson}, L{cjson}, L{json}. All other cases raise a ValueError exception. @type module: string """ jsonlayer.use(module) def _convertJSON(self) : """ Convert a JSON result into a Python dict. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: Python dictionary """ return jsonlayer.decode(self.response.read()) # patch to solve bug #2781984 # later updated with a trick from http://bob.pythonmac.org/archives/2008/10/02/python-26-released-now-with-json/ #try: # import simplejson as json #except ImportError: # import json #return json.load(self.response) def _convertXML(self) : """ Convert an XML result into a Python dom tree. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: PyXlib DOM node """ from xml.dom.minidom import parse return parse(self.response) def _convertRDF(self) : """ Convert an RDF/XML result into an RDFLib triple store. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: RDFLib Graph """ from rdflib import ConjunctiveGraph retval = ConjunctiveGraph() # this is a strange hack. If the publicID is not set, rdflib (or the underlying xml parser) makes a funny #(and, as far as I could see, meaningless) error message... retval.load(self.response,publicID=' ') return retval def _convertN3(self) : """ Convert an RDF Turtle/N3 result into a string. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: string """ return self.response.read() def convert(self) : """ Encode the return value depending on the return format: - in the case of XML, a DOM top element is returned; - in the case of JSON, a simplejson conversion will return a dictionary; - in the case of RDF/XML, the value is converted via RDFLib into a Graph instance. In all other cases the input simply returned. @return: the converted query result. See the conversion methods for more details. """ ct = self.info()["content-type"] if True in [ct.find(q) != -1 for q in _SPARQL_XML] : return self._convertXML() elif True in [ct.find(q) != -1 for q in _SPARQL_JSON] : return self._convertJSON() elif True in [ct.find(q) != -1 for q in _RDF_XML] : return self._convertRDF() elif True in [ct.find(q) != -1 for q in _RDF_N3] : return self._convertN3() else : # this should cover, well, the rest of the cases... warnings.warn("unknown response content type, returning raw response...", RuntimeWarning) return self.response sparql-wrapper-python-1.4.1/setup.cfg0000644000175000017500000000007311335213040016567 0ustar nachonacho[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 sparql-wrapper-python-1.4.1/LICENSE.txt0000644000175000017500000000412611207744210016602 0ustar nachonachoSPARQL Python Wrapper is released under the W3C® SOFTWARE NOTICE AND LICENSE. This work (and included software, documentation such as READMEs, or other related items) is being provided by the copyright holders under the following license. By obtaining, using and/or copying this work, you (the licensee) agree that you have read, understood, and will comply with the following terms and conditions. Permission to copy, modify, and distribute this software and its documentation, with or without modification, for any purpose and without fee or royalty is hereby granted, provided that you include the following on ALL copies of the software and documentation or portions thereof, including modifications: 1. The full text of this NOTICE in a location viewable to users of the redistributed or derivative work. 2. Any pre-existing intellectual property disclaimers, notices, or terms and conditions. If none exist, the W3C Software Short Notice should be included (hypertext is preferred, text is permitted) within the body of any redistributed or derivative code. 3. Notice of any changes or modifications to the files, including the date changes were made. (We recommend you provide URIs to the location from which the code is derived.) THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION. The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the software without specific, written prior permission. Title to copyright in this software and any associated documentation will at all times remain with copyright holders. See also http://www.w3.org/Consortium/Legal/copyright-software for further details sparql-wrapper-python-1.4.1/README.txt0000644000175000017500000000104711207744210016454 0ustar nachonacho SPARQL Endpoint interface to Python ----------------------------------- The distribution contains: - SPARQLWrapper: the Python library. You should copy the directory somewhere into your PYTHONPATH. Alternatively, you can also run the disutils scrips: python setup.py install - script/example.py: a basic example of the library using the public end-point of DBpedia. - scripts/sparql.py: to run a SPARQL SELECT query stored in an external file. The URIs for the SPARQL services have to be updated for local use. sparql-wrapper-python-1.4.1/MANIFEST.in0000644000175000017500000000001611207744210016507 0ustar nachonachoinclude *.txt sparql-wrapper-python-1.4.1/setup.py0000755000175000017500000000330411322616036016473 0ustar nachonacho# -*- coding: utf-8 -*- from ez_setup import use_setuptools use_setuptools() from setuptools import setup, find_packages import sys _requires = [] _install_requires = [] # rdflib _requires.append('rdflib') _install_requires.append('rdflib == 2.4.2') # simplejson if sys.version_info[0:2] < (2, 6): _requires.append('simplejson') _install_requires.append('simplejson == 2.0.9') setup( name = 'SPARQLWrapper', version = '1.4.1', description = 'SPARQL Endpoint interface to Python', long_description = 'This is a wrapper around a SPARQL service. It helps in creating the query URI and, possibly, convert the result into a more manageable format.', license = 'W3C SOFTWARE NOTICE AND LICENSE', #Should be removed by PEP 314 author = "Ivan Herman, Sergio Fernandez, Carlos Tejo Alonso", author_email = "ivan at ivan-herman net, sergio.fernandez at fundacionctic org, carlos.tejo at fundacionctic org", url = 'http://sparql-wrapper.sourceforge.net/', download_url = 'http://sourceforge.net/projects/sparql-wrapper/files', platforms = ['any'], #Should be removed by PEP 314 packages = ['SPARQLWrapper'], requires = _requires, # Used by distutils to create metadata PKG-INFO install_requires = _install_requires, #Used by setuptools to install the dependencies classifiers = [ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: W3C License', 'Operating System :: OS Independent', 'Programming Language :: Python :: 2.5', ], keywords = 'python SPARQL', #requires_python = '>=2.5', # Future in PEP 345 #scripts = ['ez_setup.py'] ) sparql-wrapper-python-1.4.1/ez_setup.py0000644000175000017500000002405511274322626017201 0ustar nachonacho#!python """Bootstrap setuptools installation If you want to use setuptools in your package's setup.py, just include this file in the same directory with it, and add this to the top of your setup.py:: from ez_setup import use_setuptools use_setuptools() If you want to require a specific version of setuptools, set a download mirror, or use an alternate download directory, you can do so by supplying the appropriate options to ``use_setuptools()``. This file can also be run as a script to install or upgrade setuptools. """ import sys DEFAULT_VERSION = "0.6c11" DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3] md5_data = { 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca', 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb', 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b', 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a', 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618', 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac', 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5', 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4', 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c', 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b', 'setuptools-0.6c10-py2.3.egg': 'ce1e2ab5d3a0256456d9fc13800a7090', 'setuptools-0.6c10-py2.4.egg': '57d6d9d6e9b80772c59a53a8433a5dd4', 'setuptools-0.6c10-py2.5.egg': 'de46ac8b1c97c895572e5e8596aeb8c7', 'setuptools-0.6c10-py2.6.egg': '58ea40aef06da02ce641495523a0b7f5', 'setuptools-0.6c11-py2.3.egg': '2baeac6e13d414a9d28e7ba5b5a596de', 'setuptools-0.6c11-py2.4.egg': 'bd639f9b0eac4c42497034dec2ec0c2b', 'setuptools-0.6c11-py2.5.egg': '64c94f3bf7a72a13ec83e0b24f2749b2', 'setuptools-0.6c11-py2.6.egg': 'bfa92100bd772d5a213eedd356d64086', 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27', 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277', 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa', 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e', 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e', 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f', 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2', 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc', 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167', 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64', 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d', 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20', 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab', 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53', 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2', 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e', 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372', 'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902', 'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de', 'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b', 'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03', 'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a', 'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6', 'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a', } import sys, os try: from hashlib import md5 except ImportError: from md5 import md5 def _validate_md5(egg_name, data): if egg_name in md5_data: digest = md5(data).hexdigest() if digest != md5_data[egg_name]: print >>sys.stderr, ( "md5 validation of %s failed! (Possible download problem?)" % egg_name ) sys.exit(2) return data def use_setuptools( version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, download_delay=15 ): """Automatically find/download setuptools and make it available on sys.path `version` should be a valid setuptools version number that is available as an egg for download under the `download_base` URL (which should end with a '/'). `to_dir` is the directory where setuptools will be downloaded, if it is not already available. If `download_delay` is specified, it should be the number of seconds that will be paused before initiating a download, should one be required. If an older version of setuptools is installed, this routine will print a message to ``sys.stderr`` and raise SystemExit in an attempt to abort the calling script. """ was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules def do_download(): egg = download_setuptools(version, download_base, to_dir, download_delay) sys.path.insert(0, egg) import setuptools; setuptools.bootstrap_install_from = egg try: import pkg_resources except ImportError: return do_download() try: pkg_resources.require("setuptools>="+version); return except pkg_resources.VersionConflict, e: if was_imported: print >>sys.stderr, ( "The required version of setuptools (>=%s) is not available, and\n" "can't be installed while this script is running. Please install\n" " a more recent version first, using 'easy_install -U setuptools'." "\n\n(Currently using %r)" ) % (version, e.args[0]) sys.exit(2) else: del pkg_resources, sys.modules['pkg_resources'] # reload ok return do_download() except pkg_resources.DistributionNotFound: return do_download() def download_setuptools( version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, delay = 15 ): """Download setuptools from a specified location and return its filename `version` should be a valid setuptools version number that is available as an egg for download under the `download_base` URL (which should end with a '/'). `to_dir` is the directory where the egg will be downloaded. `delay` is the number of seconds to pause before an actual download attempt. """ import urllib2, shutil egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3]) url = download_base + egg_name saveto = os.path.join(to_dir, egg_name) src = dst = None if not os.path.exists(saveto): # Avoid repeated downloads try: from distutils import log if delay: log.warn(""" --------------------------------------------------------------------------- This script requires setuptools version %s to run (even to display help). I will attempt to download it for you (from %s), but you may need to enable firewall access for this script first. I will start the download in %d seconds. (Note: if this machine does not have network access, please obtain the file %s and place it in this directory before rerunning this script.) ---------------------------------------------------------------------------""", version, download_base, delay, url ); from time import sleep; sleep(delay) log.warn("Downloading %s", url) src = urllib2.urlopen(url) # Read/write all in one block, so we don't create a corrupt file # if the download is interrupted. data = _validate_md5(egg_name, src.read()) dst = open(saveto,"wb"); dst.write(data) finally: if src: src.close() if dst: dst.close() return os.path.realpath(saveto) def main(argv, version=DEFAULT_VERSION): """Install or upgrade setuptools and EasyInstall""" try: import setuptools except ImportError: egg = None try: egg = download_setuptools(version, delay=0) sys.path.insert(0,egg) from setuptools.command.easy_install import main return main(list(argv)+[egg]) # we're done here finally: if egg and os.path.exists(egg): os.unlink(egg) else: if setuptools.__version__ == '0.0.1': print >>sys.stderr, ( "You have an obsolete version of setuptools installed. Please\n" "remove it from your system entirely before rerunning this script." ) sys.exit(2) req = "setuptools>="+version import pkg_resources try: pkg_resources.require(req) except pkg_resources.VersionConflict: try: from setuptools.command.easy_install import main except ImportError: from easy_install import main main(list(argv)+[download_setuptools(delay=0)]) sys.exit(0) # try to force an exit else: if argv: from setuptools.command.easy_install import main main(argv) else: print "Setuptools version",version,"or greater has been installed." print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' def update_md5(filenames): """Update our built-in md5 registry""" import re for name in filenames: base = os.path.basename(name) f = open(name,'rb') md5_data[base] = md5(f.read()).hexdigest() f.close() data = [" %r: %r,\n" % it for it in md5_data.items()] data.sort() repl = "".join(data) import inspect srcfile = inspect.getsourcefile(sys.modules[__name__]) f = open(srcfile, 'rb'); src = f.read(); f.close() match = re.search("\nmd5_data = {\n([^}]+)}", src) if not match: print >>sys.stderr, "Internal error!" sys.exit(2) src = src[:match.start(1)] + repl + src[match.end(1):] f = open(srcfile,'w') f.write(src) f.close() if __name__=='__main__': if len(sys.argv)>2 and sys.argv[1]=='--md5update': update_md5(sys.argv[2:]) else: main(sys.argv[1:]) sparql-wrapper-python-1.4.1/SPARQLWrapper.egg-info/0000755000175000017500000000000011361100677021015 5ustar nachonachosparql-wrapper-python-1.4.1/SPARQLWrapper.egg-info/PKG-INFO0000644000175000017500000000155311335213040022104 0ustar nachonachoMetadata-Version: 1.1 Name: SPARQLWrapper Version: 1.4.1 Summary: SPARQL Endpoint interface to Python Home-page: http://sparql-wrapper.sourceforge.net/ Author: Ivan Herman, Sergio Fernandez, Carlos Tejo Alonso Author-email: ivan at ivan-herman net, sergio.fernandez at fundacionctic org, carlos.tejo at fundacionctic org License: W3C SOFTWARE NOTICE AND LICENSE Download-URL: http://sourceforge.net/projects/sparql-wrapper/files Description: This is a wrapper around a SPARQL service. It helps in creating the query URI and, possibly, convert the result into a more manageable format. Keywords: python SPARQL Platform: any Classifier: Development Status :: 4 - Beta Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: W3C License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 2.5 Requires: rdflib sparql-wrapper-python-1.4.1/SPARQLWrapper.egg-info/dependency_links.txt0000644000175000017500000000000111335213040025051 0ustar nachonacho sparql-wrapper-python-1.4.1/SPARQLWrapper.egg-info/SOURCES.txt0000644000175000017500000000077511335213040022700 0ustar nachonachoAUTHORS.txt ChangeLog.txt LICENSE.txt MANIFEST.in README.txt ez_setup.py setup.py SPARQLWrapper/KeyInsensitiveDict.py SPARQLWrapper/SPARQLExceptions.py SPARQLWrapper/SPARQLUtils.py SPARQLWrapper/SmartWrapper.py SPARQLWrapper/Wrapper.py SPARQLWrapper/__init__.py SPARQLWrapper/jsonlayer.py SPARQLWrapper.egg-info/PKG-INFO SPARQLWrapper.egg-info/SOURCES.txt SPARQLWrapper.egg-info/dependency_links.txt SPARQLWrapper.egg-info/requires.txt SPARQLWrapper.egg-info/top_level.txt scripts/example.py scripts/sparql.pysparql-wrapper-python-1.4.1/SPARQLWrapper.egg-info/top_level.txt0000644000175000017500000000001611335213040023532 0ustar nachonachoSPARQLWrapper sparql-wrapper-python-1.4.1/SPARQLWrapper.egg-info/requires.txt0000644000175000017500000000001711335213040023401 0ustar nachonachordflib == 2.4.2sparql-wrapper-python-1.4.1/ChangeLog.txt0000644000175000017500000000270711335207670017360 0ustar nachonachoSPARQLWrapper's changelog: ------------------------- 2010-01-11 1.4.1 - Supporting keep-alive in SPARQLWrapper if urlgrabber is available (ticket #2929881) - fixed bugs (#2949834) 2009-12-14 1.4.0 - Added some suport for SPARUL - Improved HTTP related code - Many other minor bugs fixed 2009-09-23 1.3.2 - Remove pyxml dependecy. Instead, use xml.dom.minidom - Updated setup installation (added rdflib dependency) - Udpated example.py (added XML, N3 and RDF examples) 2009-09-11 1.3.1 - Remove simplejson dependency for python => 2.6 version - Added feature to choose the json module to use 2009-05-06 1.3.0 - Added a new method to add custom parameters (deprecated old way to do it) 2009-04-27 1.2.1 - Updated setup installation - Patched to work with JSON in Python>=2.6 2008-07-10 1.2.0 - Allowed non-standard extensions (such as SPARUL). - Exceptions fixed. - Added another example. 2008-03-24 1.1.0 - Renamed package name to SPARQLWrapper. - Added a basic catalog of exceptions. 2008-03-07 1.0.1 - Fixed some cosmetic things. 2008-02-14 1.0.0 - First stable release. - Main functionalities stabilized. - Project moved to SourceForge. 2007-07-06 0.2.0 - First public release of the library.