Conditions | 2 |
Total Lines | 418 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
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:
1 | from contextlib import contextmanager |
||
118 | def _create_linter(klass, options): |
||
119 | class LinterMeta(type): |
||
120 | |||
121 | def __repr__(cls): |
||
122 | return "<{} linter class (wrapping {!r})>".format( |
||
123 | cls.__name__, options["executable"]) |
||
124 | |||
125 | class LinterBase(LocalBear, metaclass=LinterMeta): |
||
|
|||
126 | |||
127 | def __init__(self, *args, **kwargs): |
||
128 | LocalBear.__init__(self, *args, **kwargs) |
||
129 | self._full_executable_path = shutil.which(self.get_executable()) |
||
130 | |||
131 | @staticmethod |
||
132 | def generate_config(filename, file): |
||
133 | """ |
||
134 | Generates the content of a config-file the linter-tool might need. |
||
135 | |||
136 | The contents generated from this function are written to a |
||
137 | temporary file and the path is provided inside |
||
138 | ``create_arguments()``. |
||
139 | |||
140 | By default no configuration is generated. |
||
141 | |||
142 | You can provide additional keyword arguments and defaults. These |
||
143 | will be interpreted as required settings that need to be provided |
||
144 | through a coafile-section. |
||
145 | |||
146 | :param filename: |
||
147 | The name of the file currently processed. |
||
148 | :param file: |
||
149 | The contents of the file currently processed. |
||
150 | :return: |
||
151 | The config-file-contents as a string or ``None``. |
||
152 | """ |
||
153 | return None |
||
154 | |||
155 | @staticmethod |
||
156 | def create_arguments(filename, file, config_file): |
||
157 | """ |
||
158 | Creates the arguments for the linter. |
||
159 | |||
160 | You can provide additional keyword arguments and defaults. These |
||
161 | will be interpreted as required settings that need to be provided |
||
162 | through a coafile-section. |
||
163 | |||
164 | :param filename: |
||
165 | The name of the file the linter-tool shall process. |
||
166 | :param file: |
||
167 | The contents of the file. |
||
168 | :param config_file: |
||
169 | The path of the config-file if used. ``None`` if unused. |
||
170 | :return: |
||
171 | A sequence of arguments to feed the linter-tool with. |
||
172 | """ |
||
173 | raise NotImplementedError |
||
174 | |||
175 | @staticmethod |
||
176 | def get_executable(): |
||
177 | """ |
||
178 | Returns the executable of this class. |
||
179 | |||
180 | :return: |
||
181 | The executable name. |
||
182 | """ |
||
183 | return options["executable"] |
||
184 | |||
185 | @classmethod |
||
186 | def check_prerequisites(cls): |
||
187 | """ |
||
188 | Checks whether the linter-tool the bear uses is operational. |
||
189 | |||
190 | :return: |
||
191 | True if operational, otherwise a string containing more info. |
||
192 | """ |
||
193 | if shutil.which(cls.get_executable()) is None: |
||
194 | return (repr(cls.get_executable()) + " is not installed." + |
||
195 | (" " + options["executable_check_fail_info"] |
||
196 | if options["executable_check_fail_info"] else |
||
197 | "")) |
||
198 | else: |
||
199 | if options["prerequisite_check_command"]: |
||
200 | try: |
||
201 | check_call(options["prerequisite_check_command"], |
||
202 | stdout=DEVNULL, |
||
203 | stderr=DEVNULL) |
||
204 | return True |
||
205 | except (OSError, CalledProcessError): |
||
206 | return options["prerequisite_check_fail_message"] |
||
207 | return True |
||
208 | |||
209 | @classmethod |
||
210 | def _get_create_arguments_metadata(cls): |
||
211 | return FunctionMetadata.from_function( |
||
212 | cls.create_arguments, |
||
213 | omit={"self", "filename", "file", "config_file"}) |
||
214 | |||
215 | @classmethod |
||
216 | def _get_generate_config_metadata(cls): |
||
217 | return FunctionMetadata.from_function( |
||
218 | cls.generate_config, |
||
219 | omit={"filename", "file"}) |
||
220 | |||
221 | @classmethod |
||
222 | def _get_process_output_metadata(cls): |
||
223 | metadata = FunctionMetadata.from_function(cls.process_output) |
||
224 | |||
225 | if options["output_format"] is None: |
||
226 | omitted = {"self", "output", "filename", "file"} |
||
227 | else: |
||
228 | # If a specific output format is provided, function signatures |
||
229 | # from process_output functions should not appear in the help. |
||
230 | omitted = set(chain(metadata.non_optional_params, |
||
231 | metadata.optional_params)) |
||
232 | |||
233 | metadata.omit = omitted |
||
234 | return metadata |
||
235 | |||
236 | @classmethod |
||
237 | def get_metadata(cls): |
||
238 | merged_metadata = FunctionMetadata.merge( |
||
239 | cls._get_process_output_metadata(), |
||
240 | cls._get_generate_config_metadata(), |
||
241 | cls._get_create_arguments_metadata()) |
||
242 | merged_metadata.desc = ( |
||
243 | "{}\n\nThis bear uses the {!r} tool.".format( |
||
244 | inspect.getdoc(cls), cls.get_executable())) |
||
245 | |||
246 | return merged_metadata |
||
247 | |||
248 | def _convert_output_regex_match_to_result(self, |
||
249 | match, |
||
250 | filename, |
||
251 | severity_map, |
||
252 | result_message): |
||
253 | """ |
||
254 | Converts the matched named-groups of ``output_regex`` to an actual |
||
255 | ``Result``. |
||
256 | |||
257 | :param match: |
||
258 | The regex match object. |
||
259 | :param filename: |
||
260 | The name of the file this match belongs to. |
||
261 | :param severity_map: |
||
262 | The dict to use to map the severity-match to an actual |
||
263 | ``RESULT_SEVERITY``. |
||
264 | :param result_message: |
||
265 | The static message to use for results instead of grabbing it |
||
266 | from the executable output via the ``message`` named regex |
||
267 | group. |
||
268 | """ |
||
269 | # Pre process the groups |
||
270 | groups = match.groupdict() |
||
271 | |||
272 | if 'severity' in groups: |
||
273 | try: |
||
274 | groups["severity"] = severity_map[ |
||
275 | groups["severity"].lower()] |
||
276 | except KeyError: |
||
277 | self.warn( |
||
278 | repr(groups["severity"]) + " not found in " |
||
279 | "severity-map. Assuming `RESULT_SEVERITY.NORMAL`.") |
||
280 | groups["severity"] = RESULT_SEVERITY.NORMAL |
||
281 | else: |
||
282 | groups['severity'] = RESULT_SEVERITY.NORMAL |
||
283 | |||
284 | for variable in ("line", "column", "end_line", "end_column"): |
||
285 | groups[variable] = (None |
||
286 | if groups.get(variable, None) is None else |
||
287 | int(groups[variable])) |
||
288 | |||
289 | if "origin" in groups: |
||
290 | groups["origin"] = "{} ({})".format(klass.__name__, |
||
291 | groups["origin"].strip()) |
||
292 | |||
293 | # Construct the result. |
||
294 | return Result.from_values( |
||
295 | origin=groups.get("origin", self), |
||
296 | message=(groups.get("message", "").strip() |
||
297 | if result_message is None else result_message), |
||
298 | file=filename, |
||
299 | severity=groups["severity"], |
||
300 | line=groups["line"], |
||
301 | column=groups["column"], |
||
302 | end_line=groups["end_line"], |
||
303 | end_column=groups["end_column"], |
||
304 | additional_info=groups.get("additional_info", "").strip()) |
||
305 | |||
306 | def process_output_corrected(self, |
||
307 | output, |
||
308 | filename, |
||
309 | file, |
||
310 | diff_severity=RESULT_SEVERITY.NORMAL, |
||
311 | result_message="Inconsistency found.", |
||
312 | diff_distance=1): |
||
313 | """ |
||
314 | Processes the executable's output as a corrected file. |
||
315 | |||
316 | :param output: |
||
317 | The output of the program. This can be either a single |
||
318 | string or a sequence of strings. |
||
319 | :param filename: |
||
320 | The filename of the file currently being corrected. |
||
321 | :param file: |
||
322 | The contents of the file currently being corrected. |
||
323 | :param diff_severity: |
||
324 | The severity to use for generating results. |
||
325 | :param result_message: |
||
326 | The message to use for generating results. |
||
327 | :param diff_distance: |
||
328 | Number of unchanged lines that are allowed in between two |
||
329 | changed lines so they get yielded as one diff. If a negative |
||
330 | distance is given, every change will be yielded as an own diff, |
||
331 | even if they are right beneath each other. |
||
332 | :return: |
||
333 | An iterator returning results containing patches for the |
||
334 | file to correct. |
||
335 | """ |
||
336 | if isinstance(output, str): |
||
337 | output = (output,) |
||
338 | |||
339 | for string in output: |
||
340 | for diff in Diff.from_string_arrays( |
||
341 | file, |
||
342 | string.splitlines(keepends=True)).split_diff( |
||
343 | distance=diff_distance): |
||
344 | yield Result(self, |
||
345 | result_message, |
||
346 | affected_code=diff.affected_code(filename), |
||
347 | diffs={filename: diff}, |
||
348 | severity=diff_severity) |
||
349 | |||
350 | def process_output_regex( |
||
351 | self, output, filename, file, output_regex, |
||
352 | severity_map=MappingProxyType({ |
||
353 | "critical": RESULT_SEVERITY.MAJOR, |
||
354 | "c": RESULT_SEVERITY.MAJOR, |
||
355 | "fatal": RESULT_SEVERITY.MAJOR, |
||
356 | "fail": RESULT_SEVERITY.MAJOR, |
||
357 | "f": RESULT_SEVERITY.MAJOR, |
||
358 | "error": RESULT_SEVERITY.MAJOR, |
||
359 | "err": RESULT_SEVERITY.MAJOR, |
||
360 | "e": RESULT_SEVERITY.MAJOR, |
||
361 | "warning": RESULT_SEVERITY.NORMAL, |
||
362 | "warn": RESULT_SEVERITY.NORMAL, |
||
363 | "w": RESULT_SEVERITY.NORMAL, |
||
364 | "information": RESULT_SEVERITY.INFO, |
||
365 | "info": RESULT_SEVERITY.INFO, |
||
366 | "i": RESULT_SEVERITY.INFO, |
||
367 | "suggestion": RESULT_SEVERITY.INFO}), |
||
368 | result_message=None): |
||
369 | """ |
||
370 | Processes the executable's output using a regex. |
||
371 | |||
372 | :param output: |
||
373 | The output of the program. This can be either a single |
||
374 | string or a sequence of strings. |
||
375 | :param filename: |
||
376 | The filename of the file currently being corrected. |
||
377 | :param file: |
||
378 | The contents of the file currently being corrected. |
||
379 | :param output_regex: |
||
380 | The regex to parse the output with. It should use as many |
||
381 | of the following named groups (via ``(?P<name>...)``) to |
||
382 | provide a good result: |
||
383 | |||
384 | - line - The line where the issue starts. |
||
385 | - column - The column where the issue starts. |
||
386 | - end_line - The line where the issue ends. |
||
387 | - end_column - The column where the issue ends. |
||
388 | - severity - The severity of the issue. |
||
389 | - message - The message of the result. |
||
390 | - origin - The origin of the issue. |
||
391 | - additional_info - Additional info provided by the issue. |
||
392 | |||
393 | The groups ``line``, ``column``, ``end_line`` and |
||
394 | ``end_column`` don't have to match numbers only, they can |
||
395 | also match nothing, the generated ``Result`` is filled |
||
396 | automatically with ``None`` then for the appropriate |
||
397 | properties. |
||
398 | :param severity_map: |
||
399 | A dict used to map a severity string (captured from the |
||
400 | ``output_regex`` with the named group ``severity``) to an |
||
401 | actual ``coalib.results.RESULT_SEVERITY`` for a result. |
||
402 | :param result_message: |
||
403 | The static message to use for results instead of grabbing it |
||
404 | from the executable output via the ``message`` named regex |
||
405 | group. |
||
406 | :return: |
||
407 | An iterator returning results. |
||
408 | """ |
||
409 | if isinstance(output, str): |
||
410 | output = (output,) |
||
411 | |||
412 | for string in output: |
||
413 | for match in re.finditer(output_regex, string): |
||
414 | yield self._convert_output_regex_match_to_result( |
||
415 | match, filename, severity_map=severity_map, |
||
416 | result_message=result_message) |
||
417 | |||
418 | if options["output_format"] is None: |
||
419 | # Check if user supplied a `process_output` override. |
||
420 | if not callable(getattr(klass, "process_output", None)): |
||
421 | raise ValueError("`process_output` not provided by given " |
||
422 | "class {!r}.".format(klass.__name__)) |
||
423 | # No need to assign to `process_output` here, the class mixing |
||
424 | # below automatically does that. |
||
425 | else: |
||
426 | # Prevent people from accidentally defining `process_output` |
||
427 | # manually, as this would implicitly override the internally |
||
428 | # set-up `process_output`. |
||
429 | if hasattr(klass, "process_output"): |
||
430 | raise ValueError("Found `process_output` already defined " |
||
431 | "by class {!r}, but {!r} output-format is " |
||
432 | "specified.".format(klass.__name__, |
||
433 | options["output_format"])) |
||
434 | |||
435 | if options["output_format"] == "corrected": |
||
436 | process_output_args = { |
||
437 | key: options[key] |
||
438 | for key in ("result_message", "diff_severity", |
||
439 | "diff_distance") |
||
440 | if key in options} |
||
441 | |||
442 | process_output = partialmethod( |
||
443 | process_output_corrected, **process_output_args) |
||
444 | |||
445 | else: |
||
446 | assert options["output_format"] == "regex" |
||
447 | |||
448 | process_output_args = { |
||
449 | key: options[key] |
||
450 | for key in ("output_regex", "severity_map", |
||
451 | "result_message") |
||
452 | if key in options} |
||
453 | |||
454 | process_output = partialmethod( |
||
455 | process_output_regex, **process_output_args) |
||
456 | |||
457 | @classmethod |
||
458 | @contextmanager |
||
459 | def _create_config(cls, filename, file, **kwargs): |
||
460 | """ |
||
461 | Provides a context-manager that creates the config file if the |
||
462 | user provides one and cleans it up when done with linting. |
||
463 | |||
464 | :param filename: |
||
465 | The filename of the file. |
||
466 | :param file: |
||
467 | The file contents. |
||
468 | :param kwargs: |
||
469 | Section settings passed from ``run()``. |
||
470 | :return: |
||
471 | A context-manager handling the config-file. |
||
472 | """ |
||
473 | content = cls.generate_config(filename, file, **kwargs) |
||
474 | if content is None: |
||
475 | yield None |
||
476 | else: |
||
477 | with make_temp( |
||
478 | suffix=options["config_suffix"]) as config_file: |
||
479 | with open(config_file, mode="w") as fl: |
||
480 | fl.write(content) |
||
481 | yield config_file |
||
482 | |||
483 | def run(self, filename, file, **kwargs): |
||
484 | # Get the **kwargs params to forward to `generate_config()` |
||
485 | # (from `_create_config()`). |
||
486 | generate_config_kwargs = FunctionMetadata.filter_parameters( |
||
487 | self._get_generate_config_metadata(), kwargs) |
||
488 | |||
489 | with self._create_config( |
||
490 | filename, |
||
491 | file, |
||
492 | **generate_config_kwargs) as config_file: |
||
493 | # And now retrieve the **kwargs for `create_arguments()`. |
||
494 | create_arguments_kwargs = ( |
||
495 | FunctionMetadata.filter_parameters( |
||
496 | self._get_create_arguments_metadata(), kwargs)) |
||
497 | |||
498 | args = self.create_arguments(filename, file, config_file, |
||
499 | **create_arguments_kwargs) |
||
500 | |||
501 | try: |
||
502 | args = tuple(args) |
||
503 | except TypeError: |
||
504 | self.err("The given arguments " |
||
505 | "{!r} are not iterable.".format(args)) |
||
506 | return |
||
507 | |||
508 | arguments = (self._full_executable_path,) + args |
||
509 | self.debug("Running '{}'".format(' '.join(arguments))) |
||
510 | |||
511 | output = run_shell_command( |
||
512 | arguments, |
||
513 | stdin="".join(file) if options["use_stdin"] else None) |
||
514 | |||
515 | output = tuple(compress( |
||
516 | output, |
||
517 | (options["use_stdout"], options["use_stderr"]))) |
||
518 | if len(output) == 1: |
||
519 | output = output[0] |
||
520 | |||
521 | process_output_kwargs = FunctionMetadata.filter_parameters( |
||
522 | self._get_process_output_metadata(), kwargs) |
||
523 | return self.process_output(output, filename, file, |
||
524 | **process_output_kwargs) |
||
525 | |||
526 | def __repr__(self): |
||
527 | return "<{} linter object (wrapping {!r}) at {}>".format( |
||
528 | type(self).__name__, self.get_executable(), hex(id(self))) |
||
529 | |||
530 | # Mixin the linter into the user-defined interface, otherwise |
||
531 | # `create_arguments` and other methods would be overridden by the |
||
532 | # default version. |
||
533 | result_klass = type(klass.__name__, (klass, LinterBase), {}) |
||
534 | result_klass.__doc__ = klass.__doc__ if klass.__doc__ else "" |
||
535 | return result_klass |
||
536 | |||
737 |