multi_reporter.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. import os
  6. from collections.abc import Callable
  7. from copy import copy
  8. from typing import TYPE_CHECKING, TextIO
  9. from pylint.message import Message
  10. from pylint.reporters.base_reporter import BaseReporter
  11. from pylint.utils import LinterStats
  12. if TYPE_CHECKING:
  13. from pylint.lint import PyLinter
  14. from pylint.reporters.ureports.nodes import Section
  15. class MultiReporter:
  16. """Reports messages and layouts in plain text."""
  17. name = "_internal_multi_reporter"
  18. # Note: do not register this reporter with linter.register_reporter as it is
  19. # not intended to be used directly like a regular reporter, but is
  20. # instead used to implement the
  21. # `--output-format=json:somefile.json,colorized`
  22. # multiple output formats feature
  23. extension = ""
  24. def __init__(
  25. self,
  26. sub_reporters: list[BaseReporter],
  27. close_output_files: Callable[[], None],
  28. output: TextIO | None = None,
  29. ):
  30. self._sub_reporters = sub_reporters
  31. self.close_output_files = close_output_files
  32. self._path_strip_prefix = os.getcwd() + os.sep
  33. self._linter: PyLinter | None = None
  34. self.out = output
  35. self.messages: list[Message] = []
  36. @property
  37. def out(self) -> TextIO | None:
  38. return self.__out
  39. @out.setter
  40. def out(self, output: TextIO | None = None) -> None:
  41. """MultiReporter doesn't have its own output.
  42. This method is only provided for API parity with BaseReporter
  43. and should not be called with non-None values for 'output'.
  44. """
  45. self.__out = None
  46. if output is not None:
  47. raise NotImplementedError("MultiReporter does not support direct output.")
  48. def __del__(self) -> None:
  49. self.close_output_files()
  50. @property
  51. def path_strip_prefix(self) -> str:
  52. return self._path_strip_prefix
  53. @property
  54. def linter(self) -> PyLinter | None:
  55. return self._linter
  56. @linter.setter
  57. def linter(self, value: PyLinter) -> None:
  58. self._linter = value
  59. for rep in self._sub_reporters:
  60. rep.linter = value
  61. def handle_message(self, msg: Message) -> None:
  62. """Handle a new message triggered on the current file."""
  63. for rep in self._sub_reporters:
  64. # We provide a copy so reporters can't modify message for others.
  65. rep.handle_message(copy(msg))
  66. def writeln(self, string: str = "") -> None:
  67. """Write a line in the output buffer."""
  68. for rep in self._sub_reporters:
  69. rep.writeln(string)
  70. def display_reports(self, layout: Section) -> None:
  71. """Display results encapsulated in the layout tree."""
  72. for rep in self._sub_reporters:
  73. rep.display_reports(layout)
  74. def display_messages(self, layout: Section | None) -> None:
  75. """Hook for displaying the messages of the reporter."""
  76. for rep in self._sub_reporters:
  77. rep.display_messages(layout)
  78. def on_set_current_module(self, module: str, filepath: str | None) -> None:
  79. """Hook called when a module starts to be analysed."""
  80. for rep in self._sub_reporters:
  81. rep.on_set_current_module(module, filepath)
  82. def on_close(
  83. self,
  84. stats: LinterStats,
  85. previous_stats: LinterStats | None,
  86. ) -> None:
  87. """Hook called when a module finished analyzing."""
  88. for rep in self._sub_reporters:
  89. rep.on_close(stats, previous_stats)