diff options
Diffstat (limited to 'src/semanticxo/sparql.py')
-rw-r--r-- | src/semanticxo/sparql.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/semanticxo/sparql.py b/src/semanticxo/sparql.py new file mode 100644 index 0000000..ca42167 --- /dev/null +++ b/src/semanticxo/sparql.py @@ -0,0 +1,43 @@ +''' +Created on 25 Aug 2011 + +@author: cgueret +''' +import httplib +import urllib +import cjson +from rdflib import URIRef, Literal + +class SPARQL(object): + ''' + classdocs + ''' + + + def __init__(self): + ''' + Constructor + ''' + self._url = '127.0.0.1:8080' + + def execute_select(self, query): + params = {'query': query, 'format' : 'json'} + headers = {'Content-Type': 'application/x-www-form-urlencoded'} + conn = httplib.HTTPConnection(self._url) + conn.request("POST", "/sparql", urllib.urlencode(params), headers=headers) + # Get the results + response = conn.getresponse() + r = cjson.decode(response.read(), all_unicode=False) + print r['results']['bindings'] + # Recode them + results = [] + for entry in r['results']['bindings']: + result = {} + for (name,data) in entry.iteritems(): + value = Literal(data['value']) + if data['type']=='uri': + value = URIRef(data['value']) + result[name] = value + results.append(result) + conn.close() + return results
\ No newline at end of file |