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
def pselect(self, qid, bindings={}): try: result = ConjunctiveGraph(self._store).query(self._pqueries[qid], initBindings=bindings) except SyntaxError, err: self.logger.error('Error in query: %s', self._querytext[qid]) raise QueryError(err)
def ask(self, sparql): params = urlencode({ 'query': sparql, 'queryLn': 'SPARQL', 'infer': 'false' }) resp = requests.get( f'{self._url}/repositories/{self._name}?{params}', headers={'Accept': 'application/sparql-results+xml'}) 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:]) return parse_sparql_result(to_bytes(resp))
def _query(self, sparql): if isinstance(sparql, unicode): sparql = sparql.encode('utf8') query = RDF.SPARQLQuery(sparql) try: result = query.execute(self._model) except RDF.RedlandError, err: self.logger.error("Error in query:\n%s",sparql) raise QueryError(err)
def ask(self, sparql): 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': 'application/sparql-results+xml'}) if resp['status'] != '200': raise QueryError(content) # Allegro Graph returns status 200 when parsing failed if content.startswith('Server error:'): raise QueryError(content[14:]) return parse_sparql_result(content)
def _query(self, sparql): # if isinstance(sparql, unicode): # sparql = sparql.encode('utf8') query = RDF.SPARQLQuery(sparql) try: result = query.execute(self._model) except RDF.RedlandError as err: raise QueryError(err) 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.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
def ask(self, sparql): result = self._query(sparql) if not result.is_boolean(): raise QueryError('ASK Query did not return a boolean') return result.get_boolean()
def select(self, sparql): result = self._query(sparql) if not result.is_bindings(): raise QueryError('SELECT Query did not return bindings') return parse_sparql_result(result.to_string())
def _query(self, sparql): try: result = ConjunctiveGraph(self._store).query(sparql) except SyntaxError, err: raise QueryError(err)