コード例 #1
0
ファイル: reload.py プロジェクト: sopel-irc/sopel
def f_load(bot, trigger):
    """Loads a module (for use by admins only)."""
    name = trigger.group(2)
    if not name:
        return bot.reply('Load what?')

    if bot.has_plugin(name):
        return bot.reply('Module already loaded, use reload')

    usable_plugins = plugins.get_usable_plugins(bot.config)
    if name not in usable_plugins:
        bot.reply('Module %s not found' % name)
        return

    plugin, is_enabled = usable_plugins[name]
    if not is_enabled:
        bot.reply('Module %s is disabled' % name)
        return

    try:
        _load(bot, plugin)
    except Exception as error:
        bot.reply('Could not load module %s: %s' % (name, error))
    else:
        bot.reply('Module %s loaded' % name)
コード例 #2
0
ファイル: utils.py プロジェクト: sopel-irc/sopel
def _plugins_wizard(settings):
    usable_plugins = plugins.get_usable_plugins(settings)
    for plugin, is_enabled in usable_plugins.values():
        if not is_enabled:
            # Do not configure non-enabled modules
            continue

        name = plugin.name
        try:
            _plugin_wizard(settings, plugin)
        except Exception as e:
            filename, lineno = tools.get_raising_file_and_line()
            rel_path = os.path.relpath(filename, os.path.dirname(__file__))
            raising_stmt = "%s:%d" % (rel_path, lineno)
            tools.stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt))
コード例 #3
0
ファイル: bot.py プロジェクト: xnaas/sopel
    def setup_plugins(self):
        """Load plugins into the bot."""
        load_success = 0
        load_error = 0
        load_disabled = 0

        LOGGER.info('Loading plugins...')
        usable_plugins = plugins.get_usable_plugins(self.settings)
        for name, info in usable_plugins.items():
            plugin, is_enabled = info
            if not is_enabled:
                load_disabled = load_disabled + 1
                continue

            try:
                plugin.load()
            except Exception as e:
                load_error = load_error + 1
                LOGGER.exception('Error loading %s: %s', name, e)
            else:
                try:
                    if plugin.has_setup():
                        plugin.setup(self)
                    plugin.register(self)
                except Exception as e:
                    load_error = load_error + 1
                    LOGGER.exception('Error in %s setup: %s', name, e)
                else:
                    load_success = load_success + 1
                    LOGGER.info('Plugin loaded: %s', name)

        total = sum([load_success, load_error, load_disabled])
        if total and load_success:
            LOGGER.info('Registered %d plugins, %d failed, %d disabled',
                        (load_success - 1), load_error, load_disabled)
        else:
            LOGGER.warning("Warning: Couldn't load any plugins")
コード例 #4
0
def handle_enable(options):
    """Enable a Sopel plugin"""
    plugin_names = options.names
    allow_only = options.allow_only
    settings = utils.load_settings(options)
    usable_plugins = plugins.get_usable_plugins(settings)

    # plugin does not exist
    unknown_plugins = [
        name
        for name in plugin_names
        if name not in usable_plugins
    ]
    if unknown_plugins:
        display_unknown_plugins(unknown_plugins)
        return 1  # do nothing and return an error code

    actually_enabled = tuple(
        name
        for name in plugin_names
        if _handle_enable_plugin(settings, usable_plugins, name, allow_only)
    )

    # save if required
    if actually_enabled:
        settings.save()
    else:
        return 0  # nothing to disable or save, but not an error case

    # display plugins actually disabled by the command
    print(utils.get_many_text(
        actually_enabled,
        one='Plugin {item} enabled.',
        two='Plugins {first} and {second} enabled.',
        many='Plugins {left}, and {last} enabled.'
    ))
    return 0
コード例 #5
0
ファイル: factories.py プロジェクト: r4f4/sopel
    def preloaded(self, settings, preloads=None):
        """Create a bot and preload its plugins.

        :param settings: Sopel's configuration for testing purposes
        :type settings: :class:`sopel.config.Config`
        :param list preloads: list of plugins to preload, setup, and register
        :return: a test instance of the bot
        :rtype: :class:`sopel.bot.Sopel`

        This will instantiate a :class:`~sopel.bot.Sopel` object, replace its
        backend with a :class:`~MockIRCBackend`, and then preload plugins.
        This will automatically load the ``coretasks`` plugin, and every other
        plugin from ``preloads``::

            factory = BotFactory()
            bot = factory.with_autoloads(settings, ['emoticons', 'remind'])

        .. note::

            This will automatically setup plugins: be careful with plugins that
            require access to external services on setup.

            You may also need to manually call shutdown routines for the
            loaded plugins.

        """
        preloads = set(preloads or []) | {'coretasks'}
        mockbot = self(settings)

        usable_plugins = plugins.get_usable_plugins(settings)
        for name in preloads:
            plugin = usable_plugins[name][0]
            plugin.load()
            plugin.setup(mockbot)
            plugin.register(mockbot)

        return mockbot
コード例 #6
0
def handle_disable(options):
    """Disable Sopel plugins.

    :param options: parsed arguments
    :type options: :class:`argparse.Namespace`
    :return: 0 if everything went fine;
             1 if the plugin doesn't exist,
             or if attempting to disable coretasks (required)
    """
    plugin_names = options.names
    force = options.force
    ensure_remove = options.remove
    settings = utils.load_settings(options)
    usable_plugins = plugins.get_usable_plugins(settings)
    actually_disabled = []

    # coretasks is sacred
    if 'coretasks' in plugin_names:
        tools.stderr('Plugin coretasks cannot be disabled.')
        return 1  # do nothing and return an error code

    unknown_plugins = [
        name
        for name in plugin_names
        if name not in usable_plugins
    ]
    if unknown_plugins:
        display_unknown_plugins(unknown_plugins)
        return 1  # do nothing and return an error code

    # remove from enabled if asked
    if ensure_remove:
        settings.core.enable = [
            name
            for name in settings.core.enable
            if name not in plugin_names
        ]
        settings.save()

    # disable plugin (when needed)
    actually_disabled = tuple(
        name
        for name in plugin_names
        if _handle_disable_plugin(settings, name, force)
    )

    # save if required
    if actually_disabled:
        settings.save()
    else:
        return 0  # nothing to disable or save, but not an error case

    # display plugins actually disabled by the command
    print(utils.get_many_text(
        actually_disabled,
        one='Plugin {item} disabled.',
        two='Plugins {first} and {second} disabled.',
        many='Plugins {left}, and {last} disabled.'
    ))

    return 0
コード例 #7
0
def handle_list(options):
    """List Sopel plugins.

    :param options: parsed arguments
    :type options: :class:`argparse.Namespace`
    :return: 0 if everything went fine
    """
    settings = utils.load_settings(options)
    no_color = options.no_color
    name_only = options.name_only
    enabled_only = options.enabled_only
    disabled_only = options.disabled_only

    # get usable plugins
    items = (
        (name, info[0], info[1])
        for name, info in plugins.get_usable_plugins(settings).items()
    )
    items = (
        (name, plugin, is_enabled)
        for name, plugin, is_enabled in items
    )
    # filter on enabled/disabled if required
    if enabled_only:
        items = (
            (name, plugin, is_enabled)
            for name, plugin, is_enabled in items
            if is_enabled
        )
    elif disabled_only:
        items = (
            (name, plugin, is_enabled)
            for name, plugin, is_enabled in items
            if not is_enabled
        )
    # sort plugins
    items = sorted(items, key=operator.itemgetter(0))

    for name, plugin, is_enabled in items:
        description = {
            'name': name,
            'status': 'enabled' if is_enabled else 'disabled',
        }

        # optional meta description from the plugin itself
        try:
            plugin.load()
            description.update(plugin.get_meta_description())

            # colorize name for display purpose
            if not no_color:
                if is_enabled:
                    description['name'] = utils.green(name)
                else:
                    description['name'] = utils.red(name)
        except Exception as error:
            label = ('%s' % error) or 'unknown loading exception'
            error_status = 'error'
            description.update({
                'label': 'Error: %s' % label,
                'type': 'unknown',
                'source': 'unknown',
                'status': error_status,
            })
            if not no_color:
                if is_enabled:
                    # yellow instead of green
                    description['name'] = utils.yellow(name)
                else:
                    # keep it red for disabled plugins
                    description['name'] = utils.red(name)
                description['status'] = utils.red(error_status)

        template = '{name}/{type} {label} ({source}) [{status}]'
        if name_only:
            template = '{name}'

        print(template.format(**description))

    return 0  # successful operation