Exemplo n.º 1
0
    def construct(self, sparql, fmt):
        out_format = fmt
        if fmt in ('json', 'dict'):
            out_format = 'ntriples'
        ctype = self._get_mimetype(out_format)
        params = urlencode({
            'query': sparql,
            'queryLn': 'SPARQL',
            'infer': 'false'
        })

        # content: bytes
        resp = requests.get(f'{self._url}/repositories/{self._name}?{params}',
                            headers={'Accept': ctype})

        if resp.status_code != 200:
            raise QueryError(resp.status_code)

        # Allegro Graph returns status 200 when parsing failed
        if resp.text.startswith('Server error:'):
            raise QueryError(resp[14:])

        if fmt == 'json':
            return ntriples_to_json(BytesIO(to_bytes(resp)))
        elif fmt == 'dict':
            return ntriples_to_dict(BytesIO(to_bytes(resp)))
        else:
            return StringIO(resp.text)
    def construct(self, sparql, format):
        out_format = format
        if format in ['json', 'dict']:
            out_format = 'ntriples'
        ctype = self._get_mimetype(out_format)
        params = urlencode({
            'query': sparql,
            'queryLn': 'SPARQL',
            'infer': 'false'
        })

        resp, content = self._http.request('%s/repositories/%s?%s' %
                                           (self._url, self._name, params),
                                           'GET',
                                           headers={'Accept': ctype})

        if resp['status'] != '200':
            raise QueryError(content)
        # Allegro Graph returns status 200 when parsing failed
        if content.startswith('Server error:'):
            raise QueryError(content[14:])

        result = StringIO(content)
        if format == 'json':
            result = ntriples_to_json(result)
        elif format == 'dict':
            result = ntriples_to_dict(result)
        return result
Exemplo n.º 3
0
    def construct(self, query, fmt):
        result = super(AllegroTripleStore, self).construct(query, 'rdfxml')
        if fmt == 'rdfxml':
            return result

        result = self._rdfxml_to_ntriples(result)
        if fmt == 'ntriples':
            return result
        elif fmt == 'json':
            return ntriples_to_json(result)
        elif fmt == 'dict':
            return ntriples_to_dict(result)
        elif fmt == 'turtle':
            return self._ntriples_to_turtle(result)
Exemplo n.º 4
0
    def construct(self, sparql, format):
        out_format = format
        if format in ['json', 'dict']:
            out_format = 'ntriples'

        result = self._query(sparql)
        if not result.construct:
            raise QueryError('CONSTRUCT Query did not return a graph')

        result = self._serialize(result.result,
                                 self._rdflib_format(out_format))
        if format == 'json':
            result = ntriples_to_json(result)
        elif format == 'dict':
            result = ntriples_to_dict(result)
        return result
 def construct(self, sparql, format):
     out_format = format
     if format in ['json', 'dict']:
         out_format = 'ntriples'
     result = self._query(sparql)
     if not result.is_graph():
         raise QueryError('CONSTRUCT Query did not return a graph')
     
     m = RDF.Model()
     stream = result.as_stream()
     if stream is None:
         return
     result = self._serialize_stream(stream, out_format)
     if format == 'json':
         result = ntriples_to_json(result)
     elif format == 'dict':
         result = ntriples_to_dict(result)
     return result
Exemplo n.º 6
0
 def get_json(self, context_name):
     data = self.get_ntriples(context_name)
     return ntriples_to_json(data)