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

replace_args()   C

Complexity

Conditions 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 23
rs 5.5

3 Methods

Rating   Name   Duplication   Size   Complexity  
B _replace_args() 0 13 6
A _replace() 0 5 3
B _wrapper() 0 11 5
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