Esempio n. 1
0
    def __init__(self, extra_config=None):
        self.outgoing_message_queue = Queue()
        if extra_config is None:
            extra_config = {}
        # make up a config.
        tempdir = mkdtemp()
        # reset the config every time
        sys.modules.pop('errbot.config-template', None)
        __import__('errbot.config-template')
        config = ShallowConfig()
        config.__dict__.update(sys.modules['errbot.config-template'].__dict__)
        bot_config_defaults(config)

        # It injects itself as a plugin. Changed the name to be sure we distinguish it.
        self.name = 'DummyBackendRealName'

        config.BOT_DATA_DIR = tempdir
        config.BOT_LOG_FILE = tempdir + sep + 'log.txt'
        config.BOT_PLUGIN_INDEXES = tempdir + sep + 'repos.json'
        config.BOT_EXTRA_PLUGIN_DIR = []
        config.BOT_LOG_LEVEL = logging.DEBUG
        config.BOT_IDENTITY = {'username': '******'}
        config.BOT_ASYNC = False
        config.BOT_PREFIX = '!'
        config.CHATROOM_FN = 'blah'

        # Writeout the made up repos file
        with open(config.BOT_PLUGIN_INDEXES, "w") as index_file:
            index_file.write(SIMPLE_JSON_PLUGINS_INDEX)

        for key in extra_config:
            setattr(config, key, extra_config[key])
        super().__init__(config)
        self.bot_identifier = self.build_identifier('err')
        self.md = text()  # We just want simple text for testing purposes

        # setup a memory based storage
        spm = SpecificPluginManager(config, 'storage', StoragePluginBase, CORE_STORAGE, None)
        storage_plugin = spm.get_plugin_by_name('Memory')

        # setup the plugin_manager just internally
        botplugins_dir = os.path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
        if not os.path.exists(botplugins_dir):
            os.makedirs(botplugins_dir, mode=0o755)

        # get it back from where we publish it.
        repo_index_paths = (os.path.join(os.path.dirname(__file__), '..', 'docs', '_extra', 'repos.json'),)
        repo_manager = BotRepoManager(storage_plugin,
                                      botplugins_dir,
                                      repo_index_paths)
        self.attach_storage_plugin(storage_plugin)
        self.attach_repo_manager(repo_manager)
        self.attach_plugin_manager(BotPluginManager(storage_plugin,
                                                    repo_manager,
                                                    config.BOT_EXTRA_PLUGIN_DIR,
                                                    config.AUTOINSTALL_DEPS,
                                                    getattr(config, 'CORE_PLUGINS', None),
                                                    getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, ))))
        self.inject_commands_from(self)
        self.inject_command_filters_from(ACLS(self))
Esempio n. 2
0
    def __init__(self, extra_config=None):
        self.outgoing_message_queue = Queue()
        if extra_config is None:
            extra_config = {}
        # make up a config.
        tempdir = mkdtemp()
        # reset the config every time
        sys.modules.pop('errbot.config-template', None)
        __import__('errbot.config-template')
        config = ShallowConfig()
        config.__dict__.update(sys.modules['errbot.config-template'].__dict__)
        bot_config_defaults(config)

        # It injects itself as a plugin. Changed the name to be sure we distinguish it.
        self.name = 'DummyBackendRealName'

        config.BOT_DATA_DIR = tempdir
        config.BOT_LOG_FILE = tempdir + sep + 'log.txt'
        config.BOT_PLUGIN_INDEXES = tempdir + sep + 'repos.json'
        config.BOT_EXTRA_PLUGIN_DIR = []
        config.BOT_LOG_LEVEL = logging.DEBUG
        config.BOT_IDENTITY = {'username': '******'}
        config.BOT_ASYNC = False
        config.BOT_PREFIX = '!'
        config.CHATROOM_FN = 'blah'

        # Writeout the made up repos file
        with open(config.BOT_PLUGIN_INDEXES, "w") as index_file:
            index_file.write(SIMPLE_JSON_PLUGINS_INDEX)

        for key in extra_config:
            setattr(config, key, extra_config[key])
        super().__init__(config)
        self.bot_identifier = self.build_identifier('err')
        self.md = text()  # We just want simple text for testing purposes

        # setup a memory based storage
        spm = SpecificPluginManager(config, 'storage', StoragePluginBase, CORE_STORAGE, None)
        storage_plugin = spm.get_plugin_by_name('Memory')

        # setup the plugin_manager just internally
        botplugins_dir = os.path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
        if not os.path.exists(botplugins_dir):
            os.makedirs(botplugins_dir, mode=0o755)

        # get it back from where we publish it.
        repo_index_paths = (os.path.join(os.path.dirname(__file__), '..', 'docs', '_extra', 'repos.json'),)
        repo_manager = BotRepoManager(storage_plugin,
                                      botplugins_dir,
                                      repo_index_paths)
        self.attach_storage_plugin(storage_plugin)
        self.attach_repo_manager(repo_manager)
        self.attach_plugin_manager(BotPluginManager(storage_plugin,
                                                    repo_manager,
                                                    config.BOT_EXTRA_PLUGIN_DIR,
                                                    config.AUTOINSTALL_DEPS,
                                                    getattr(config, 'CORE_PLUGINS', None),
                                                    getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, ))))
        self.inject_commands_from(self)
        self.inject_command_filters_from(ACLS(self))
Esempio n. 3
0
    def __init__(self, extra_config=None):
        self.outgoing_message_queue = Queue()
        if extra_config is None:
            extra_config = {}
        # make up a config.
        tempdir = mkdtemp()
        # reset the config every time
        sys.modules.pop("errbot.config-template", None)
        __import__("errbot.config-template")
        config = ShallowConfig()
        config.__dict__.update(sys.modules["errbot.config-template"].__dict__)
        bot_config_defaults(config)
        config.BOT_DATA_DIR = tempdir
        config.BOT_LOG_FILE = tempdir + sep + "log.txt"
        config.BOT_PLUGIN_INDEXES = tempdir + sep + "repos.json"
        config.BOT_EXTRA_PLUGIN_DIR = []
        config.BOT_LOG_LEVEL = logging.DEBUG
        config.BOT_IDENTITY = {"username": "******"}
        config.BOT_ASYNC = False
        config.BOT_PREFIX = "!"
        config.CHATROOM_FN = "blah"

        # Writeout the made up repos file
        with open(config.BOT_PLUGIN_INDEXES, "w") as index_file:
            index_file.write(SIMPLE_JSON_PLUGINS_INDEX)

        for key in extra_config:
            setattr(config, key, extra_config[key])
        super().__init__(config)
        self.bot_identifier = self.build_identifier("err")
        self.md = text()  # We just want simple text for testing purposes

        # setup a memory based storage
        spm = SpecificPluginManager(config, "storage", StoragePluginBase, CORE_STORAGE, None)
        storage_plugin = spm.get_plugin_by_name("Memory")

        # setup the plugin_manager just internally
        botplugins_dir = os.path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
        if not os.path.exists(botplugins_dir):
            os.makedirs(botplugins_dir, mode=0o755)

        # get it back from where we publish it.
        repo_index_paths = (os.path.join(os.path.dirname(__file__), "..", "docs", "_extra", "repos.json"),)
        repo_manager = BotRepoManager(storage_plugin, botplugins_dir, repo_index_paths)
        self.attach_storage_plugin(storage_plugin)
        self.attach_repo_manager(repo_manager)
        self.attach_plugin_manager(
            BotPluginManager(
                storage_plugin,
                repo_manager,
                config.BOT_EXTRA_PLUGIN_DIR,
                config.AUTOINSTALL_DEPS,
                getattr(config, "CORE_PLUGINS", None),
                getattr(config, "PLUGINS_CALLBACK_ORDER", (None,)),
            )
        )
        self.inject_commands_from(self)
        self.inject_command_filters_from(ACLS(self))
Esempio n. 4
0
def enumerate_backends(config):
    """ Returns all the backends found for the given config.
    """
    bpm = SpecificPluginManager(config,
                                'backends',
                                ErrBot,
                                CORE_BACKENDS,
                                extra_search_dirs=())
    return [plug.name for (_, _, plug) in bpm.getPluginCandidates()]
Esempio n. 5
0
def enumerate_backends(config):
    """ Returns all the backends found for the given config.
    """
    bpm = SpecificPluginManager(
            config,
            'backends',
            ErrBot,
            CORE_BACKENDS,
            extra_search_dirs=())
    return [plug.name for (_, _, plug) in bpm.getPluginCandidates()]
Esempio n. 6
0
 def test_builtins(self):
     bpm = SpecificPluginManager({},
                                 'backends',
                                 ErrBot,
                                 CORE_BACKENDS,
                                 extra_search_dirs=())
     backend_plug = bpm.getPluginCandidates()
     names = [plug.name for (_, _, plug) in backend_plug]
     assert 'Text' in names
     assert 'Test' in names
     assert 'Null' in names
Esempio n. 7
0
def test_builtins():
    bpm = SpecificPluginManager(
            {},
            'backends',
            ErrBot,
            CORE_BACKENDS,
            extra_search_dirs=())
    backend_plug = bpm.getPluginCandidates()
    names = [plug.name for (_, _, plug) in backend_plug]
    assert 'Text' in names
    assert 'Test' in names
    assert 'Null' in names
Esempio n. 8
0
def get_storage_plugin(config):
    """
    Find and load the storage plugin
    :param config: the bot configuration.
    :return: the storage plugin
    """
    storage_name = getattr(config, 'STORAGE', 'Shelf')
    extra_storage_plugins_dir = getattr(config, 'BOT_EXTRA_STORAGE_PLUGINS_DIR', None)
    spm = SpecificPluginManager(config, 'storage', StoragePluginBase, CORE_STORAGE, extra_storage_plugins_dir)
    storage_pluginfo = spm.get_candidate(storage_name)
    log.info("Found Storage plugin: '%s'\nDescription: %s" % (storage_pluginfo.name, storage_pluginfo.description))
    storage_plugin = spm.get_plugin_by_name(storage_name)
    return storage_plugin
Esempio n. 9
0
def get_storage_plugin(config):
    """
    Find and load the storage plugin
    :param config: the bot configuration.
    :return: the storage plugin
    """
    storage_name = getattr(config, 'STORAGE', 'Shelf')
    extra_storage_plugins_dir = getattr(config, 'BOT_EXTRA_STORAGE_PLUGINS_DIR', None)
    spm = SpecificPluginManager(config, 'storage', StoragePluginBase, CORE_STORAGE, extra_storage_plugins_dir)
    storage_pluginfo = spm.get_candidate(storage_name)
    log.info("Found Storage plugin: '%s'\nDescription: %s" % (storage_pluginfo.name, storage_pluginfo.description))
    storage_plugin = spm.get_plugin_by_name(storage_name)
    return storage_plugin
Esempio n. 10
0
    def test_storage(self):
        key = b'test' if PY2 else 'test'
        config = sys.modules['errbot.config-template']
        bot_config_defaults(config)

        spm = SpecificPluginManager(config, 'storage', StoragePluginBase, CORE_STORAGE, None)
        storage_plugin = spm.get_plugin_by_name('Memory')

        persistent_object = StoreMixin()
        persistent_object.open_storage(storage_plugin, 'test')
        persistent_object[key] = 'à value'
        self.assertEquals(persistent_object[key], 'à value')
        self.assertIn(key, persistent_object)
        del persistent_object[key]
        self.assertNotIn(key, persistent_object)
        self.assertEquals(len(persistent_object), 0)
Esempio n. 11
0
    def __init__(self, extra_config=None):
        self.outgoing_message_queue = Queue()
        if extra_config is None:
            extra_config = {}
        # make up a config.
        tempdir = mkdtemp()
        # reset the config every time
        sys.modules.pop("errbot.config-template", None)
        __import__("errbot.config-template")
        config = sys.modules["errbot.config-template"]
        bot_config_defaults(config)
        config.BOT_DATA_DIR = tempdir
        config.BOT_LOG_FILE = tempdir + sep + "log.txt"
        config.BOT_EXTRA_PLUGIN_DIR = []
        config.BOT_LOG_LEVEL = logging.DEBUG
        config.BOT_IDENTITY = {"username": "******"}
        config.BOT_ASYNC = False
        config.BOT_PREFIX = "!"
        config.CHATROOM_FN = "blah"

        for key in extra_config:
            setattr(config, key, extra_config[key])
        super().__init__(config)
        self.bot_identifier = self.build_identifier("err")
        self.md = text()  # We just want simple text for testing purposes

        # setup a memory based storage
        spm = SpecificPluginManager(config, "storage", StoragePluginBase, CORE_STORAGE, None)
        storage_plugin = spm.get_plugin_by_name("Memory")

        # setup the plugin_manager just internally
        botplugins_dir = os.path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
        if not os.path.exists(botplugins_dir):
            os.makedirs(botplugins_dir, mode=0o755)

        self.attach_plugin_manager(
            BotPluginManager(
                storage_plugin,
                botplugins_dir,
                config.BOT_EXTRA_PLUGIN_DIR,
                config.AUTOINSTALL_DEPS,
                getattr(config, "CORE_PLUGINS", None),
            )
        )
        self.attach_storage_plugin(storage_plugin)
        self.inject_commands_from(self)
        self.inject_command_filters_from(ACLS(self))
Esempio n. 12
0
    def test_storage(self):
        key = b'test' if PY2 else 'test'
        config = sys.modules['errbot.config-template']
        bot_config_defaults(config)

        spm = SpecificPluginManager(config, 'storage', StoragePluginBase,
                                    CORE_STORAGE, None)
        storage_plugin = spm.get_plugin_by_name('Memory')

        persistent_object = StoreMixin()
        persistent_object.open_storage(storage_plugin, 'test')
        persistent_object[key] = 'à value'
        self.assertEquals(persistent_object[key], 'à value')
        self.assertIn(key, persistent_object)
        del persistent_object[key]
        self.assertNotIn(key, persistent_object)
        self.assertEquals(len(persistent_object), 0)
Esempio n. 13
0
def bpm_from_config(config):
    """Creates a backend plugin manager from a given config."""
    extra = getattr(config, 'BOT_EXTRA_BACKEND_DIR', [])
    return SpecificPluginManager(config,
                                 'backends',
                                 ErrBot,
                                 CORE_BACKENDS,
                                 extra_search_dirs=extra)
Esempio n. 14
0
    def __init__(self, extra_config=None):
        self.outgoing_message_queue = Queue()
        if extra_config is None:
            extra_config = {}
        # make up a config.
        tempdir = mkdtemp()
        # reset the config every time
        sys.modules.pop('errbot.config-template', None)
        __import__('errbot.config-template')
        config = sys.modules['errbot.config-template']
        bot_config_defaults(config)
        config.BOT_DATA_DIR = tempdir
        config.BOT_LOG_FILE = tempdir + sep + 'log.txt'
        config.BOT_EXTRA_PLUGIN_DIR = []
        config.BOT_LOG_LEVEL = logging.DEBUG
        config.BOT_IDENTITY = {'username': '******'}
        config.BOT_ASYNC = False
        config.BOT_PREFIX = '!'
        config.CHATROOM_FN = 'blah'

        for key in extra_config:
            setattr(config, key, extra_config[key])
        super().__init__(config)
        self.bot_identifier = self.build_identifier('err')
        self.md = text()  # We just want simple text for testing purposes

        # setup a memory based storage
        spm = SpecificPluginManager(config, 'storage', StoragePluginBase,
                                    CORE_STORAGE, None)
        storage_plugin = spm.get_plugin_by_name('Memory')

        # setup the plugin_manager just internally
        botplugins_dir = os.path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
        if not os.path.exists(botplugins_dir):
            os.makedirs(botplugins_dir, mode=0o755)

        self.attach_plugin_manager(
            BotPluginManager(storage_plugin, botplugins_dir,
                             config.BOT_EXTRA_PLUGIN_DIR,
                             config.AUTOINSTALL_DEPS,
                             getattr(config, 'CORE_PLUGINS', None)))
        self.attach_storage_plugin(storage_plugin)
        self.inject_commands_from(self)
        self.inject_command_filters_from(ACLS(self))
Esempio n. 15
0
def test_storage():
    key = 'test'

    __import__('errbot.config-template')
    config = ShallowConfig()
    config.__dict__.update(sys.modules['errbot.config-template'].__dict__)
    bot_config_defaults(config)

    spm = SpecificPluginManager(config, 'storage', StoragePluginBase, CORE_STORAGE, None)
    storage_plugin = spm.get_plugin_by_name('Memory')

    persistent_object = StoreMixin()
    persistent_object.open_storage(storage_plugin, 'test')
    persistent_object[key] = 'à value'
    assert persistent_object[key] == 'à value'
    assert key in persistent_object
    del persistent_object[key]
    assert key not in persistent_object
    assert len(persistent_object) == 0
Esempio n. 16
0
def test_storage():
    key = 'test'

    __import__('errbot.config-template')
    config = ShallowConfig()
    config.__dict__.update(sys.modules['errbot.config-template'].__dict__)
    bot_config_defaults(config)

    spm = SpecificPluginManager(config, 'storage', StoragePluginBase,
                                CORE_STORAGE, None)
    storage_plugin = spm.get_plugin_by_name('Memory')

    persistent_object = StoreMixin()
    persistent_object.open_storage(storage_plugin, 'test')
    persistent_object[key] = 'à value'
    assert persistent_object[key] == 'à value'
    assert key in persistent_object
    del persistent_object[key]
    assert key not in persistent_object
    assert len(persistent_object) == 0
Esempio n. 17
0
def setup_bot(backend_name, logger, config, restore=None):
    # from here the environment is supposed to be set (daemon / non daemon,
    # config.py in the python path )

    from .errBot import bot_config_defaults

    bot_config_defaults(config)

    if config.BOT_LOG_FILE:
        hdlr = logging.FileHandler(config.BOT_LOG_FILE)
        hdlr.setFormatter(logging.Formatter("%(asctime)s %(levelname)-8s %(name)-25s %(message)s"))
        logger.addHandler(hdlr)

    if config.BOT_LOG_SENTRY:
        try:
            from raven.handlers.logging import SentryHandler
        except ImportError:
            log.exception(
                "You have BOT_LOG_SENTRY enabled, but I couldn't import modules "
                "needed for Sentry integration. Did you install raven? "
                "(See http://raven.readthedocs.org/en/latest/install/index.html "
                "for installation instructions)"
            )
            exit(-1)

        sentryhandler = SentryHandler(config.SENTRY_DSN, level=config.SENTRY_LOGLEVEL)
        logger.addHandler(sentryhandler)

    logger.setLevel(config.BOT_LOG_LEVEL)

    # init the storage plugin
    storage_name = getattr(config, 'STORAGE', 'Shelf')
    extra_storage_plugins_dir = getattr(config, 'EXTRA_STORAGE_PLUGINS_DIR', None)
    spm = SpecificPluginManager(config, 'storage', StoragePluginBase, CORE_STORAGE, extra_storage_plugins_dir)
    storage_pluginfo = spm.get_candidate(storage_name)
    log.info("Found Storage plugin: '%s'\nDescription: %s" % (storage_pluginfo.name, storage_pluginfo.description))
    storage_plugin = spm.get_plugin_by_name(storage_name)

    # init the botplugin manager
    botplugins_dir = path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
    if not path.exists(botplugins_dir):
        makedirs(botplugins_dir, mode=0o755)
    botpm = BotPluginManager(storage_plugin,
                             botplugins_dir,
                             config.BOT_EXTRA_PLUGIN_DIR,
                             config.AUTOINSTALL_DEPS,
                             getattr(config, 'CORE_PLUGINS', None))

    # init the backend manager & the bot
    extra = getattr(config, 'BOT_EXTRA_BACKEND_DIR', [])
    backendpm = SpecificPluginManager(config, 'backends', ErrBot, CORE_BACKENDS, extra)

    backend_plug = backendpm.get_candidate(backend_name)

    log.info("Found Backend plugin: '%s'\n\t\t\t\t\t\tDescription: %s" % (backend_plug.name, backend_plug.description))

    try:
        bot = backendpm.get_plugin_by_name(backend_name)
        bot.attach_plugin_manager(botpm)
        bot.attach_storage_plugin(storage_plugin)
    except Exception:
        log.exception("Unable to load or configure the backend.")
        exit(-1)

    # restore the bot from the restore script
    if restore:
        # Prepare the context for the restore script
        if 'repos' in bot:
            log.fatal('You cannot restore onto a non empty bot.')
            sys.exit(-1)
        log.info('**** RESTORING the bot from %s' % restore)
        with open(restore) as f:
            exec(f.read())
        bot.close_storage()
        print('Restore complete. You can restart the bot normally')
        sys.exit(0)

    errors = bot.plugin_manager.update_dynamic_plugins()
    if errors:
        log.error('Some plugins failed to load:\n' + '\n'.join(errors))
    return bot
Esempio n. 18
0
def setup_bot(backend_name, logger, config, restore=None):
    # from here the environment is supposed to be set (daemon / non daemon,
    # config.py in the python path )

    from .errBot import bot_config_defaults

    bot_config_defaults(config)

    if config.BOT_LOG_FILE:
        hdlr = logging.FileHandler(config.BOT_LOG_FILE)
        hdlr.setFormatter(
            logging.Formatter(
                "%(asctime)s %(levelname)-8s %(name)-25s %(message)s"))
        logger.addHandler(hdlr)

    if config.BOT_LOG_SENTRY:
        try:
            from raven.handlers.logging import SentryHandler
        except ImportError:
            log.exception(
                "You have BOT_LOG_SENTRY enabled, but I couldn't import modules "
                "needed for Sentry integration. Did you install raven? "
                "(See http://raven.readthedocs.org/en/latest/install/index.html "
                "for installation instructions)")
            exit(-1)

        sentryhandler = SentryHandler(config.SENTRY_DSN,
                                      level=config.SENTRY_LOGLEVEL)
        logger.addHandler(sentryhandler)

    logger.setLevel(config.BOT_LOG_LEVEL)

    # init the storage plugin
    storage_name = getattr(config, 'STORAGE', 'Shelf')
    extra_storage_plugins_dir = getattr(config,
                                        'BOT_EXTRA_STORAGE_PLUGINS_DIR', None)
    spm = SpecificPluginManager(config, 'storage', StoragePluginBase,
                                CORE_STORAGE, extra_storage_plugins_dir)
    storage_pluginfo = spm.get_candidate(storage_name)
    log.info("Found Storage plugin: '%s'\nDescription: %s" %
             (storage_pluginfo.name, storage_pluginfo.description))
    storage_plugin = spm.get_plugin_by_name(storage_name)

    # init the botplugin manager
    botplugins_dir = path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
    if not path.exists(botplugins_dir):
        makedirs(botplugins_dir, mode=0o755)
    botpm = BotPluginManager(storage_plugin, botplugins_dir,
                             config.BOT_EXTRA_PLUGIN_DIR,
                             config.AUTOINSTALL_DEPS,
                             getattr(config, 'CORE_PLUGINS', None))

    # init the backend manager & the bot
    extra = getattr(config, 'BOT_EXTRA_BACKEND_DIR', [])
    backendpm = SpecificPluginManager(config, 'backends', ErrBot,
                                      CORE_BACKENDS, extra)

    backend_plug = backendpm.get_candidate(backend_name)

    log.info("Found Backend plugin: '%s'\n\t\t\t\t\t\tDescription: %s" %
             (backend_plug.name, backend_plug.description))

    try:
        bot = backendpm.get_plugin_by_name(backend_name)
        bot.attach_plugin_manager(botpm)
        bot.attach_storage_plugin(storage_plugin)
    except Exception:
        log.exception("Unable to load or configure the backend.")
        exit(-1)

    # restore the bot from the restore script
    if restore:
        # Prepare the context for the restore script
        if 'repos' in bot:
            log.fatal('You cannot restore onto a non empty bot.')
            sys.exit(-1)
        log.info('**** RESTORING the bot from %s' % restore)
        with open(restore) as f:
            exec(f.read())
        bot.close_storage()
        print('Restore complete. You can restart the bot normally')
        sys.exit(0)

    errors = bot.plugin_manager.update_dynamic_plugins()
    if errors:
        log.error('Some plugins failed to load:\n' + '\n'.join(errors))
    return bot