third_party.pylibs.pylint.src/pylint/reporters/text.py

250 lines
7.8 KiB
Python
Raw Normal View History

# Copyright (c) 2006-2007, 2010-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
2017-12-15 11:24:15 +00:00
# Copyright (c) 2012-2014 Google, Inc.
# Copyright (c) 2014 Brett Cannon <brett@python.org>
# Copyright (c) 2014 Arun Persaud <arun@nubati.net>
2018-07-15 09:36:36 +00:00
# Copyright (c) 2015-2018 Claudiu Popa <pcmanticore@gmail.com>
2017-12-15 11:24:15 +00:00
# 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>
2018-07-15 09:36:36 +00:00
# Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com>
# Copyright (c) 2018 Jace Browning <jacebrowning@gmail.com>
# Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com>
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
2006-05-09 07:55:16 +00:00
"""Plain text reporters:
2006-04-26 10:48:09 +00:00
:text: the default one grouping messages by module
:colorized: an ANSI colorized text reporter
"""
from __future__ import print_function
2006-04-26 10:48:09 +00:00
import os
import sys
import warnings
2006-04-26 10:48:09 +00:00
from pylint import utils
2006-04-26 10:48:09 +00:00
from pylint.interfaces import IReporter
from pylint.reporters import BaseReporter
from pylint.reporters.ureports.text_writer import TextWriter
2018-09-16 15:33:50 +00:00
TITLE_UNDERLINES = ["", "=", "-", "."]
2006-04-26 10:48:09 +00:00
2018-09-16 15:33:50 +00:00
ANSI_PREFIX = "\033["
ANSI_END = "m"
ANSI_RESET = "\033[0m"
ANSI_STYLES = {
2018-09-16 15:33:50 +00:00
"reset": "0",
"bold": "1",
"italic": "3",
"underline": "4",
"blink": "5",
"inverse": "7",
"strike": "9",
}
ANSI_COLORS = {
2018-09-16 15:33:50 +00:00
"reset": "0",
"black": "30",
"red": "31",
"green": "32",
"yellow": "33",
"blue": "34",
"magenta": "35",
"cyan": "36",
"white": "37",
}
2018-09-16 15:33:50 +00:00
def _get_ansi_code(color=None, style=None):
"""return ansi escape code corresponding to color and style
:type color: str or None
:param color:
the color name (see `ANSI_COLORS` for available values)
or the color number when 256 colors are available
:type style: str or None
:param style:
style string (see `ANSI_COLORS` for available values). To get
several style effects at the same time, use a coma as separator.
:raise KeyError: if an unexistent color or style identifier is given
:rtype: str
:return: the built escape code
"""
ansi_code = []
if style:
style_attrs = utils._splitstrip(style)
for effect in style_attrs:
ansi_code.append(ANSI_STYLES[effect])
if color:
if color.isdigit():
2018-09-16 15:33:50 +00:00
ansi_code.extend(["38", "5"])
ansi_code.append(color)
else:
ansi_code.append(ANSI_COLORS[color])
if ansi_code:
2018-09-16 15:33:50 +00:00
return ANSI_PREFIX + ";".join(ansi_code) + ANSI_END
return ""
def colorize_ansi(msg, color=None, style=None):
"""colorize message by wrapping it with ansi escape codes
:type msg: str or unicode
:param msg: the message string to colorize
:type color: str or None
:param color:
the color identifier (see `ANSI_COLORS` for available values)
:type style: str or None
:param style:
style string (see `ANSI_COLORS` for available values). To get
several style effects at the same time, use a coma as separator.
:raise KeyError: if an unexistent color or style identifier is given
:rtype: str or unicode
:return: the ansi escaped string
"""
# If both color and style are not defined, then leave the text as is
if color is None and style is None:
return msg
escape_code = _get_ansi_code(color, style)
# If invalid (or unknown) color, don't wrap msg with ansi codes
if escape_code:
2018-09-16 15:33:50 +00:00
return "%s%s%s" % (escape_code, msg, ANSI_RESET)
return msg
2006-04-26 10:48:09 +00:00
class TextReporter(BaseReporter):
"""reports messages and layouts in plain text"""
2006-04-26 10:48:09 +00:00
__implements__ = IReporter
2018-09-16 15:33:50 +00:00
name = "text"
extension = "txt"
line_format = "{path}:{line}:{column}: {msg_id}: {msg} ({symbol})"
2011-06-16 17:29:00 +00:00
def __init__(self, output=None):
2006-04-26 10:48:09 +00:00
BaseReporter.__init__(self, output)
self._modules = set()
self._template = None
def on_set_current_module(self, module, filepath):
2018-05-27 05:12:14 +00:00
self._template = str(self.linter.config.msg_template or self.line_format)
2013-07-31 07:05:01 +00:00
def write_message(self, msg):
"""Convenience method to write a formated message with class default template"""
2013-07-31 07:05:01 +00:00
self.writeln(msg.format(self._template))
2006-04-26 10:48:09 +00:00
def handle_message(self, msg):
2006-04-26 10:48:09 +00:00
"""manage message of different type and in the context of path"""
if msg.module not in self._modules:
if msg.module:
2018-09-16 15:33:50 +00:00
self.writeln("************* Module %s" % msg.module)
self._modules.add(msg.module)
2006-05-09 07:55:16 +00:00
else:
2018-09-16 15:33:50 +00:00
self.writeln("************* ")
self.write_message(msg)
2006-04-26 10:48:09 +00:00
def _display(self, layout):
"""launch layouts display"""
print(file=self.out)
2006-04-26 10:48:09 +00:00
TextWriter().format(layout, self.out)
class ParseableTextReporter(TextReporter):
2006-04-26 10:48:09 +00:00
"""a reporter very similar to TextReporter, but display messages in a form
recognized by most text editors :
2011-06-16 17:29:00 +00:00
2006-04-26 10:48:09 +00:00
<filename>:<linenum>:<msg>
"""
2018-09-16 15:33:50 +00:00
name = "parseable"
line_format = "{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}"
2011-06-16 17:29:00 +00:00
2013-07-31 07:05:01 +00:00
def __init__(self, output=None):
2018-09-16 15:33:50 +00:00
warnings.warn(
"%s output format is deprecated. This is equivalent "
"to --msg-template=%s" % (self.name, self.line_format),
DeprecationWarning,
)
2006-04-26 10:48:09 +00:00
TextReporter.__init__(self, output)
2011-06-16 17:29:00 +00:00
2012-09-19 15:36:47 +00:00
2007-02-19 12:52:19 +00:00
class VSTextReporter(ParseableTextReporter):
"""Visual studio text reporter"""
2018-09-16 15:33:50 +00:00
name = "msvs"
line_format = "{path}({line}): [{msg_id}({symbol}){obj}] {msg}"
2011-06-16 17:29:00 +00:00
2006-04-26 10:48:09 +00:00
class ColorizedTextReporter(TextReporter):
"""Simple TextReporter that colorizes text output"""
2018-09-16 15:33:50 +00:00
name = "colorized"
2006-04-26 10:48:09 +00:00
COLOR_MAPPING = {
2018-09-16 15:33:50 +00:00
"I": ("green", None),
"C": (None, "bold"),
"R": ("magenta", "bold, italic"),
"W": ("magenta", None),
"E": ("red", "bold"),
"F": ("red", "bold, underline"),
"S": ("yellow", "inverse"), # S stands for module Separator
2006-04-26 10:48:09 +00:00
}
def __init__(self, output=None, color_mapping=None):
2006-04-26 10:48:09 +00:00
TextReporter.__init__(self, output)
2018-09-16 15:33:50 +00:00
self.color_mapping = color_mapping or dict(ColorizedTextReporter.COLOR_MAPPING)
ansi_terms = ["xterm-16color", "xterm-256color"]
if os.environ.get("TERM") not in ansi_terms:
if sys.platform == "win32":
2018-01-03 02:08:48 +00:00
# pylint: disable=import-error
import colorama
2018-09-16 15:33:50 +00:00
self.out = colorama.AnsiToWin32(self.out)
2011-06-16 17:29:00 +00:00
2006-04-26 10:48:09 +00:00
def _get_decoration(self, msg_id):
"""Returns the tuple color, style associated with msg_id as defined
in self.color_mapping
"""
try:
return self.color_mapping[msg_id[0]]
2006-04-26 10:48:09 +00:00
except KeyError:
return None, None
def handle_message(self, msg):
2006-04-26 10:48:09 +00:00
"""manage message of different types, and colorize output
using ansi escape codes
"""
2013-07-31 07:05:01 +00:00
if msg.module not in self._modules:
2018-09-16 15:33:50 +00:00
color, style = self._get_decoration("S")
2013-07-31 07:05:01 +00:00
if msg.module:
2018-09-16 15:33:50 +00:00
modsep = colorize_ansi(
"************* Module %s" % msg.module, color, style
)
2006-05-09 07:55:16 +00:00
else:
2018-09-16 15:33:50 +00:00
modsep = colorize_ansi("************* %s" % msg.module, color, style)
2006-04-26 10:48:09 +00:00
self.writeln(modsep)
self._modules.add(msg.module)
2013-07-31 07:05:01 +00:00
color, style = self._get_decoration(msg.C)
msg = msg._replace(
2018-09-16 15:33:50 +00:00
**{
attr: colorize_ansi(getattr(msg, attr), color, style)
for attr in ("msg", "symbol", "category", "C")
}
)
2013-07-31 07:05:01 +00:00
self.write_message(msg)
def register(linter):
"""Register the reporter classes with the linter."""
linter.register_reporter(TextReporter)
linter.register_reporter(ParseableTextReporter)
linter.register_reporter(VSTextReporter)
linter.register_reporter(ColorizedTextReporter)