Conditions | 7 |
Total Lines | 23 |
Lines | 0 |
Ratio | 0 % |
1 | import functools |
||
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 |