Ejemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     RDFReader.__init__(self, *args, **kwargs)
     self.use_subqueries = kwargs.get('use_subqueries', False)
     if type(self.use_subqueries) in [str, tuple]:
         self.use_subqueries = (self.use_subqueries.lower() == 'true')
     elif type(self.use_subqueries) is not bool:
         raise ValueError('The use_subqueries parameter must be a bool or a string set to "true" or "false"')
Ejemplo n.º 2
0
    def __init__(self, reader = None, writer = None, *args, **kwargs):
        super(Store, self).__init__()
        self.log_level = kwargs.get('log_level', logging.NOTSET)

        self.log.info('initializing the store')
        load_plugins(logger=self.log)

        self.__default_context = None
        if "default_context" in kwargs:
            self.__default_context = URIRef(kwargs["default_context"])

        if reader:
            self.reader = reader if isinstance(reader, RDFReader) else get_reader(reader, *args, **kwargs)
        else:
            self.reader = RDFReader(*args, **kwargs)
        self.reader.log_level = self.log_level

        if writer:
            self.writer = writer if isinstance(writer, RDFWriter) else get_writer(writer, self.reader, *args, **kwargs)
        else:
            self.writer = RDFWriter(self.reader, *args, **kwargs)
        self.writer.log_level = self.log_level

        if hasattr(self.reader, 'use_subqueries'):
            self.use_subqueries = property(fget = lambda self: self.reader.use_subqueries)

        self.log.info('store initialized')
Ejemplo n.º 3
0
 def __init__(self, *args, **kwargs):
     RDFReader.__init__(self, *args, **kwargs)
     self.use_subqueries = kwargs.get('use_subqueries', False)
     if type(self.use_subqueries) in [str, tuple]:
         self.use_subqueries = (self.use_subqueries.lower() == 'true')
     elif type(self.use_subqueries) is not bool:
         raise ValueError('The use_subqueries parameter must be a bool or a string set to "true" or "false"')
Ejemplo n.º 4
0
class Store(LogMixin):
    """ The `Store` class is comprised of a reader and a writer, getting
    access to an underlying triple store. Also store specific parameters must
    be handled by the class, the plugins act based on various settings.

    The `Store` is also the `plugin` manager and provides convenience methods
    for working with plugins.

    """

    """ True if the `reader` plugin is using sub queries, False otherwise. """
    use_subqueries = False

    default_context = property(lambda self: self.__default_context)

    def __init__(self, reader = None, writer = None, *args, **kwargs):
        super(Store, self).__init__()
        self.log_level = kwargs.get('log_level', logging.NOTSET)

        self.log.info('initializing the store')
        load_plugins(logger=self.log)

        self.__default_context = None
        if "default_context" in kwargs:
            self.__default_context = URIRef(kwargs["default_context"])

        if reader:
            self.reader = reader if isinstance(reader, RDFReader) else get_reader(reader, *args, **kwargs)
        else:
            self.reader = RDFReader(*args, **kwargs)
        self.reader.log_level = self.log_level

        if writer:
            self.writer = writer if isinstance(writer, RDFWriter) else get_writer(writer, self.reader, *args, **kwargs)
        else:
            self.writer = RDFWriter(self.reader, *args, **kwargs)
        self.writer.log_level = self.log_level

        if hasattr(self.reader, 'use_subqueries'):
            self.use_subqueries = property(fget = lambda self: self.reader.use_subqueries)

        self.log.info('store initialized')


    def __add_default_context(self, context):
        """ Return default context if context is None. """

        if context == NO_CONTEXT:
            context = None
        elif not context:
            context = self.__default_context

        return context

    def _set_level(self, level):
        self.log.setLevel(level)
        self.reader.log_level = level
        self.writer.log_level = level

    @deprecated
    def enable_logging(self, enable):
        """ Toggle `logging` on or off. """
        #TODO: -------------------[remove in v1.2.0]------------------------
#        deprecation('the enable_logging method will be removed in version 1.2.0, use the logging and log_level properties instead!')
        #TODO: -------------------[remove in v1.2.0]------------------------
        level = enable and logging.DEBUG or logging.NOTSET
        self.log.setLevel(level)
        self.reader.enable_logging(enable)
        self.writer.enable_logging(enable)

    @deprecated
    def is_enable_logging(self):
        """ True if `logging` is enabled, False otherwise. """
        #TODO: -------------------[remove in v1.2.0]------------------------
#        deprecation('the is_enabled_logging method will be removed in version 1.2.0, use the logging and log_level properties instead!')
        #TODO: -------------------[remove in v1.2.0]------------------------
        return (self.log.level == logging.DEBUG)

    def close(self):
        """ Close the `store`.

        Both the `reader` and the `writer` plugins are closed.
        See :func:`surf.plugin.writer.RDFWriter.close`
        and :func:`surf.plugin.reader.RDFReader.close` methods.

        """

        try:
            self.reader.close()
            self.log.debug('reader closed successfully')
        except Exception:
            self.log.exception("Error on closing the reader")

        try:
            self.writer.close()
            self.log.debug('writer closed successfully')
        except Exception:
            self.log.exception("Error on closing the writer")

    #---------------------------------------------------------------------------
    # the reader interface
    #---------------------------------------------------------------------------

    def get(self, resource, attribute, direct):
        """ :func:`surf.plugin.reader.RDFReader.get` method. """

        return self.reader.get(resource, attribute, direct)

    # cRud
    def load(self, resource, direct):
        """ :func:`surf.plugin.reader.RDFReader.load` method. """

        return self.reader.load(resource, direct)

    def is_present(self, resource):
        """ :func:`surf.plugin.reader.RDFReader.is_present` method. """

        return self.reader.is_present(resource)

    def concept(self, resource):
        """ :func:`surf.plugin.reader.RDFReader.concept` method. """

        return self.reader.concept(resource)

    def instances_by_attribute(self, resource, attributes, direct, context):
        """ :func:`surf.plugin.reader.RDFReader.instances_by_attribute` method. """

        context = self.__add_default_context(context)
        return self.reader.instances_by_attribute(resource, attributes,
                                                  direct, context)

    def get_by(self, params):
        params["context"] = self.__add_default_context(params.get("context"))
        return self.reader.get_by(params)

    #---------------------------------------------------------------------------
    # the query reader interface
    #---------------------------------------------------------------------------

    def execute(self, query):
        """see :meth:`surf.plugin.query_reader.RDFQueryReader.execute` method. """

        if hasattr(self.reader, 'execute') and isinstance(query, Query):
            return self.reader.execute(query)

        return None

    def execute_sparql(self, sparql_query, format = 'JSON'):
        """see :meth:`surf.plugin.query_reader.RDFQueryReader.execute_sparql` method. """

        if hasattr(self.reader, 'execute_sparql') and type(sparql_query) in [str, unicode]:
            return self.reader.execute_sparql(sparql_query, format = format)
        return None

    #---------------------------------------------------------------------------
    # the writer interface
    #---------------------------------------------------------------------------

    def clear(self, context = None):
        """ See :func:`surf.plugin.writer.RDFWriter.clear` method. """

        context = self.__add_default_context(context)
        self.writer.clear(context = context)

    # Crud
    def save(self, *resources):
        """ See :func:`surf.plugin.writer.RDFWriter.save` method. """

        self.writer.save(*resources)

        for resource in resources:
            resource.dirty = False

    # crUd
    def update(self, *resources):
        """ See :func:`surf.plugin.writer.RDFWriter.update` method. """

        self.writer.update(*resources)

        for resource in resources:
            resource.dirty = False

    # cruD
    def remove(self, *resources, **kwargs):
        """ See :func:`surf.plugin.writer.RDFWriter.remove` method. """

        self.writer.remove(*resources, **kwargs)

        for resource in resources:
            resource.dirty = False


    def size(self):
        """ See :func:`surf.plugin.writer.RDFWriter.size` method. """

        return self.writer.size()

    # triple level access methods
    def add_triple(self, s = None, p = None, o = None, context = None):
        """ See :func:`surf.plugin.writer.RDFWriter.add_triple` method. """

        context = self.__add_default_context(context)
        self.writer.add_triple(s = s, p = p, o = o, context = context)

    def set_triple(self, s = None, p = None, o = None, context = None):
        """ See :func:`surf.plugin.writer.RDFWriter.set_triple` method. """

        context = self.__add_default_context(context)
        self.writer.set_triple(s = s, p = p, o = o, context = context)

    def remove_triple(self, s = None, p = None, o = None, context = None):
        """ See :func:`surf.plugin.writer.RDFWriter.remove_triple` method. """

        context = self.__add_default_context(context)
        self.writer.remove_triple(s = s, p = p, o = o, context = context)

    def index_triples(self, **kwargs):
        """ See :func:`surf.plugin.writer.RDFWriter.index_triples` method. """

        return self.writer.index_triples(**kwargs)

    def load_triples(self, context=None, **kwargs):
        """ See :func:`surf.plugin.writer.RDFWriter.load_triples` method. """

        context = self.__add_default_context(context)
        return self.writer.load_triples(context=context, **kwargs)