Completed
Pull Request — master (#340)
by Arma
03:46
created

_wrapper()   B

Complexity

Conditions 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 11
rs 8.5455
1
import functools
2
3
__all__ = [
4
    'replace_args',
5
]
6
7
8
def replace_args(attribute=None):
9
    """
10
    Decorator to Apply replacements in a list of command line arguments.
11
12
    :param attribute: Class attribute name which stores replacement rules.
13
    :type attribute: ``str``
14
    :return:
15
    :rtype: ``callable``
16
    """
17
    def _replace_args(f):
18
        @functools.wraps(f)
19
        def _wrapper(self, *args):
20
            def _replace(arg):
21
                for rule in rules:
22
                    if arg.startswith(rule):
23
                        return arg.replace(rule, rules[rule])
24
                return arg
25
            rules = getattr(self, attribute)
26
            if not rules:
27
                return f(self, *args)
28
            return map(_replace, f(self, *args))
29
        return _wrapper
30
    return _replace_args
31