# -*- coding: utf-8 -*-
"""
    lantz_core.features.util
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Tools to customize feature and help in their writings.

    :copyright: 2015 by Lantz Authors, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.

"""
from __future__ import (division, unicode_literals, print_function,
                        absolute_import)

from types import MethodType
from functools import update_wrapper

from future.utils import exec_


def wrap_custom_feat_method(meth, feat):
    """ Wrap a HasFeature method to make it an driver method of a Feature.

    This is necessary so that users can define overriding method in a natural
    way in the HasFeatures subclass assuming that the driver object will be
    passed as first argument and the Feature object as second when in reality
    it will be the other way round due to python binding mechanism.

    Parameters
    ----------
    meth : MethodType
        Method which should be used to alter the default behaviour of the
        Feature.
    feat : Feature
        Instance of Feature whose default behaviour should be overridden.

    Returns
    -------
    wrapped : MethodType
        Method object which can be

    """
    # Access the real function in case a method is passed.
    if isinstance(meth, MethodType):
        if meth.__self__ is feat:
            return meth

        wrapped = meth.__func__
    else:
        wrapped = meth