1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# Copyright (c) 2006-2007, 2010-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
# Copyright (c) 2012-2014 Google, Inc.
# Copyright (c) 2014 Brett Cannon <brett@python.org>
# Copyright (c) 2014 Arun Persaud <arun@nubati.net>
# Copyright (c) 2015-2018, 2020 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2015 Florian Bruhin <me@the-compiler.org>
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
# Copyright (c) 2016 y2kbugger <y2kbugger@users.noreply.github.com>
# Copyright (c) 2018-2019 Nick Drozd <nicholasdrozd@gmail.com>
# Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com>
# Copyright (c) 2018 Jace Browning <jacebrowning@gmail.com>
# Copyright (c) 2019-2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
# Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
# Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com>
# Copyright (c) 2021 Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
# Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
"""Plain text reporters:
:text: the default one grouping messages by module
:colorized: an ANSI colorized text reporter
"""
import os
import re
import sys
import warnings
from typing import (
TYPE_CHECKING,
Dict,
NamedTuple,
Optional,
Set,
TextIO,
Tuple,
Union,
cast,
overload,
)
from pylint.interfaces import IReporter
from pylint.message import Message
from pylint.reporters import BaseReporter
from pylint.reporters.ureports.text_writer import TextWriter
from pylint.utils import _splitstrip
if TYPE_CHECKING:
from pylint.lint import PyLinter
from pylint.reporters.ureports.nodes import Section
class MessageStyle(NamedTuple):
"""Styling of a message"""
color: Optional[str]
"""The color name (see `ANSI_COLORS` for available values)
or the color number when 256 colors are available
"""
style: Tuple[str, ...]
"""Tuple of style strings (see `ANSI_COLORS` for available values).
"""
ColorMappingDict = Dict[str, MessageStyle]
TITLE_UNDERLINES = ["", "=", "-", "."]
ANSI_PREFIX = "\033["
ANSI_END = "m"
ANSI_RESET = "\033[0m"
ANSI_STYLES = {
"reset": "0",
"bold": "1",
"italic": "3",
"underline": "4",
"blink": "5",
"inverse": "7",
"strike": "9",
}
ANSI_COLORS = {
"reset": "0",
"black": "30",
"red": "31",
"green": "32",
"yellow": "33",
"blue": "34",
"magenta": "35",
"cyan": "36",
"white": "37",
}
def _get_ansi_code(msg_style: MessageStyle) -> str:
"""return ansi escape code corresponding to color and style
:param msg_style: the message style
:raise KeyError: if an unexistent color or style identifier is given
:return: the built escape code
"""
ansi_code = [ANSI_STYLES[effect] for effect in msg_style.style]
if msg_style.color:
if msg_style.color.isdigit():
ansi_code.extend(["38", "5"])
ansi_code.append(msg_style.color)
else:
ansi_code.append(ANSI_COLORS[msg_style.color])
if ansi_code:
return ANSI_PREFIX + ";".join(ansi_code) + ANSI_END
return ""
@overload
def colorize_ansi(
msg: str,
msg_style: Optional[MessageStyle] = None,
) -> str:
...
@overload
def colorize_ansi(
msg: str,
msg_style: Optional[str] = None,
style: Optional[str] = None,
*,
color: Optional[str] = None,
) -> str:
# Remove for pylint 3.0
...
def colorize_ansi(
msg: str,
msg_style: Union[MessageStyle, str, None] = None,
style: Optional[str] = None,
**kwargs: Optional[str],
) -> str:
"""colorize message by wrapping it with ansi escape codes
:param msg: the message string to colorize
:param msg_style: the message style
or color (for backwards compatibility): the color of the message style
:param style: the message's style elements, this will be deprecated
:param **kwargs: used to accept `color` parameter while it is being deprecated
:return: the ansi escaped string
"""
# pylint: disable-next=fixme
# TODO: Remove DeprecationWarning and only accept MessageStyle as parameter
if not isinstance(msg_style, MessageStyle):
warnings.warn(
"In pylint 3.0, the colorize_ansi function of Text reporters will only accept a MessageStyle parameter",
DeprecationWarning,
)
color = kwargs.get("color")
style_attrs = tuple(_splitstrip(style))
msg_style = MessageStyle(color or msg_style, style_attrs)
# If both color and style are not defined, then leave the text as is
if msg_style.color is None and len(msg_style.style) == 0:
return msg
escape_code = _get_ansi_code(msg_style)
# If invalid (or unknown) color, don't wrap msg with ansi codes
if escape_code:
return f"{escape_code}{msg}{ANSI_RESET}"
return msg
class TextReporter(BaseReporter):
"""Reports messages and layouts in plain text"""
__implements__ = IReporter
name = "text"
extension = "txt"
line_format = "{path}:{line}:{column}: {msg_id}: {msg} ({symbol})"
def __init__(self, output: Optional[TextIO] = None) -> None:
super().__init__(output)
self._modules: Set[str] = set()
self._template = self.line_format
self._fixed_template = self.line_format
"""The output format template with any unrecognized arguments removed"""
def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
"""Set the format template to be used and check for unrecognized arguments."""
template = str(self.linter.config.msg_template or self._template)
# Return early if the template is the same as the previous one
if template == self._template:
return
# Set template to the currently selected template
self._template = template
# Check to see if all parameters in the template are attributes of the Message
arguments = re.findall(r"\{(.+?)(:.*)?\}", template)
for argument in arguments:
if argument[0] not in Message._fields:
warnings.warn(
f"Don't recognize the argument '{argument[0]}' in the --msg-template. "
"Are you sure it is supported on the current version of pylint?"
)
template = re.sub(r"\{" + argument[0] + r"(:.*?)?\}", "", template)
self._fixed_template = template
def write_message(self, msg: Message) -> None:
"""Convenience method to write a formatted message with class default template"""
self_dict = msg._asdict()
for key in ("end_line", "end_column"):
self_dict[key] = self_dict[key] or ""
self.writeln(self._fixed_template.format(**self_dict))
def handle_message(self, msg: Message) -> None:
"""manage message of different type and in the context of path"""
if msg.module not in self._modules:
if msg.module:
self.writeln(f"************* Module {msg.module}")
self._modules.add(msg.module)
else:
self.writeln("************* ")
self.write_message(msg)
def _display(self, layout: "Section") -> None:
"""launch layouts display"""
print(file=self.out)
TextWriter().format(layout, self.out)
class ParseableTextReporter(TextReporter):
"""a reporter very similar to TextReporter, but display messages in a form
recognized by most text editors :
<filename>:<linenum>:<msg>
"""
name = "parseable"
line_format = "{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}"
def __init__(self, output: Optional[TextIO] = None) -> None:
warnings.warn(
f"{self.name} output format is deprecated. This is equivalent to --msg-template={self.line_format}",
DeprecationWarning,
)
super().__init__(output)
class VSTextReporter(ParseableTextReporter):
"""Visual studio text reporter"""
name = "msvs"
line_format = "{path}({line}): [{msg_id}({symbol}){obj}] {msg}"
class ColorizedTextReporter(TextReporter):
"""Simple TextReporter that colorizes text output"""
name = "colorized"
COLOR_MAPPING: ColorMappingDict = {
"I": MessageStyle("green", ()),
"C": MessageStyle(None, ("bold",)),
"R": MessageStyle("magenta", ("bold", "italic")),
"W": MessageStyle("magenta", ()),
"E": MessageStyle("red", ("bold",)),
"F": MessageStyle("red", ("bold", "underline")),
"S": MessageStyle("yellow", ("inverse",)), # S stands for module Separator
}
@overload
def __init__(
self,
output: Optional[TextIO] = None,
color_mapping: Optional[ColorMappingDict] = None,
) -> None:
...
@overload
def __init__(
self,
output: Optional[TextIO] = None,
color_mapping: Optional[Dict[str, Tuple[Optional[str], Optional[str]]]] = None,
) -> None:
# Remove for pylint 3.0
...
def __init__(
self,
output: Optional[TextIO] = None,
color_mapping: Union[
ColorMappingDict, Dict[str, Tuple[Optional[str], Optional[str]]], None
] = None,
) -> None:
super().__init__(output)
# pylint: disable-next=fixme
# TODO: Remove DeprecationWarning and only accept ColorMappingDict as color_mapping parameter
if color_mapping and not isinstance(
list(color_mapping.values())[0], MessageStyle
):
warnings.warn(
"In pylint 3.0, the ColoreziedTextReporter will only accept ColorMappingDict as color_mapping parameter",
DeprecationWarning,
)
temp_color_mapping: ColorMappingDict = {}
for key, value in color_mapping.items():
color = value[0]
style_attrs = tuple(_splitstrip(value[1]))
temp_color_mapping[key] = MessageStyle(color, style_attrs)
color_mapping = temp_color_mapping
else:
color_mapping = cast(Optional[ColorMappingDict], color_mapping)
self.color_mapping = color_mapping or ColorizedTextReporter.COLOR_MAPPING
ansi_terms = ["xterm-16color", "xterm-256color"]
if os.environ.get("TERM") not in ansi_terms:
if sys.platform == "win32":
# pylint: disable=import-outside-toplevel
import colorama
self.out = colorama.AnsiToWin32(self.out)
def _get_decoration(self, msg_id: str) -> MessageStyle:
"""Returns the message style as defined in self.color_mapping"""
return self.color_mapping.get(msg_id[0]) or MessageStyle(None, ())
def handle_message(self, msg: Message) -> None:
"""manage message of different types, and colorize output
using ansi escape codes
"""
if msg.module not in self._modules:
msg_style = self._get_decoration("S")
if msg.module:
modsep = colorize_ansi(f"************* Module {msg.module}", msg_style)
else:
modsep = colorize_ansi(f"************* {msg.module}", msg_style)
self.writeln(modsep)
self._modules.add(msg.module)
msg_style = self._get_decoration(msg.C)
msg = msg._replace(
**{
attr: colorize_ansi(getattr(msg, attr), msg_style)
for attr in ("msg", "symbol", "category", "C")
}
)
self.write_message(msg)
def register(linter: "PyLinter") -> None:
"""Register the reporter classes with the linter."""
linter.register_reporter(TextReporter)
linter.register_reporter(ParseableTextReporter)
linter.register_reporter(VSTextReporter)
linter.register_reporter(ColorizedTextReporter)
|