Esempio n. 1
0
def plugin(plugin_name, **args):
    """
    Receives a plugin identifier and tries to load its main class. Calls class
    functions.
    """
    from kolibri.utils import conf
    plugin_classes = []

    # Try to load kolibri_plugin from given plugin module identifier
    try:
        plugin_module = importlib.import_module(
            plugin_name + ".kolibri_plugin"
        )
        for obj in plugin_module.__dict__.values():
            if _is_plugin(obj):
                plugin_classes.append(obj)
    except ImportError as e:
        if e.message.startswith("No module named"):
            raise PluginDoesNotExist(
                "Plugin '{}' does not seem to exist. Is it on the PYTHONPATH?".
                format(plugin_name)
            )
        else:
            raise

    if args.get('enable', False):
        for klass in plugin_classes:
            klass.enable()

    if args.get('disable', False):
        for klass in plugin_classes:
            klass.disable()

    conf.save()
Esempio n. 2
0
def conf():
    from kolibri.utils import conf

    old_config = copy.deepcopy(conf.config)
    yield conf
    conf.update(old_config)
    conf.save()
def plugin(plugin_name, **args):
    """
    Receives a plugin identifier and tries to load its main class. Calls class
    functions.
    """
    from kolibri.utils import conf
    plugin_classes = []

    # Try to load kolibri_plugin from given plugin module identifier
    try:
        plugin_module = importlib.import_module(plugin_name +
                                                ".kolibri_plugin")
        for obj in plugin_module.__dict__.values():
            if _is_plugin(obj):
                plugin_classes.append(obj)
    except ImportError as e:
        if e.message.startswith("No module named"):
            raise PluginDoesNotExist(
                "Plugin '{}' does not seem to exist. Is it on the PYTHONPATH?".
                format(plugin_name))
        else:
            raise

    if args.get('enable', False):
        for klass in plugin_classes:
            klass.enable()

    if args.get('disable', False):
        for klass in plugin_classes:
            klass.disable()

    conf.save()
Esempio n. 4
0
def plugin(plugin_name, **args):
    """
    Receives a plugin identifier and tries to load its main class. Calls class
    functions.
    """
    from kolibri.utils import conf
    plugin_classes = []

    from kolibri.plugins.base import KolibriPluginBase  # NOQA

    # Try to load kolibri_plugin from given plugin module identifier
    try:
        plugin_module = importlib.import_module(plugin_name +
                                                ".kolibri_plugin")
        for obj in plugin_module.__dict__.values():
            if type(obj
                    ) == type and obj is not KolibriPluginBase and issubclass(
                        obj, KolibriPluginBase):
                plugin_classes.append(obj)
    except ImportError:
        raise PluginDoesNotExist("Plugin does not exist")

    if args.get('enable', False):
        for klass in plugin_classes:
            klass.enable()

    if args.get('disable', False):
        for klass in plugin_classes:
            klass.disable()

    conf.save()
Esempio n. 5
0
def plugin(plugin_name, **args):
    """
    Receives a plugin identifier and tries to load its main class. Calls class
    functions.
    """
    from kolibri.utils import conf

    plugin_classes = []

    from kolibri.plugins.base import KolibriPluginBase  # NOQA

    # Try to load kolibri_plugin from given plugin module identifier
    try:
        plugin_module = importlib.import_module(plugin_name + ".kolibri_plugin")
        for obj in plugin_module.__dict__.values():
            if type(obj) == type and obj is not KolibriPluginBase and issubclass(obj, KolibriPluginBase):
                plugin_classes.append(obj)
    except ImportError:
        raise PluginDoesNotExist("Plugin does not exist")

    if args.get("enable", False):
        for klass in plugin_classes:
            klass.enable()

    if args.get("disable", False):
        for klass in plugin_classes:
            klass.disable()

    conf.save()
Esempio n. 6
0
def plugin(plugin_name, **args):
    """
    Receives a plugin identifier and tries to load its main class. Calls class
    functions.
    """
    from kolibri.utils import conf

    if args.get('enable', False):
        plugin_classes = get_kolibri_plugin(plugin_name)
        for klass in plugin_classes:
            klass.enable()

    if args.get('disable', False):
        try:
            plugin_classes = get_kolibri_plugin(plugin_name)
            for klass in plugin_classes:
                klass.disable()
        except PluginDoesNotExist as e:
            logger.error(str(e))
            logger.warning(
                "Removing '{}' from configuration in a naive way.".format(
                    plugin_name))
            if plugin_name in conf.config['INSTALLED_APPS']:
                conf.config['INSTALLED_APPS'].remove(plugin_name)
                logger.info(
                    "Removed '{}' from INSTALLED_APPS".format(plugin_name))
            else:
                logger.warning(
                    ("Could not find any matches for {} in INSTALLED_APPS".
                     format(plugin_name)))

    conf.save()
Esempio n. 7
0
def test_bogus_plugin_autoremove_no_path(conf):
    """
    Checks that a plugin without a dotted path is also auto-removed
    """
    plugin_name = "giraffehorse"
    conf.config["INSTALLED_APPS"].append(plugin_name)
    conf.save()
    conf.autoremove_unavailable_plugins()
    assert plugin_name not in conf.config["INSTALLED_APPS"]
Esempio n. 8
0
def test_bogus_plugin_autoremove(conf):
    """
    Checks that a plugin is auto-removed when it cannot be imported
    """
    plugin_name = "giraffe.horse"
    conf.config["INSTALLED_APPS"].append(plugin_name)
    conf.save()
    conf.autoremove_unavailable_plugins()
    assert plugin_name not in conf.config["INSTALLED_APPS"]
Esempio n. 9
0
def test_bogus_plugin_autoremove_no_path(conf):
    """
    Checks that a plugin without a dotted path is also auto-removed
    """
    plugin_name = "giraffehorse"
    conf.config["INSTALLED_APPS"].append(plugin_name)
    conf.save()
    conf.autoremove_unavailable_plugins()
    assert plugin_name not in conf.config["INSTALLED_APPS"]
Esempio n. 10
0
def test_plugin_cannot_be_imported_disable(conf):
    """
    A plugin may be in conf.config['INSTALLED_APPS'] but broken or uninstalled
    """
    plugin_name = "giraffe.horse"
    conf.config["INSTALLED_APPS"].append(plugin_name)
    conf.save()
    cli.plugin(plugin_name, disable=True)
    assert plugin_name not in conf.config["INSTALLED_APPS"]
Esempio n. 11
0
def test_plugin_cannot_be_imported_disable(conf):
    """
    A plugin may be in conf.config['INSTALLED_APPS'] but broken or uninstalled
    """
    plugin_name = "giraffe.horse"
    conf.config["INSTALLED_APPS"].append(plugin_name)
    conf.save()
    cli.plugin(plugin_name, disable=True)
    assert plugin_name not in conf.config["INSTALLED_APPS"]
Esempio n. 12
0
def test_bogus_plugin_autoremove(conf):
    """
    Checks that a plugin is auto-removed when it cannot be imported
    """
    plugin_name = "giraffe.horse"
    conf.config["INSTALLED_APPS"].append(plugin_name)
    conf.save()
    conf.autoremove_unavailable_plugins()
    assert plugin_name not in conf.config["INSTALLED_APPS"]
Esempio n. 13
0
def set_build_time_plugins():
    if "BUILD_TIME_PLUGINS" in os.environ and os.environ["BUILD_TIME_PLUGINS"]:
        build_plugins = load_plugins_from_file(os.environ["BUILD_TIME_PLUGINS"])
        # For now, we still need to enable these plugins in order to build them
        # When the build system has been decoupled, this list of plugins can be used for that instead
        # Import CLI to set kolibri defaults
        from kolibri.utils import cli  # noqa
        from kolibri.utils import conf
        print("Setting build time plugins to:")
        for build_plugin in build_plugins:
            print(build_plugin)
        print("### End build plugins ###")
        conf.config['INSTALLED_APPS'] = build_plugins
        conf.save()
Esempio n. 14
0
def set_default_language(lang):
    """
    Set the default language for this installation of Kolibri. Any running
    instance of Kolibri needs to be restarted in order for this change to work.
    """

    from kolibri.utils import conf
    from django.conf import settings

    valid_languages = [l[0] for l in settings.LANGUAGES]

    if lang in valid_languages:
        conf.config['LANGUAGE_CODE'] = lang
        conf.save()
    else:
        msg = "Invalid language code {langcode}. Must be one of: {validlangs}".format(
            langcode=lang, validlangs=valid_languages)

        logging.warning(msg)
Esempio n. 15
0
def set_default_language(lang):
    """
    Set the default language for this installation of Kolibri. Any running
    instance of Kolibri needs to be restarted in order for this change to work.
    """

    from kolibri.utils import conf
    from django.conf import settings

    valid_languages = [l[0] for l in settings.LANGUAGES]

    if lang in valid_languages:
        conf.config['LANGUAGE_CODE'] = lang
        conf.save()
    else:
        msg = "Invalid language code {langcode}. Must be one of: {validlangs}".format(
            langcode=lang, validlangs=valid_languages
        )

        logging.warning(msg)
Esempio n. 16
0
def plugin(plugin_name, **args):
    """
    Receives a plugin identifier and tries to load its main class. Calls class
    functions.
    """
    from kolibri.utils import conf

    if args.get('enable', False):
        plugin_classes = get_kolibri_plugin(plugin_name)
        for klass in plugin_classes:
            klass.enable()

    if args.get('disable', False):
        try:
            plugin_classes = get_kolibri_plugin(plugin_name)
            for klass in plugin_classes:
                klass.disable()
        except PluginDoesNotExist as e:
            logger.error(str(e))
            logger.warning(
                "Removing '{}' from configuration in a naive way.".format(
                    plugin_name
                )
            )
            if plugin_name in conf.config['INSTALLED_APPS']:
                conf.config['INSTALLED_APPS'].remove(plugin_name)
                logger.info(
                    "Removed '{}' from INSTALLED_APPS".format(plugin_name)
                )
            else:
                logger.warning(
                    (
                        "Could not find any matches for {} in INSTALLED_APPS"
                        .format(plugin_name)
                    )
                )

    conf.save()
Esempio n. 17
0
def conf():
    from kolibri.utils import conf
    old_config = copy.deepcopy(conf.config)
    yield conf
    conf.update(old_config)
    conf.save()