multi_reporter.py 3.5 KB

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