import importlib
import logging


def load_plugin(entry_point, callback=None):
    """Load a generic plugin.

    Parameters
    ----------
    entry_point : str
        A entry point specify for the plugin. The specifier should be a
        dotted module name followed by a ``:`` and an identifier nameing an
        object within the module.
    callback : func
        If provided, this function will be called on the plugin and the
        result returned.

    Returns
    -------
    object
        The plugin.
    """
    module_name, cls_name = entry_point.split(":")

    plugin = None
    try:
        module = importlib.import_module(module_name)
    except ImportError:
        logging.info("Unable to import {module}.".format(module=module_name))
    else:
        try:
            plugin = module.__dict__[cls_name]
        except KeyError:
            logging.info(
                "{plugin} not contained in {module}.".format(
                    plugin=cls_name, module=module_name
                )
            )
    if callback and plugin:
        plugin = callback(plugin)

    return plugin