예제 #1
0
파일: serve.py 프로젝트: drewp/sparqlhttp
class SPARQLResource(Resource):
    """a sparql-over-http server. This resource is commonly at the
    root of an http server. There is ad-hoc support for some things
    beyond queries:
    POST /add?context={uri} with postdata that's an nt file
    GET /save?context={uri}
    GET / to view stats about what's been served
    POST /remove?context={uri} with postdata that's ntriples to remove, context is optional

    todo:
    DELETE /?context={uri}   drop this context
    """
    isLeaf = True
    def __init__(self, graph):
        self.graph = graph
        self.stats = Stats()

    def render_GET(self, request):
        # see http://twistedmatrix.com/projects/web/documentation/howto/using-twistedweb.html#rendering
        if request.path == '/':
            if 'query' not in request.args:
                return self.stats.statusPage()
            return self.getQuery(request)
        
        if request.path == '/save':
            warnings.warn('Use POST for /save requests', DeprecationWarning)
            return self.getSave(request)
        
        request.setResponseCode(http.BAD_REQUEST)
        return "<html>Invalid request: this is a sparql query server</html>"

    def getQuery(self, request):
        """GET /?query=SELECT... returns sparql results in xml"""
        query = request.args['query'][0]
        log.debug("received query: %r", query)
        isCount = request.getHeader('x-stat-result') == 'count'
        
        self.stats.queries += 1
        t1 = time.time()
        try:
            self.stats.lastQuery = query
            if isCount:
                count = self.graph.countQuery(query)
            else:
                results = self.graph.queryd(query)
        except Exception, e:
            self.stats.lastErrorQuery = query
            self.stats.lastError = traceback.format_exc()
            log.debug("query error: %s", self.stats.lastError)
            raise

        if not isCount:
            try:
                results = list(results)
                count = len(results)
                log.debug("got %s rows", count)
            except TypeError,e:
                count = 0 # this bogus value only affects the stats
예제 #2
0
파일: serve.py 프로젝트: drewp/sparqlhttp
 def __init__(self, graph):
     self.graph = graph
     self.stats = Stats()