Esempio n. 1
0
    def __init__(self, reader = None, writer = None, *args, **kwargs):
        self.log = logging.getLogger(self.__class__.__name__)
        self.log.info('initializing the store')
        load_plugins()

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

        if reader:
            if reader in __readers__:
                self.reader = __readers__[reader](*args, **kwargs)
            else:
                raise PluginNotFoundException('The <%s> READER plugin was not found' % (reader))
        else:
            self.reader = RDFReader(*args, **kwargs)

        if writer:
            if writer in __writers__:
                self.writer = __writers__[writer](self.reader, *args, **kwargs)
            else:
                raise PluginNotFoundException('The <%s> WRITER plugin was not found' % (reader))
        else:
            self.writer = RDFWriter(self.reader, *args, **kwargs)

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

        self.log.info('store initialized')
Esempio n. 2
0
    def __init__(self, reader=None, writer=None, *args, **kwargs):
        self.log = logging.getLogger(self.__class__.__name__)
        self.log.info('initializing the store')
        load_plugins()

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

        if reader:
            if reader in __readers__:
                self.reader = __readers__[reader](*args, **kwargs)
            elif isinstance(reader, RDFReader):
                # We've received already configured reader, use it.
                self.reader = reader
            else:
                raise PluginNotFoundException(
                    'The <%s> READER plugin was not found' % (reader))
        else:
            self.reader = RDFReader(*args, **kwargs)

        if writer:
            if writer in __writers__:
                self.writer = __writers__[writer](self.reader, *args, **kwargs)
            elif isinstance(writer, RDFWriter):
                # We've received already configured writer, use it.
                self.writer = writer
            else:
                raise PluginNotFoundException(
                    'The <%s> WRITER plugin was not found' % (reader))
        else:
            self.writer = RDFWriter(self.reader, *args, **kwargs)

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

        self.log.info('store initialized')
Esempio n. 3
0
class Store(object):
    """ 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):
        self.log = logging.getLogger(self.__class__.__name__)
        self.log.info('initializing the store')
        load_plugins()

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

        if reader:
            if reader in __readers__:
                self.reader = __readers__[reader](*args, **kwargs)
            else:
                raise PluginNotFoundException('The <%s> READER plugin was not found' % (reader))
        else:
            self.reader = RDFReader(*args, **kwargs)

        if writer:
            if writer in __writers__:
                self.writer = __writers__[writer](self.reader, *args, **kwargs)
            else:
                raise PluginNotFoundException('The <%s> WRITER plugin was not found' % (reader))
        else:
            self.writer = RDFWriter(self.reader, *args, **kwargs)

        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 enable_logging(self, enable):
        """ Toggle `logging` on or off. """

        level = enable and logging.DEBUG or logging.NOTSET
        self.log.setLevel(level)
        self.reader.enable_logging(enable)
        self.writer.enable_logging(enable)

    def is_enable_logging(self):
        """ True if `logging` is enabled, False otherwise. """
        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('reader closed successfully')
        except Exception, e:
            self.log('error on closing the reader ' + unicode(e))
        try:
            self.writer.close()
            self.log('writer closed successfully')
        except Exception, e:
            self.log('error on closing the writer ' + unicode(e))
Esempio n. 4
0
class Store(object):
    """ 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):
        self.log = logging.getLogger(self.__class__.__name__)
        self.log.info('initializing the store')
        load_plugins()

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

        if reader:
            if reader in __readers__:
                self.reader = __readers__[reader](*args, **kwargs)
            elif isinstance(reader, RDFReader):
                # We've received already configured reader, use it.
                self.reader = reader
            else:
                raise PluginNotFoundException(
                    'The <%s> READER plugin was not found' % (reader))
        else:
            self.reader = RDFReader(*args, **kwargs)

        if writer:
            if writer in __writers__:
                self.writer = __writers__[writer](self.reader, *args, **kwargs)
            elif isinstance(writer, RDFWriter):
                # We've received already configured writer, use it.
                self.writer = writer
            else:
                raise PluginNotFoundException(
                    'The <%s> WRITER plugin was not found' % (reader))
        else:
            self.writer = RDFWriter(self.reader, *args, **kwargs)

        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 enable_logging(self, enable):
        """ Toggle `logging` on or off. """

        level = enable and logging.DEBUG or logging.NOTSET
        self.log.setLevel(level)
        self.reader.enable_logging(enable)
        self.writer.enable_logging(enable)

    def is_enable_logging(self):
        """ True if `logging` is enabled, False otherwise. """
        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, e:
            self.log.exception("Error on closing the reader")
        try:
            self.writer.close()
            self.log.debug('writer closed successfully')
        except Exception, e:
            self.log.exception("Error on closing the writer")