import types


def good_decorator_accepting_args(decorator):
    """This decorator makes decorators behave well wrt to decorated
    functions names, doc, etc. 

    Differently from good_decorator, this accepts decorators possibly
    receiving arguments and keyword arguments.

    This decorato can be used indifferently with class methods and
    functions."""
    def new_decorator(*f, **k):
        g = decorator(*f, **k)
        if 1 == len(f) and isinstance(f[0], types.FunctionType):
            g.__name__ = f[0].__name__
            g.__doc__ = f[0].__doc__
            g.__dict__.update(f[0].__dict__)
            pass
        return g

    new_decorator.__name__ = decorator.__name__
    new_decorator.__doc__ = decorator.__doc__
    new_decorator.__dict__.update(decorator.__dict__)
    # Required for Sphinx' automodule.
    new_decorator.__module__ = decorator.__module__
    return new_decorator