Completed
Pull Request — master (#2595)
by
unknown
01:59
created

_deprecate_decorator()   B

Complexity

Conditions 6

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
dl 0
loc 14
rs 8
1
from coalib.settings.FunctionMetadata import FunctionMetadata
2
3
4
def deprecate_settings(**depr_args):
5
    """
6
     The purpose of this decorator is to Allow passing old settings names to
7
     bears due to the heavy changes in their names.
8
9
     :param depr_args:     A dictionnary of settings as keys and their
10
                           deprecated names as values.
11
    """
12
    def _deprecate_decorator(func):
13
        def wrapping_function(*args, **kwargs):
14
            for arg, deprecated_arg in depr_args.items():
15
                if deprecated_arg in kwargs and arg not in kwargs:
16
                    print("The setting `{}` is deprecated. Please use `{}` "
17
                          "instead.".format(deprecated_arg, arg))
18
                    kwargs[arg] = kwargs[deprecated_arg]
19
                    del kwargs[deprecated_arg]
20
            return func(*args, **kwargs)
21
        wrapping_function.__metadata__ = FunctionMetadata.from_function(func)
22
        for arg, deprecated_arg in depr_args.items():
23
            wrapping_function.__metadata__.optional_params[deprecated_arg] = (
24
                wrapping_function.__metadata__.optional_params[arg])
25
        return wrapping_function
26
    return _deprecate_decorator
27