base_reporter.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import sys
  5. from typing import TYPE_CHECKING, List, Optional, TextIO
  6. from warnings import warn
  7. from pylint.message import Message
  8. from pylint.reporters.ureports.nodes import Text
  9. from pylint.utils import LinterStats
  10. if TYPE_CHECKING:
  11. from pylint.lint.pylinter import PyLinter
  12. from pylint.reporters.ureports.nodes import Section
  13. class BaseReporter:
  14. """base class for reporters
  15. symbols: show short symbolic names for messages.
  16. """
  17. extension = ""
  18. def __init__(self, output: Optional[TextIO] = None) -> None:
  19. self.linter: "PyLinter"
  20. self.section = 0
  21. self.out: TextIO = output or sys.stdout
  22. self.messages: List[Message] = []
  23. # Build the path prefix to strip to get relative paths
  24. self.path_strip_prefix = os.getcwd() + os.sep
  25. def handle_message(self, msg: Message) -> None:
  26. """Handle a new message triggered on the current file."""
  27. self.messages.append(msg)
  28. def set_output(self, output: Optional[TextIO] = None) -> None:
  29. """set output stream"""
  30. # pylint: disable-next=fixme
  31. # TODO: Remove this method after depreciation
  32. warn(
  33. "'set_output' will be removed in 3.0, please use 'reporter.out = stream' instead",
  34. DeprecationWarning,
  35. )
  36. self.out = output or sys.stdout
  37. def writeln(self, string: str = "") -> None:
  38. """write a line in the output buffer"""
  39. print(string, file=self.out)
  40. def display_reports(self, layout: "Section") -> None:
  41. """display results encapsulated in the layout tree"""
  42. self.section = 0
  43. if layout.report_id:
  44. if isinstance(layout.children[0].children[0], Text):
  45. layout.children[0].children[0].data += f" ({layout.report_id})"
  46. else:
  47. raise ValueError(f"Incorrect child for {layout.children[0].children}")
  48. self._display(layout)
  49. def _display(self, layout: "Section") -> None:
  50. """display the layout"""
  51. raise NotImplementedError()
  52. def display_messages(self, layout: Optional["Section"]) -> None:
  53. """Hook for displaying the messages of the reporter
  54. This will be called whenever the underlying messages
  55. needs to be displayed. For some reporters, it probably
  56. doesn't make sense to display messages as soon as they
  57. are available, so some mechanism of storing them could be used.
  58. This method can be implemented to display them after they've
  59. been aggregated.
  60. """
  61. # Event callbacks
  62. def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
  63. """Hook called when a module starts to be analysed."""
  64. def on_close(
  65. self,
  66. stats: LinterStats,
  67. previous_stats: LinterStats,
  68. ) -> None:
  69. """Hook called when a module finished analyzing."""