Conditions | 16 |
Total Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like CmdValidatorsPlugin.validate() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import sys |
||
47 | def validate(self, command, search=None, regex=None, timeout=2, allowed_return_codes=None, decode="utf-8"): |
||
48 | """ |
||
49 | Validates the output of a given command. |
||
50 | |||
51 | The validation can be based on a simple string search or on a complex regular expression. |
||
52 | Also the return_code can be validated. As well as the execution duration by setting a timeout. |
||
53 | |||
54 | :param command: string, which is used as command for a new subprocess. E.g. 'git -v'. |
||
55 | :param search: string, which shall be contained in the output of the command. Default is None |
||
56 | :param regex: regular expression, which is tested against the command output. |
||
57 | Default is None |
||
58 | :param timeout: Time ins seconds, after which the execution is stopped and the validation fails. |
||
59 | Default is 2 seconds |
||
60 | :param allowed_return_codes: List of allowed return values. Default is [] |
||
61 | :param decode: Format of the console encoding, which shall be used. Default is 'utf-8' |
||
62 | :return: True, if validation succeeded. Else False. |
||
63 | """ |
||
64 | if search is None and regex is None: |
||
65 | raise ValueError("Parameter search or regex must be set.") |
||
66 | if search is not None and regex is not None: |
||
67 | raise ValueError("Only search OR regex is allowed to be used. Not both!") |
||
68 | |||
69 | if allowed_return_codes is None: |
||
70 | allowed_return_codes = [] |
||
71 | if isinstance(allowed_return_codes, int): |
||
72 | allowed_return_codes = [allowed_return_codes] |
||
73 | if not isinstance(allowed_return_codes, list): |
||
74 | raise TypeError("allowed_return_code must be a list of integers") |
||
75 | |||
76 | try: |
||
77 | output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True, timeout=timeout) |
||
78 | return_code = 0 |
||
79 | except subprocess.CalledProcessError as e: |
||
80 | output = e.output |
||
81 | return_code = e.returncode |
||
82 | except subprocess.TimeoutExpired as e: |
||
83 | raise CommandTimeoutExpired(e) |
||
84 | |||
85 | if len(allowed_return_codes) > 0 and return_code not in allowed_return_codes: |
||
86 | raise NotAllowedReturnCode("For command %s got return code '%s', which is not in %s" |
||
87 | % (command, return_code, allowed_return_codes)) |
||
88 | |||
89 | self.plugin.log.debug("Executed '%s' with return code: %s" % (command, return_code)) |
||
90 | |||
91 | output = output.decode(decode) |
||
92 | found = False |
||
93 | if search is not None: |
||
94 | if search in output: |
||
95 | found = True |
||
96 | elif regex is not None: |
||
97 | for m in finditer(regex, output): |
||
98 | self.plugin.log.debug("Found cmd validation '%s' at %02d-%02d" % (m.group(0), m.start(), m.end())) |
||
99 | found = True |
||
100 | return found |
||
101 | |||
109 |