<commit_before>from typing import TYPE_CHECKING, List

from django.conf import settings
from django.core.checks import Error, register
from django.utils.module_loading import import_string

if TYPE_CHECKING:
    from .base_plugin import BasePlugin


@register()
def check_plugins(app_configs, **kwargs):
    """Confirm a correct import of plugins and manager."""
    errors = []
    check_manager(errors)

    plugins = settings.PLUGINS or []

    for plugin_path in plugins:
        check_single_plugin(plugin_path, errors)

    return errors


def check_manager(errors: List[Error]):
    if not hasattr(settings, "PLUGINS_MANAGER") or not settings.PLUGINS_MANAGER:
        errors.append(Error("Settings should contain PLUGINS_MANAGER env"))
        return

    try:
        import_string(settings.PLUGINS_MANAGER)
    except ImportError:
        errors.append(
            Error("Plugins Manager path: %s doesn't exist" % settings.PLUGINS_MANAGER)
        )


def check_single_plugin(plugin_path: str, errors: List[Error]):
    if not plugin_path:
        errors.append(Error("Wrong plugin_path %s" % plugin_path))
        return
    try:
        plugin_class = import_string(plugin_path)
    except ImportError:
        errors.append(Error("Plugin with path: %s doesn't exist" % plugin_path))

    if not errors:
        check_plugin_fields(["PLUGIN_ID"], plugin_class, errors)


def check_plugin_fields(
    fields: List[str], plugin_class: "BasePlugin", errors: List[Error]
):
    name = plugin_class.__name__  # type: ignore

    for field in fields:
        if not getattr(plugin_class, field, None):
            errors.append(Error(f"Missing field {field} for plugin - {name}"))
<commit_msg>Print import error when plugin import fails (#6748)<commit_after>from typing import TYPE_CHECKING, List

from django.conf import settings
from django.core.checks import Error, register
from django.utils.module_loading import import_string

if TYPE_CHECKING:
    from .base_plugin import BasePlugin


@register()
def check_plugins(app_configs, **kwargs):
    """Confirm a correct import of plugins and manager."""
    errors = []
    check_manager(errors)

    plugins = settings.PLUGINS or []

    for plugin_path in plugins:
        check_single_plugin(plugin_path, errors)

    return errors


def check_manager(errors: List[Error]):
    if not hasattr(settings, "PLUGINS_MANAGER") or not settings.PLUGINS_MANAGER:
        errors.append(Error("Settings should contain PLUGINS_MANAGER env"))
        return

    try:
        import_string(settings.PLUGINS_MANAGER)
    except ImportError:
        errors.append(
            Error("Plugins Manager path: %s doesn't exist" % settings.PLUGINS_MANAGER)
        )


def check_single_plugin(plugin_path: str, errors: List[Error]):
    if not plugin_path:
        errors.append(Error("Wrong plugin_path %s" % plugin_path))
        return
    try:
        plugin_class = import_string(plugin_path)
    except ImportError as e:
        errors.append(Error(f"Failed to import plugin {plugin_path}: {e}"))

    if not errors:
        check_plugin_fields(["PLUGIN_ID"], plugin_class, errors)


def check_plugin_fields(
    fields: List[str], plugin_class: "BasePlugin", errors: List[Error]
):
    name = plugin_class.__name__  # type: ignore

    for field in fields:
        if not getattr(plugin_class, field, None):
            errors.append(Error(f"Missing field {field} for plugin - {name}"))

{'fraction_non_alphanumeric': 0.09254947613504075, 'fraction_numerical': 0.0011641443538998836, 'mean_word_length': 3.582666666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1172116', 'n_tokens_mistral': 1037, 'n_tokens_neox': 1003, 'n_words': 278}