Example #1
0
    def parse_json_results(self, response):
        graph = rdflib.ConjunctiveGraph()
        json = json.load(response)

        if 'boolean' in json:
            return SparqlResultBool(json['boolean'])

        vars_ = json['head']['vars']
        ResultClass = Result(json['head']['vars'])
        pb = self.parse_json_binding

        results = SparqlResultList(vars_)
        for binding in json['results']['bindings']:
            results.append(ResultClass(*[pb(binding.get(v), graph) for v in vars_]))
        return results
Example #2
0
import functools
import itertools

import rdflib

from humfrey.sparql.results import SparqlResultList, Result
from humfrey.utils.namespaces import NS

_TEST_BNODE = rdflib.BNode()
TEST_RESULTSET_FIELDS = ('one', 'two')
TEST_RESULTSET_RESULT = functools.partial(Result, TEST_RESULTSET_FIELDS)
TEST_RESULTSET = SparqlResultList(('one', 'two'), itertools.imap(TEST_RESULTSET_RESULT, [
    (rdflib.URIRef('http://example.org/one'), _TEST_BNODE),
    (rdflib.Literal('hello'), rdflib.Literal('hello', lang='en')),
    (rdflib.Literal('foo"bar'), rdflib.Literal('foo\nbar')),
    (rdflib.Literal('foo,bar'), rdflib.Literal('foo;bar')),
    (rdflib.Literal('foo bar'), rdflib.Literal('foo\tbar')),
    (rdflib.Literal(1), rdflib.Literal('2011-01-02T12:34:56Z', datatype=NS.xsd.timeDate)),
    (None, None),
    (_TEST_BNODE, rdflib.BNode()),
    (rdflib.URIRef('http://example.org/'), rdflib.URIRef('mailto:alice@example.org')),
    (rdflib.URIRef('urn:isbn:9781449306595'), rdflib.URIRef('tag:bob@example.org,2011:foo')),
]))
del _TEST_BNODE
TEST_RESULTSET.fields = ('one', 'two')
TEST_RESULTSET.query = 'The query that was run'
TEST_RESULTSET.duration = 1