Exemple #1
0
class ReaderPlugin(RDFQueryReader):
    def __init__(self, *args, **kwargs):
        super(ReaderPlugin, self).__init__(*args, **kwargs)

        self._rdflib_store = kwargs.get("rdflib_store", "IOMemory")
        self._rdflib_identifier = kwargs.get("rdflib_identifier")
        self._commit_pending_transaction_on_close = \
            kwargs.get("commit_pending_transaction_on_close", True)

        self._graph = ConjunctiveGraph(
            store=self._rdflib_store,
            identifier=self._rdflib_identifier
        )

    @property
    def rdflib_store(self):
        return self._rdflib_store

    @property
    def rdflib_identifier(self):
        return self._rdflib_identifier

    @property
    def graph(self):
        return self._graph

    @property
    def commit_pending_transaction_on_close(self):
        return self._commit_pending_transaction_on_close

    def _to_table(self, result):
        # Elements in result.selectionF are instances of rdflib.Variable,
        # rdflib.Variable is subclass of unicode. We convert them to 
        # unicode here anyway to hide rdflib internals from clients. 
        vars = [unicode(var) for var in result.vars]

        # Convert each row to dict: { var->value, ... }
        return [dict(zip(vars, row)) for row in result]

    def _ask(self, result):
        # askAnswer is list with boolean values, we want first value. 
        return result.askAnswer[0]

    def _execute(self, query):
        q_string = unicode(query)
        debug(q_string)
        return self._graph.query(q_string)

    def execute_sparql(self, q_string, format=None):
        debug(q_string)

        result = self._graph.query(q_string)
        return loads(result.serialize(format='json'))

    def close(self):
        self._graph.close(commit_pending_transaction=self._commit_pending_transaction_on_close)
Exemple #2
0
class ReaderPlugin(RDFQueryReader):
    def __init__(self, *args, **kwargs):
        super(ReaderPlugin, self).__init__(*args, **kwargs)

        self._rdflib_store = kwargs.get("rdflib_store", "IOMemory")
        self._rdflib_identifier = kwargs.get("rdflib_identifier")
        self._commit_pending_transaction_on_close = \
            kwargs.get("commit_pending_transaction_on_close", True)

        self._graph = ConjunctiveGraph(
            store=self._rdflib_store,
            identifier=self._rdflib_identifier
        )

    @property
    def rdflib_store(self):
        return self._rdflib_store

    @property
    def rdflib_identifier(self):
        return self._rdflib_identifier

    @property
    def graph(self):
        return self._graph

    @property
    def commit_pending_transaction_on_close(self):
        return self._commit_pending_transaction_on_close

    def _to_table(self, result):
        # Elements in result.selectionF are instances of rdflib.Variable,
        # rdflib.Variable is subclass of unicode. We convert them to 
        # unicode here anyway to hide rdflib internals from clients. 
        vars = [str(var) for var in result.vars]

        # Convert each row to dict: { var->value, ... }
        return [dict(list(zip(vars, row))) for row in result]

    def _ask(self, result):
        # askAnswer is list with boolean values, we want first value. 
        return result.askAnswer[0]

    def _execute(self, query):
        q_string = str(query)
        debug(q_string)
        return self._graph.query(q_string)

    def execute_sparql(self, q_string, format=None):
        debug(q_string)

        result = self._graph.query(q_string)
        return loads(result.serialize(format='json'))

    def close(self):
        self._graph.close(commit_pending_transaction=self._commit_pending_transaction_on_close)
Exemple #3
0
class ReaderPlugin(RDFQueryReader):
    def __init__(self, *args, **kwargs):
        RDFQueryReader.__init__(self, *args, **kwargs)

        self.__rdflib_store = kwargs.get("rdflib_store", "IOMemory")
        self.__rdflib_identifier = kwargs.get("rdflib_identifier") 
        self.__commit_pending_transaction_on_close = \
            kwargs.get("commit_pending_transaction_on_close", True)

        self.__graph = ConjunctiveGraph(store = self.__rdflib_store,
                                        identifier = self.__rdflib_identifier)

    rdflib_store = property(lambda self: self.__rdflib_store)
    rdflib_identifier = property(lambda self: self.__rdflib_identifier)
    graph = property(lambda self: self.__graph)
    commit_pending_transaction_on_close = \
        property(lambda self: self.__commit_pending_transaction_on_close)

    def _to_table(self, result):
        vars = [unicode(var) for var in result.selectionF]
        def row_to_dict(row):
            return dict([ (vars[i], row[i]) for i in range(len(row)) ])
        return [ row_to_dict(row) for row in result ]

    def _ask(self, result):
        # askAnswer is list with boolean values, we want first value. 
        return result.askAnswer[0]

    # execute
    def _execute(self, query):
        return self.execute_sparql(unicode(query))

    def execute_sparql(self, q_string, format = None):
        self.log.debug(q_string)
        return self.__graph.query(q_string)

    def close(self):
        self.__graph.close(commit_pending_transaction = self.__commit_pending_transaction_on_close)
Exemple #4
0
class WriterPlugin(RDFWriter):
    def __init__(self, reader, *args, **kwargs):
        super(WriterPlugin, self).__init__(reader, *args, **kwargs)

        if isinstance(self.reader, ReaderPlugin):
            self._rdflib_store = self.reader.rdflib_store
            self._rdflib_identifier = self.reader.rdflib_identifier
            self._commit_pending_transaction_on_close = \
                self.reader.commit_pending_transaction_on_close

            self._graph = self.reader.graph
        else:
            self._rdflib_store = kwargs.get("rdflib_store", "IOMemory")
            self._rdflib_identifier = kwargs.get("rdflib_identifier")
            self._commit_pending_transaction_on_close = \
                kwargs.get("commit_pending_transaction_on_close", True)

            self._graph = ConjunctiveGraph(store=self._rdflib_store,
                                           identifier=self._rdflib_identifier)

            warnings.warn("Graph is not readable through the reader plugin",
                          UserWarning)

    @property
    def rdflib_store(self):
        return self._rdflib_store

    @property
    def rdflib_identifier(self):
        return self._rdflib_identifier

    @property
    def graph(self):
        return self._graph

    @property
    def commit_pending_transaction_on_close(self):
        return self._commit_pending_transaction_on_close

    def _save(self, *resources):
        for resource in resources:
            s = resource.subject
            self._remove_from_graph(s)
            for p, objs in list(resource.rdf_direct.items()):
                for o in objs:
                    self.__add(s, p, o)

        self._graph.commit()

    def _update(self, *resources):
        for resource in resources:
            s = resource.subject
            for p in resource.rdf_direct:
                self._remove_from_graph(s, p)
            for p, objs in list(resource.rdf_direct.items()):
                for o in objs:
                    self.__add(s, p, o)

        self._graph.commit()

    def _remove(self, *resources, **kwargs):
        inverse = kwargs.get("inverse")
        for resource in resources:
            self._remove_from_graph(s=resource.subject)
            if inverse:
                self._remove_from_graph(o=resource.subject)

        self._graph.commit()

    def _size(self):
        return len(self._graph)

    def _add_triple(self, s=None, p=None, o=None, context=None):
        self.__add(s, p, o, context)

    def _set_triple(self, s=None, p=None, o=None, context=None):
        self._remove_from_graph(s, p, context=context)
        self.__add(s, p, o, context)

    def _remove_triple(self, s=None, p=None, o=None, context=None):
        self._remove_from_graph(s, p, o, context)

    def __add(self, s=None, p=None, o=None, context=None):
        info('ADD: %s, %s, %s, %s' % (s, p, o, context))
        self._graph.add((s, p, o))

    def _remove_from_graph(self, s=None, p=None, o=None, context=None):
        info('REM: %s, %s, %s, %s' % (s, p, o, context))
        self._graph.remove((s, p, o))

    def index_triples(self, **kwargs):
        """
        Index triples if this functionality is present.
        
        :return: True if successful
        :rtype: bool
        """
        # TODO: can indexing be forced ?
        return False

    def load_triples(self, source=None, public_id=None, format="xml", **args):
        """
        Load files (or resources on the web) into the triple-store.

        :param str source: the source file to load RDF triples from
        :param public_id: the :mod:`rdflib` publicID
        :param str format: the source file format
        :param dict args: extra args passed to :meth:`rdflib.graph.Graph.parse`
        :return: True if successful
        :rtype: bool
        """
        if source is not None:
            debug("have %s triples, loading ...", len(self._graph))
            self._graph.parse(source,
                              publicID=public_id,
                              format=format,
                              **args)
            debug("load complete; have %s triples", len(self._graph))
            return True

        return False

    def _clear(self, context=None):
        """
        Clear the triple-store.
        """
        self._graph.remove((None, None, None))

    def close(self):
        self._graph.close(commit_pending_transaction=self.
                          _commit_pending_transaction_on_close)
Exemple #5
0
class WriterPlugin(RDFWriter):
    def __init__(self, reader, *args, **kwargs):
        RDFWriter.__init__(self, reader, *args, **kwargs)
        if isinstance(self.reader, ReaderPlugin):
            self.__rdflib_store = self.reader.rdflib_store
            self.__rdflib_identifier = self.reader.rdflib_identifier
            self.__commit_pending_transaction_on_close = \
                self.reader.commit_pending_transaction_on_close

            self.__graph = self.reader.graph
        else:
            self.__rdflib_store = kwargs.get("rdflib_store", "IOMemory")
            self.__rdflib_identifier = kwargs.get("rdflib_identifier")
            self.__commit_pending_transaction_on_close = \
                kwargs.get("commit_pending_transaction_on_close", True)

            self.__graph = ConjunctiveGraph(
                store=self.__rdflib_store, identifier=self.__rdflib_identifier)

            warnings.warn("Graph is not readable through the reader plugin",
                          UserWarning)

    rdflib_store = property(lambda self: self.__rdflib_store)
    rdflib_identifier = property(lambda self: self.__rdflib_identifier)
    graph = property(lambda self: self.__graph)
    commit_pending_transaction_on_close = \
        property(lambda self: self.__commit_pending_transaction_on_close)

    def _save(self, *resources):
        for resource in resources:
            s = resource.subject
            self.__remove(s)
            for p, objs in resource.rdf_direct.items():
                for o in objs:
                    self.__add(s, p, o)

        self.__graph.commit()

    def _update(self, *resources):
        for resource in resources:
            s = resource.subject
            for p in resource.rdf_direct:
                self.__remove(s, p)
            for p, objs in resource.rdf_direct.items():
                for o in objs:
                    self.__add(s, p, o)

        self.__graph.commit()

    def _remove(self, *resources, **kwargs):
        inverse = kwargs.get("inverse")
        for resource in resources:
            self.__remove(s=resource.subject)
            if inverse:
                self.__remove(o=resource.subject)

        self.__graph.commit()

    def _size(self):
        return len(self.__graph)

    def _add_triple(self, s=None, p=None, o=None, context=None):
        self.__add(s, p, o, context)

    def _set_triple(self, s=None, p=None, o=None, context=None):
        self.__remove(s, p, context=context)
        self.__add(s, p, o, context)

    def _remove_triple(self, s=None, p=None, o=None, context=None):
        self.__remove(s, p, o, context)

    def __add(self, s=None, p=None, o=None, context=None):
        self.log.info('ADD: %s, %s, %s, %s' % (s, p, o, context))
        self.__graph.add((s, p, o))

    def __remove(self, s=None, p=None, o=None, context=None):
        self.log.info('REM: %s, %s, %s, %s' % (s, p, o, context))
        self.__graph.remove((s, p, o))

    def index_triples(self, **kwargs):
        """ Index triples if this functionality is present.  
        
        Return `True` if successful.
        
        """

        # TODO: can indexing be forced ?
        return True

    def load_triples(self, source=None, publicID=None, format="xml", **args):
        """ Load files (or resources on the web) into the triple-store. """

        if source:
            self.__graph.parse(source,
                               publicID=publicID,
                               format=format,
                               **args)
            return True

        return False

    def _clear(self, context=None):
        """ Clear the triple-store. """

        self.__graph.remove((None, None, None))

    def close(self):
        self.__graph.close(commit_pending_transaction=self.
                           __commit_pending_transaction_on_close)
Exemple #6
0
class WriterPlugin(RDFWriter):
    def __init__(self, reader, *args, **kwargs):
        RDFWriter.__init__(self, reader, *args, **kwargs)
        if isinstance(self.reader, ReaderPlugin):
            self.__rdflib_store = self.reader.rdflib_store
            self.__rdflib_identifier = self.reader.rdflib_identifier
            self.__commit_pending_transaction_on_close = \
                self.reader.commit_pending_transaction_on_close

            self.__graph = self.reader.graph
        else:
            self.__rdflib_store = kwargs.get("rdflib_store", "IOMemory")
            self.__rdflib_identifier = kwargs.get("rdflib_identifier") 
            self.__commit_pending_transaction_on_close = \
                kwargs.get("commit_pending_transaction_on_close", True)

            self.__graph = ConjunctiveGraph(store = self.__rdflib_store, identifier = self.__rdflib_identifier)

            warnings.warn("Graph is not readable through the reader plugin", 
                          UserWarning)

    rdflib_store = property(lambda self: self.__rdflib_store)
    rdflib_identifier = property(lambda self: self.__rdflib_identifier)
    graph = property(lambda self: self.__graph)
    commit_pending_transaction_on_close = \
        property(lambda self: self.__commit_pending_transaction_on_close)


    def _save(self, *resources):
        for resource in resources:
            s = resource.subject
            self.__remove(s)
            for p, objs in resource.rdf_direct.items():
                for o in objs:
                    self.__add(s, p, o)

        self.__graph.commit()

    def _update(self, *resources):
        for resource in resources:
            s = resource.subject
            for p in resource.rdf_direct:
                self.__remove(s, p)
            for p, objs in resource.rdf_direct.items():
                for o in objs:
                    self.__add(s, p, o)

        self.__graph.commit()

    def _remove(self, *resources, **kwargs):
        inverse = kwargs.get("inverse")
        for resource in resources:
            self.__remove(s = resource.subject)
            if inverse:
                self.__remove(o = resource.subject)
            

    def _size(self):
        return len(self.__graph)

    def _add_triple(self, s = None, p = None, o = None, context = None):
        self.__add(s, p, o, context)

    def _set_triple(self, s = None, p = None, o = None, context = None):
        self.__remove(s, p, context = context)
        self.__add(s, p, o, context)

    def _remove_triple(self, s = None, p = None, o = None, context = None):
        self.__remove(s, p, o, context)

    def __add(self, s = None, p = None, o = None, context = None):
        self.log.info('ADD : ' + str(s) + ', ' + str(p) + ', ' + str(o) + ', ' + str(context))
        self.__graph.add((s, p, o))

    def __remove(self, s = None, p = None, o = None, context = None):
        self.log.info('REM : ' + str(s) + ', ' + str(p) + ', ' + str(o) + ', ' + str(context))
        self.__graph.remove((s, p, o))

    def index_triples(self, **kwargs):
        """ Index triples if this functionality is present.  
        
        Return `True` if successful.
        
        """

        # TODO: can indexing be forced ?
        return True

    def load_triples(self, source = None, publicID = None, format = "xml", **args):
        """ Load files (or resources on the web) into the triple-store.
        
        This method is kept for backward compatibility only.
        
        """
        
        if source:
            self.__graph.parse(source, publicID = publicID, format = format, **args)
            return True
        
        return False

    def _clear(self, context = None):
        """ Clear the triple-store. """

        self.__graph.remove((None, None, None))

    def close(self):
        self.__graph.close(commit_pending_transaction = self.__commit_pending_transaction_on_close)
Exemple #7
0
class WriterPlugin(RDFWriter):
    def __init__(self, reader, *args, **kwargs):
        super(WriterPlugin, self).__init__(reader, *args, **kwargs)

        if isinstance(self.reader, ReaderPlugin):
            self._rdflib_store = self.reader.rdflib_store
            self._rdflib_identifier = self.reader.rdflib_identifier
            self._commit_pending_transaction_on_close = \
                self.reader.commit_pending_transaction_on_close

            self._graph = self.reader.graph
        else:
            self._rdflib_store = kwargs.get("rdflib_store", "IOMemory")
            self._rdflib_identifier = kwargs.get("rdflib_identifier")
            self._commit_pending_transaction_on_close = \
                kwargs.get("commit_pending_transaction_on_close", True)

            self._graph = ConjunctiveGraph(store=self._rdflib_store, identifier=self._rdflib_identifier)

            warnings.warn("Graph is not readable through the reader plugin", UserWarning)

    @property
    def rdflib_store(self):
        return self._rdflib_store

    @property
    def rdflib_identifier(self):
        return self._rdflib_identifier

    @property
    def graph(self):
        return self._graph

    @property
    def commit_pending_transaction_on_close(self):
        return self._commit_pending_transaction_on_close

    def _save(self, *resources):
        for resource in resources:
            s = resource.subject
            self._remove_from_graph(s)
            for p, objs in resource.rdf_direct.items():
                for o in objs:
                    self.__add(s, p, o)

        self._graph.commit()

    def _update(self, *resources):
        for resource in resources:
            s = resource.subject
            for p in resource.rdf_direct:
                self._remove_from_graph(s, p)
            for p, objs in resource.rdf_direct.items():
                for o in objs:
                    self.__add(s, p, o)

        self._graph.commit()

    def _remove(self, *resources, **kwargs):
        inverse = kwargs.get("inverse")
        for resource in resources:
            self._remove_from_graph(s=resource.subject)
            if inverse:
                self._remove_from_graph(o=resource.subject)

        self._graph.commit()

    def _size(self):
        return len(self._graph)

    def _add_triple(self, s=None, p=None, o=None, context=None):
        self.__add(s, p, o, context)

    def _set_triple(self, s=None, p=None, o=None, context=None):
        self._remove_from_graph(s, p, context=context)
        self.__add(s, p, o, context)

    def _remove_triple(self, s=None, p=None, o=None, context=None):
        self._remove_from_graph(s, p, o, context)

    def __add(self, s=None, p=None, o=None, context=None):
        info('ADD: %s, %s, %s, %s' % (s, p, o, context))
        self._graph.add((s, p, o))

    def _remove_from_graph(self, s=None, p=None, o=None, context=None):
        info('REM: %s, %s, %s, %s' % (s, p, o, context))
        self._graph.remove((s, p, o))

    def index_triples(self, **kwargs):
        """
        Index triples if this functionality is present.
        
        :return: True if successful
        :rtype: bool
        """
        # TODO: can indexing be forced ?
        return False

    def load_triples(self, source=None, public_id=None, format="xml", **args):
        """
        Load files (or resources on the web) into the triple-store.

        :param str source: the source file to load RDF triples from
        :param public_id: the :mod:`rdflib` publicID
        :param str format: the source file format
        :param dict args: extra args passed to :meth:`rdflib.graph.Graph.parse`
        :return: True if successful
        :rtype: bool
        """
        if source is not None:
            debug("have %s triples, loading ...", len(self._graph))
            self._graph.parse(source, publicID=public_id, format=format, **args)
            debug("load complete; have %s triples", len(self._graph))
            return True

        return False

    def _clear(self, context=None):
        """
        Clear the triple-store.
        """
        self._graph.remove((None, None, None))

    def close(self):
        self._graph.close(commit_pending_transaction=self._commit_pending_transaction_on_close)