Coverage for C:\Repos\ekr-pylint\pylint\reporters\base_reporter.py: 53%

43 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

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# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt 

4 

5from __future__ import annotations 

6 

7import os 

8import sys 

9import warnings 

10from typing import TYPE_CHECKING, TextIO 

11from warnings import warn 

12 

13from pylint.message import Message 

14from pylint.reporters.ureports.nodes import Text 

15from pylint.utils import LinterStats 

16 

17if TYPE_CHECKING: 

18 from pylint.lint.pylinter import PyLinter 

19 from pylint.reporters.ureports.nodes import Section 

20 

21 

22class BaseReporter: 

23 """Base class for reporters. 

24 

25 symbols: show short symbolic names for messages. 

26 """ 

27 

28 extension = "" 

29 

30 name = "base" 

31 """Name of the reporter.""" 

32 

33 def __init__(self, output: TextIO | None = None) -> None: 

34 if getattr(self, "__implements__", None): 

35 warnings.warn( 

36 "Using the __implements__ inheritance pattern for BaseReporter is no " 

37 "longer supported. Child classes should only inherit BaseReporter", 

38 DeprecationWarning, 

39 ) 

40 self.linter: PyLinter 

41 self.section = 0 

42 self.out: TextIO = output or sys.stdout 

43 self.messages: list[Message] = [] 

44 # Build the path prefix to strip to get relative paths 

45 self.path_strip_prefix = os.getcwd() + os.sep 

46 

47 def handle_message(self, msg: Message) -> None: 

48 """Handle a new message triggered on the current file.""" 

49 self.messages.append(msg) 

50 

51 def set_output(self, output: TextIO | None = None) -> None: 

52 """Set output stream.""" 

53 # TODO: 3.0: Remove deprecated method 

54 warn( 

55 "'set_output' will be removed in 3.0, please use 'reporter.out = stream' instead", 

56 DeprecationWarning, 

57 ) 

58 self.out = output or sys.stdout 

59 

60 def writeln(self, string: str = "") -> None: 

61 """Write a line in the output buffer.""" 

62 print(string, file=self.out) 

63 

64 def display_reports(self, layout: Section) -> None: 

65 """Display results encapsulated in the layout tree.""" 

66 self.section = 0 

67 if layout.report_id: 

68 if isinstance(layout.children[0].children[0], Text): 

69 layout.children[0].children[0].data += f" ({layout.report_id})" 

70 else: 

71 raise ValueError(f"Incorrect child for {layout.children[0].children}") 

72 self._display(layout) 

73 

74 def _display(self, layout: Section) -> None: 

75 """Display the layout.""" 

76 raise NotImplementedError() 

77 

78 def display_messages(self, layout: Section | None) -> None: 

79 """Hook for displaying the messages of the reporter. 

80 

81 This will be called whenever the underlying messages 

82 needs to be displayed. For some reporters, it probably 

83 doesn't make sense to display messages as soon as they 

84 are available, so some mechanism of storing them could be used. 

85 This method can be implemented to display them after they've 

86 been aggregated. 

87 """ 

88 

89 # Event callbacks 

90 

91 def on_set_current_module(self, module: str, filepath: str | None) -> None: 

92 """Hook called when a module starts to be analysed.""" 

93 

94 def on_close( 

95 self, 

96 stats: LinterStats, 

97 previous_stats: LinterStats | None, 

98 ) -> None: 

99 """Hook called when a module finished analyzing."""