def clear(self, context):
     context = quote(self._get_context(context))
     resp, content = self._http.request(
         '%s/repositories/%s/statements?context=%s' %
         (self._url, self._name, context), 'DELETE')
     if resp['status'] != '204':
         raise TripleStoreError(content)
Example #2
0
    def add_json(self, data, context_name):
        data = self._get_file(data)
        try:
            data = json_to_ntriples(data)
        except ValueError as err:
            raise TripleStoreError(err)

        self.add_ntriples(data, context_name)
Example #3
0
    def clear(self, context):
        context = quote(self._get_context(context))
        resp = requests.delete(
            f'{self._url}/repositories/{self._name}/statements?context={context}'
        )

        if resp.status_code != 204:
            raise TripleStoreError(resp)
Example #4
0
    def _parse(self, file, format, base_uri=None):            
        if format == 'turtle':
            parser = RDF.TurtleParser()
        else:
            parser = RDF.Parser(format)

        # if isinstance(base_uri, unicode):
        #     base_uri = base_uri.encode('utf8')
        data = file.read()
        file.close()
        try:
            stream = parser.parse_string_as_stream(data, base_uri)
        except RDF.RedlandError as err:
            raise TripleStoreError(err)
        if stream is None:
            raise TripleStoreError('Parsing RDF Failed')
        return stream
Example #5
0
class RDFLibTripleStore(BaseBackend):
    implements(ITripleStore, ISPARQLEndpoint)

    def __init__(self):
        super(RDFLibTripleStore, self).__init__()
        self.logger = logging.getLogger(__name__)
        self._nsmap = {}
        self._nsobjmap = {}
        self._querytext = {}

    def add_pquery(self, qidstring, querytext):
        self._pqueries[qidstring] = prepareQuery(querytext,
                                                 initNs=self._nsobjmap)
        self._querytext[qidstring] = querytext

    def connect(self, dburi):
        if rdflib is None:
            raise ConnectionError('RDFLib backend is not installed')
        if dburi == 'memory':
            self._store = IOMemory()
        else:
            raise ConnectionError('Unknown database config: %s' % dburi)

    def disconnect(self):
        pass

    def _rdflib_format(self, format):
        return {'ntriples': 'nt', 'rdfxml': 'xml', 'turtle': 'n3'}[format]

    def contexts(self):
        return [
            c._Graph__identifier.decode('utf8')
            for c in self._store.contexts()
        ]

    def _get_context(self, context_name):
        for ctxt in self._store.contexts():
            if ctxt._Graph__identifier == context_name:
                return ctxt

    def register_prefix(self, prefix, namespace):
        self._nsmap[prefix] = namespace
        self._nsobjmap[prefix] = Namespace(namespace)

    def _parse(self, graph, file, format, base_uri=None):
        try:
            graph.parse(file, base_uri, format)
        except rdflib.exceptions.ParserError, err:
            # not sure if this ever happens
            raise TripleStoreError(err)
        except Exception, err:
            # each parser throws different errors,
            # there's an ntriples error, but the rdfxml
            # parser throws no errors so you end up with
            # a saxparser exception.
            # The n3 parser just silently fails
            # without any traceback
            raise TripleStoreError(err)
 def _turtle_to_ntriples(self, data):
     # Turtle syntax is not supported by allegro graph
     # HACK workaround using redland
     import RDF
     model = RDF.Model()
     parser = RDF.TurtleParser()
     try:
         parser.parse_string_into_model(model, data.read(), '-')
     except RDF.RedlandError, err:
         raise TripleStoreError(err)
Example #7
0
    def count(self, context=None):
        context = '?context=' + quote(
            self._get_context(context)) if context else ''
        resp = requests.get(
            f'{self._url}/repositories/{self._name}/size{context}')

        if resp.status_code != 200:
            raise TripleStoreError(resp)
        else:
            return int(resp.text)
 def _rdfxml_to_ntriples(self, data):
     # Ntriples syntax is not supported by allegro graph
     # as a result format for SPARQL Construct Queries
     # HACK workaround using redland
     import RDF
     model = RDF.Model()
     parser = RDF.Parser()
     try:
         parser.parse_string_into_model(model, data.read(), '-')
     except RDF.RedlandError, err:
         raise TripleStoreError(err)
class RedlandTripleStore(BaseBackend):
    implements(ITripleStore, ISPARQLEndpoint)
    
    def __init__(self):
        super(RedlandTripleStore, self).__init__()
        self.logger = logging.getLogger(__name__)
        
    def connect(self, dburi):
        self._model = model_from_uri(dburi, contexts='yes')

    def disconnect(self):
        del self._model

    def contexts(self):
        return [c.literal_value['string'].decode('utf8') for c in
                self._model.get_contexts()]

    def register_prefix(self, prefix, namespace):
        self._nsmap[prefix] = namespace

    def add_rdfxml(self, data, context_name, base_uri):
        data = self._get_file(data)
        self._add_stream(self._parse(data, 'rdfxml', base_uri), context_name)
        
    def add_ntriples(self, data, context_name):
        data = self._get_file(data)
        self._add_stream(self._parse(data, 'ntriples', '-'), context_name)

    def add_turtle(self, data, context_name):
        data = self._get_file(data)
        self._add_stream(self._parse(data, 'turtle', '-'), context_name)

    def _add_stream(self, stream, context_name):
        if isinstance(context_name, unicode):
            context_name = context_name.encode('utf8')
        self._model.add_statements(stream, RDF.Node(context_name))
        
    def _parse(self, file, format, base_uri=None):            
        if format == 'turtle':
            parser = RDF.TurtleParser()
        else:
            parser = RDF.Parser(format)

        if isinstance(base_uri, unicode):
            base_uri = base_uri.encode('utf8')
        data = file.read()
        file.close()
        try:
            stream = parser.parse_string_as_stream(data, base_uri)
        except RDF.RedlandError, err:
            raise TripleStoreError(err)
        if stream is None:
            raise TripleStoreError('Parsing RDF Failed')
        return stream
 def count(self, context=None):
     if context is None:
         context = ''
     else:
         context = '?context=' + quote(self._get_context(context))
     resp, content = self._http.request(
         '%s/repositories/%s/size%s' % (self._url, self._name, context),
         'GET')
     if resp['status'] != '200':
         raise TripleStoreError(content)
     return int(content)
Example #11
0
    def register_prefix(self, prefix, namespace):
        # store also in _nsmap for allegro turtle workaround
        self._nsmap[prefix] = namespace

        content_len = str(len(namespace))
        resp = requests.put(
            f'{self._url}/repositories/{self._name}/namespaces/{prefix}',
            data=namespace,
            headers={"Content-length": content_len})

        if resp.status_code != 204:
            raise TripleStoreError(resp)
Example #12
0
    def _serialize(self, format, context, pretty=False):
        quoted_context = quote(self._get_context(context))
        ctype = self._get_mimetype(format)

        resp = requests.get(
            f'{self._url}/repositories/{self._name}/statements?context={quoted_context}',
            headers={"Accept": ctype})

        if resp.status_code != 200:
            raise TripleStoreError(resp.status_code)
        else:
            return StringIO(resp.text)
 def _ntriples_to_turtle(self, data):
     # Turtle syntax is not supported by allegro graph
     # HACK workaround using redland
     import RDF
     model = RDF.Model()
     parser = RDF.Parser('ntriples')
     data = data.read()
     data = (data.strip() + '\n')
     try:
         parser.parse_string_into_model(model, data, '-')
     except RDF.RedlandError, err:
         raise TripleStoreError(err)
    def register_prefix(self, prefix, namespace):
        # store also in _nsmap for allegro turtle workaround
        self._nsmap[prefix] = namespace

        clength = str(len(namespace))
        resp, content = self._http.request('%s/repositories/%s/namespaces/%s' %
                                           (self._url, self._name, prefix),
                                           'PUT',
                                           body=namespace,
                                           headers={"Content-length": clength})
        if resp['status'] != '204':
            raise TripleStoreError(content)
    def _serialize(self, format, context, pretty=False):

        context = quote(self._get_context(context))
        ctype = self._get_mimetype(format)

        resp, content = self._http.request(
            '%s/repositories/%s/statements?context=%s' %
            (self._url, self._name, context),
            'GET',
            headers={"Accept": ctype})
        if resp['status'] != '200':
            raise TripleStoreError(content)

        return StringIO(content)
Example #16
0
    def _ntriples_to_turtle(self, data):
        # Turtle syntax is not supported by allegro graph
        # HACK workaround using redland
        import RDF
        model = RDF.Model()
        parser = RDF.Parser('ntriples')
        data = data.read()
        data = (data.strip() + '\n')
        try:
            parser.parse_string_into_model(model, data, '-')
        except RDF.RedlandError as err:
            raise TripleStoreError(err)

        serializer = RDF.Serializer(name='turtle')
        for prefix, ns in self._nsmap.items():
            serializer.set_namespace(prefix, ns)
        return StringIO(serializer.serialize_model_to_string(model))
Example #17
0
    def _remove(self, file, format, context, base_uri=None):
        data = file.read()
        file.close()
        clength = str(len(data))
        ctype = self._get_mimetype(format)
        params = {'context': self._get_context(context)}
        if base_uri:
            params['baseURI'] = '<%s>' % base_uri
        params = urlencode(params)
        content = requests.delete(
            f'{self._url}/repositories/{self._name}/statements?{params}',
            data=data,
            headers={
                "Content-type": ctype,
                "Content-length": clength
            })

        if content.status_code != 204:
            raise TripleStoreError(content)
 def _remove(self, file, format, context, base_uri=None):
     data = file.read()
     file.close()
     clength = str(len(data))
     ctype = self._get_mimetype(format)
     params = {'context': self._get_context(context)}
     if base_uri:
         params['baseURI'] = '<%s>' % base_uri
     params = urlencode(params)
     resp, content = self._http.request('%s/repositories/%s/statements?%s' %
                                        (self._url, self._name, params),
                                        'DELETE',
                                        body=data,
                                        headers={
                                            "Content-type": ctype,
                                            "Content-length": clength
                                        })
     if resp['status'] != '204':
         raise TripleStoreError(content)
Example #19
0
 def _parse(self, graph, file, format, base_uri=None):
     try:
         graph.parse(file, base_uri, format)
     except rdflib.exceptions.ParserError, err:
         # not sure if this ever happens
         raise TripleStoreError(err)