| Conditions | 1 |
| Total Lines | 237 |
| Lines | 0 |
| Ratio | 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 |
||
| 202 | def create_linter(klass): |
||
| 203 | # Mixin the given interface into the default interface. |
||
| 204 | class LinterInterface(klass, DefaultLinterInterface): |
||
|
|
|||
| 205 | pass |
||
| 206 | |||
| 207 | class Linter(LocalBear): |
||
| 208 | |||
| 209 | @staticmethod |
||
| 210 | def get_interface(): |
||
| 211 | """ |
||
| 212 | Returns the class that contains the interface methods (like |
||
| 213 | ``create_arguments()``) this class uses. |
||
| 214 | |||
| 215 | :return: The interface class. |
||
| 216 | """ |
||
| 217 | return LinterInterface |
||
| 218 | |||
| 219 | @staticmethod |
||
| 220 | def get_executable(): |
||
| 221 | """ |
||
| 222 | Returns the executable of this class. |
||
| 223 | |||
| 224 | :return: The executable name. |
||
| 225 | """ |
||
| 226 | return options["executable"] |
||
| 227 | |||
| 228 | @classmethod |
||
| 229 | def check_prerequisites(cls): |
||
| 230 | """ |
||
| 231 | Checks whether the linter-tool the bear uses is operational. |
||
| 232 | |||
| 233 | :return: True if available, otherwise a string containing more |
||
| 234 | info. |
||
| 235 | """ |
||
| 236 | if shutil.which(cls.get_executable()) is None: |
||
| 237 | return repr(cls.get_executable()) + " is not installed." |
||
| 238 | else: |
||
| 239 | return True |
||
| 240 | |||
| 241 | @classmethod |
||
| 242 | def _get_create_arguments_metadata(cls): |
||
| 243 | return FunctionMetadata.from_function( |
||
| 244 | cls.get_interface().create_arguments, |
||
| 245 | omit={"filename", "file", "config_file"}) |
||
| 246 | |||
| 247 | @classmethod |
||
| 248 | def _get_generate_config_metadata(cls): |
||
| 249 | return FunctionMetadata.from_function( |
||
| 250 | cls.get_interface().generate_config, |
||
| 251 | omit={"filename", "file"}) |
||
| 252 | |||
| 253 | @classmethod |
||
| 254 | def get_non_optional_settings(cls): |
||
| 255 | return cls.get_metadata().non_optional_params |
||
| 256 | |||
| 257 | @classmethod |
||
| 258 | def get_metadata(cls): |
||
| 259 | return cls._merge_metadata( |
||
| 260 | cls._get_create_arguments_metadata(), |
||
| 261 | cls._get_generate_config_metadata()) |
||
| 262 | |||
| 263 | @staticmethod |
||
| 264 | def _merge_metadata(metadata1, metadata2): |
||
| 265 | """ |
||
| 266 | Merges signatures of two ``FunctionMetadata`` objects. |
||
| 267 | |||
| 268 | Parameter descriptions (either optional or non-optional) from |
||
| 269 | ``metadata1`` are overridden by ``metadata2``. |
||
| 270 | |||
| 271 | :param metadata1: The first metadata. |
||
| 272 | :param metadata2: The second metadata. |
||
| 273 | :return: A ``FunctionMetadata`` object containing the |
||
| 274 | merged signature of ``metadata1`` and |
||
| 275 | ``metadata2``. |
||
| 276 | """ |
||
| 277 | merged_optional_params = metadata1.optional_params |
||
| 278 | merged_optional_params.update(metadata2.optional_params) |
||
| 279 | merged_non_optional_params = metadata1.non_optional_params |
||
| 280 | merged_non_optional_params.update(metadata2.non_optional_params) |
||
| 281 | |||
| 282 | return FunctionMetadata( |
||
| 283 | "<Merged signature of {} and {}>".format( |
||
| 284 | repr(metadata1.name), |
||
| 285 | repr(metadata2.name)), |
||
| 286 | "{}:\n{}\n{}:\n{}".format( |
||
| 287 | metadata1.name, |
||
| 288 | metadata1.desc, |
||
| 289 | metadata2.name, |
||
| 290 | metadata2.desc), |
||
| 291 | "{}:\n{}\n{}:\n{}".format( |
||
| 292 | metadata1.name, |
||
| 293 | metadata1.retval_desc, |
||
| 294 | metadata2.name, |
||
| 295 | metadata2.retval_desc), |
||
| 296 | merged_non_optional_params, |
||
| 297 | merged_optional_params, |
||
| 298 | metadata1.omit | metadata2.omit) |
||
| 299 | |||
| 300 | @classmethod |
||
| 301 | def _execute_command(cls, args, stdin=None): |
||
| 302 | """ |
||
| 303 | Executes the underlying tool with the given arguments. |
||
| 304 | |||
| 305 | :param args: The argument sequence to pass to the executable. |
||
| 306 | :param stdin: Input to send to the opened process as stdin. |
||
| 307 | :return: A tuple with ``(stdout, stderr)``. |
||
| 308 | """ |
||
| 309 | return run_shell_command( |
||
| 310 | (cls.get_executable(),) + tuple(args), |
||
| 311 | stdin=stdin) |
||
| 312 | |||
| 313 | def _convert_output_regex_match_to_result(self, match, filename): |
||
| 314 | """ |
||
| 315 | Converts the matched named-groups of ``output_regex`` to an |
||
| 316 | actual ``Result``. |
||
| 317 | |||
| 318 | :param match: The regex match object. |
||
| 319 | :param filename: The name of the file this match belongs to. |
||
| 320 | """ |
||
| 321 | # Pre process the groups |
||
| 322 | groups = match.groupdict() |
||
| 323 | |||
| 324 | if "severity_map" in options: |
||
| 325 | # `severity_map` is only contained inside `options` when |
||
| 326 | # it's actually used. |
||
| 327 | groups["severity"] = ( |
||
| 328 | options["severity_map"][groups["severity"]]) |
||
| 329 | |||
| 330 | for variable in ("line", "column", "end_line", "end_column"): |
||
| 331 | if variable in groups and groups[variable]: |
||
| 332 | groups[variable] = int(groups[variable]) |
||
| 333 | |||
| 334 | if "origin" in groups: |
||
| 335 | groups["origin"] = "{} ({})".format( |
||
| 336 | str(klass.__name__), |
||
| 337 | str(groups["origin"])) |
||
| 338 | |||
| 339 | # Construct the result. |
||
| 340 | return Result.from_values( |
||
| 341 | origin=groups.get("origin", self), |
||
| 342 | message=groups.get("message", ""), |
||
| 343 | file=filename, |
||
| 344 | severity=int(groups.get("severity", |
||
| 345 | RESULT_SEVERITY.NORMAL)), |
||
| 346 | line=groups.get("line", None), |
||
| 347 | column=groups.get("column", None), |
||
| 348 | end_line=groups.get("end_line", None), |
||
| 349 | end_column=groups.get("end_column", None)) |
||
| 350 | |||
| 351 | if options["provides_correction"]: |
||
| 352 | def _process_output(self, output, filename, file): |
||
| 353 | for diff in Diff.from_string_arrays( |
||
| 354 | file, |
||
| 355 | output.splitlines(keepends=True)).split_diff(): |
||
| 356 | yield Result(self, |
||
| 357 | options["diff_message"], |
||
| 358 | affected_code=(diff.range(filename),), |
||
| 359 | diffs={filename: diff}, |
||
| 360 | severity=options["diff_severity"]) |
||
| 361 | else: |
||
| 362 | def _process_output(self, output, filename, file): |
||
| 363 | for match in options["output_regex"].finditer(output): |
||
| 364 | yield self._convert_output_regex_match_to_result( |
||
| 365 | match, filename) |
||
| 366 | |||
| 367 | if options["use_stderr"]: |
||
| 368 | @staticmethod |
||
| 369 | def _grab_output(stdout, stderr): |
||
| 370 | return stderr |
||
| 371 | else: |
||
| 372 | @staticmethod |
||
| 373 | def _grab_output(stdout, stderr): |
||
| 374 | return stdout |
||
| 375 | |||
| 376 | if options["use_stdin"]: |
||
| 377 | @staticmethod |
||
| 378 | def _pass_file_as_stdin_if_needed(file): |
||
| 379 | return "".join(file) |
||
| 380 | else: |
||
| 381 | @staticmethod |
||
| 382 | def _pass_file_as_stdin_if_needed(file): |
||
| 383 | return None |
||
| 384 | |||
| 385 | @classmethod |
||
| 386 | @contextmanager |
||
| 387 | def _create_config(cls, filename, file, **kwargs): |
||
| 388 | """ |
||
| 389 | Provides a context-manager that creates the config file if the |
||
| 390 | user provides one and cleans it up when done with linting. |
||
| 391 | |||
| 392 | :param filename: The filename of the file. |
||
| 393 | :param file: The file contents. |
||
| 394 | :param kwargs: Section settings passed from ``run()``. |
||
| 395 | :return: A context-manager handling the config-file. |
||
| 396 | """ |
||
| 397 | content = cls.get_interface().generate_config(filename, |
||
| 398 | file, |
||
| 399 | **kwargs) |
||
| 400 | if content is None: |
||
| 401 | yield None |
||
| 402 | else: |
||
| 403 | tmp_suffix = options["config_suffix"] |
||
| 404 | with make_temp(suffix=tmp_suffix) as config_file: |
||
| 405 | with open(config_file, mode="w") as fl: |
||
| 406 | fl.write(content) |
||
| 407 | yield config_file |
||
| 408 | |||
| 409 | def run(self, filename, file, **kwargs): |
||
| 410 | # Get the **kwargs params to forward to `generate_config()` |
||
| 411 | # (from `_create_config()`). |
||
| 412 | generate_config_kwargs = { |
||
| 413 | key: kwargs[key] |
||
| 414 | for key in self._get_generate_config_metadata() |
||
| 415 | .non_optional_params.keys()} |
||
| 416 | |||
| 417 | with self._create_config( |
||
| 418 | filename, |
||
| 419 | file, |
||
| 420 | **generate_config_kwargs) as config_file: |
||
| 421 | |||
| 422 | # And now retrieve the **kwargs for `create_arguments()`. |
||
| 423 | create_arguments_kwargs = { |
||
| 424 | key: kwargs[key] |
||
| 425 | for key in self._get_create_arguments_metadata() |
||
| 426 | .non_optional_params.keys()} |
||
| 427 | |||
| 428 | stdout, stderr = self._execute_command( |
||
| 429 | self.get_interface().create_arguments( |
||
| 430 | filename, |
||
| 431 | file, |
||
| 432 | config_file, |
||
| 433 | **create_arguments_kwargs), |
||
| 434 | stdin=self._pass_file_as_stdin_if_needed(file)) |
||
| 435 | output = self._grab_output(stdout, stderr) |
||
| 436 | return self._process_output(output, filename, file) |
||
| 437 | |||
| 438 | return Linter |
||
| 439 | |||
| 441 |