Completed
Pull Request — master (#2598)
by Lasse
01:58
created

wrapping_function()   A

Complexity

Conditions 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 8
rs 9.2
1
"""
2
The bearlib is an optional library designed to ease the task of any Bear. Just
3
as the rest of coala the bearlib is designed to be as easy to use as possible
4
while offering the best possible flexibility.
5
"""
6
7
from coalib.settings.FunctionMetadata import FunctionMetadata
8
9
10
def deprecate_settings(**depr_args):
11
    """
12
     The purpose of this decorator is to allow passing old settings names to
13
     bears due to the heavy changes in their names.
14
15
     >>> @deprecate_settings(new='old')
16
     ... def run(new):
17
     ...     print(new)
18
19
     Now we can simply call the bear with the deprecated setting, we'll get a
20
     warning - but it still works!
21
22
     >>> run(old="Hello world!")
23
     The setting `old` is deprecated. Please use `new` instead.
24
     Hello world!
25
     >>> run(new="Hello world!")
26
     Hello world!
27
28
     :param depr_args: A dictionary of settings as keys and their deprecated
29
                       names as values.
30
    """
31
    def _deprecate_decorator(func):
32
33
        def wrapping_function(*args, **kwargs):
34
            for arg, deprecated_arg in depr_args.items():
35
                if deprecated_arg in kwargs and arg not in kwargs:
36
                    print("The setting `{}` is deprecated. Please use `{}` "
37
                          "instead.".format(deprecated_arg, arg))
38
                    kwargs[arg] = kwargs[deprecated_arg]
39
                    del kwargs[deprecated_arg]
40
            return func(*args, **kwargs)
41
42
        new_metadata = FunctionMetadata.from_function(func)
43
        for arg, deprecated_arg in depr_args.items():
44
            new_metadata.add_alias(arg, deprecated_arg)
45
        wrapping_function.__metadata__ = new_metadata
46
47
        return wrapping_function
48
49
    return _deprecate_decorator
50