SPAQRL endpoint for Turbogears
triplestore.py is an RDF SPARQL endpoint using rdflib. It is meant to compliment Turbogear's SQL database backends SQLAlchemy and SQLObject, not act as an alternative.
It is brand-spanking-new and hardly tested, so any input is welcome, especially from SPARQL experts and Turbogears experts who may be able to check my code to see how we can integrate it via a threaded hub like SQLObject/SQLAlchemy.
Backends supported (via rdflib):
- mysql
- sleepycat
- sqlite
- berkeleydb
- redland (un-tested)
- zodb (Zope Object Database - un-tested)
Example TurboGears SPARQL endpoint: http://doapspace.org/sparql/
News
I'm working on a web-based SPARQL client/editor based on Danny Ayer's javascript work. You can take a look at it here: http://doapspace.org/sparql/
Usage
Copy source:trunk/triplestore.py to your project directory.
Edit your dev.cfg or prod.cfg and add an rdflib.dburi:
rdflib.dburi="mysql://username:password@hostname[:port]/databasename" rdflib.dburi="sleepycat:///path/to/dbname" rdflib.dburi="berkeleydb:///path/to/dbname" rdflib.dburi="sqlite:///path/to/dbname"
Create your triplestore if you are not using an existing one:
./triplestore.py -c
Add an import and a 'sparql' method to your controller.py:
from triplestore import graph_hub, sparql_negotiate import cherrypy #if you don't already
In your Root controller:
@cherrypy.expose() def sparql(self, query=None, output=None, results=None): """ SPARQL endpoint using rdflib @param: query @type: string containing a SPARQL query @returns: Results in format specified by Accept headers """ accept = str(request.headers.get("Accept")) content_type, format = sparql_negotiate(accept) if query is None or format is None: cherrypy.response.headers["Content-Type"] = "text/html" return "Error: This is a SPARQL endpoint.<br>" \ "Use a SPARQL client, not a web browser!<br><br>" \ "Content type is not supported by SPAQRL protocol:\n %s" % accept resp = graph_hub.sparql_query(query) cherrypy.response.headers["Content-Type"] = content_type return resp.serialize(format=format)
That's it! Now point a SPARQL client to http://example.com/sparql/ and away you go.
Optional configuration
triplestore.py has a short list of common RDF namespaces such as DOAP, FOAF, RDF etc. If you need to add more RDF namespaces, modify RDF_NAMESPACES in triplestore.py
If you're using an existing triplestore, change STORE_NAME in triplestore.py to the named graph you're using.
Example SPARQL Client
Here is a very simply Python client which uses the SPARQL library.
XML Results
from SPARQL import SPARQLWrapper
query = """
PREFIX doap: <http://usefulinc.com/ns/doap#>
SELECT ?name ?description ?url WHERE {
?proj doap:name "Raptor";
doap:description ?description;
doap:homepage ?url;
doap:name ?name
}
"""
sparql = SPARQLWrapper("http://doapspace.org/sparql/")
sparql.setQuery(query)
for line in sparql.query():
print line.strip()
JSON Output
from SPARQL import SPARQLWrapper, JSON
query = """
PREFIX doap: <http://usefulinc.com/ns/doap#>
SELECT ?name ?description ?url WHERE {
?proj doap:name "Raptor" ;
doap:description ?description ;
doap:homepage ?url ;
doap:name ?name
}
"""
sparql = SPARQLWrapper("http://doapspace.org/sparql/")
sparql.setReturnFormat(JSON)
sparql.setQuery(query)
for line in sparql.query():
print line.strip()
TODO
Convert triplestore.py into a proper Turbogears tg-admin command.
Create a sparql_expose decorator so we can properly use rdflib sanely in a threaded environment.
Look into supporting RDFAlchemy as a general-purpose RDF library.
![(please configure the [header_logo] section in trac.ini)](http://doapspace.org/static/doapspace.png)