Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/semanticxo/sparql.py
blob: 968ecef3367395f517bd9d71f35b87b55acd88b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'''
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)
        # 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