base_reporter.py 3.4 KB

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