from django.db import models
from django.forms import MultipleChoiceField, ChoiceField, CheckboxSelectMultiple
from pkg_resources import EntryPoint, iter_entry_points
from pkgutil import iter_modules


class PluginNotFound(Exception):
    pass


class PluginLoader(object):

    __plugins__ = None

    @classmethod
    def load_all(cls):
        if cls.__plugins__ is not None:
            return cls.__plugins__

        entry_points = []

        # builtin plugins
        for _, m, _ in iter_modules(['squad/plugins']):
            e = EntryPoint(m, 'squad.plugins.' + m, attrs=('Plugin',))
            entry_points.append(e)

        # external plugins
        plugins = iter_entry_points('squad_plugins')
        entry_points += list(plugins)

        cls.__plugins__ = {e.name: e.resolve() for e in entry_points}
        return cls.__plugins__


def get_plugin_instance(name):
    try:
        plugin_class = PluginLoader.load_all()[name]
    except KeyError:
        raise PluginNotFound(name)
    return plugin_class()


def get_all_plugins():
    plugins = PluginLoader.load_all()
    return plugins.keys()


def apply_plugins(plugin_names):
    """
    This function should be used by code in the SQUAD core to trigger
    functionality from plugins.

    The ``plugin_names`` argument is list of plugins names to be used. Most
    probably, you will want to pass the list of plugins enabled for a given
    project, e.g.  ``project.enabled_plugins``.

    Example::

        from squad.plugins import apply_plugins

        # ...

        for plugin in apply_plugins(project.enabled_plugins):
            plugin.method(...)

    """
    if plugin_names is None:
        return

    for p in plugin_names:
        try:
            plugin = get_plugin_instance(p)
            yield(plugin)
        except PluginNotFound:
            pass


class Plugin(object):
    """
    This class must be used as a superclass for all SQUAD plugins. All the
    methods declared here have empty implementations (i.e. they do nothing),
    and should be overriden in your plugin to provide extra functionality to
    the SQUAD core.
    """

    def postprocess_testrun(self, testrun):
        """
        This method is called after a test run has been received by SQUAD, and
        the test run data (tests, metrics, metadata, logs, etc) have been saved
        to the database.

        You can use this method to parse logs, do any special handling of
        metadata, test results, etc.

        The ``testrun`` arguments is an instance of
        ``squad.core.models.TestRun``.
        """
        pass

    def postprocess_testjob(self, testjob):
        """
        This method is called after a test job has been fetched by SQUAD, and
        the test run data (tests, metrics, metadata, logs, etc) have been saved
        to the database.

        You can use this method to do any processing that is specific to a
        given CI backend (e.g. LAVA).

        The ``testjob`` arguments is an instance of
        ``squad.ci.models.TestJob``.
        """
        pass


class PluginField(models.CharField):

    def __init__(self, **args):
        defaults = {'max_length': 256}
        defaults.update(args)
        return super(PluginField, self).__init__(**defaults)

    def deconstruct(self):
        name, path, args, kwargs = super(PluginField, self).deconstruct()
        del kwargs["max_length"]
        return name, path, args, kwargs

    def formfield(self, **kwargs):
        plugins = ((v, v) for v in get_all_plugins())
        return ChoiceField(choices=plugins)


class PluginListField(models.TextField):

    def from_db_value(self, value, expression, connection, context):
        if value is None:
            return None
        return [item.strip() for item in value.split(',')]

    def to_python(self, value):
        if isinstance(value, list):
            return value
        if value is None:
            return None
        return [item.strip() for item in value.split(',')]

    def get_prep_value(self, value):
        if value is None:
            return value
        return ', '.join(value)

    def formfield(self, **kwargs):
        plugins = ((v, v) for v in get_all_plugins())
        required = not self.null
        return MultipleChoiceField(
            required=required,
            choices=plugins,
            widget=CheckboxSelectMultiple,
        )

@@ -3062,32 +3062,660 @@
 %22%0A        pass%0A%0A
+    def notify_patch_build_created(self, build):%0A        %22%22%22%0A        This method is called when a patch build is created. It should notify%0A        the corresponding patch source that the checks are in progress.%0A%0A        The %60%60build%60%60 argument is an instance of %60%60squad.core.Build%60%60.%0A        %22%22%22%0A        pass%0A%0A    def notify_patch_build_finished(self, build):%0A        %22%22%22%0A        This method is called when a patch build is finished. It should notify%0A        the patch source about the status of the tests (success, failure, etc).%0A%0A        The %60%60build%60%60 argument is an instance of %60%60squad.core.Build%60%60.%0A        %22%22%22%0A        pass%0A%0A
 %0Aclass PluginFie

add API for patch source notifications