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

144 lines
5.1 KiB
Python
Raw Normal View History

# Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE).
2006-04-26 10:48:09 +00:00
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
2014-02-23 23:02:47 +00:00
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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
"""
2006-04-26 10:48:09 +00:00
import warnings
2006-04-26 10:48:09 +00:00
from logilab.common.ureports import TextWriter
from logilab.common.textutils import colorize_ansi
from pylint.interfaces import IReporter
from pylint.reporters import BaseReporter, Message
2006-04-26 10:48:09 +00:00
TITLE_UNDERLINES = ['', '=', '-', '.']
class TextReporter(BaseReporter):
"""reports messages and layouts in plain text"""
2006-04-26 10:48:09 +00:00
__implements__ = IReporter
name = 'text'
2006-04-26 10:48:09 +00:00
extension = 'txt'
2013-07-31 07:07:54 +00:00
line_format = '{C}:{line:3d},{column:2d}: {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 = {}
self._template = None
def on_set_current_module(self, module, filepath):
self._template = unicode(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 add_message(self, msg_id, location, msg):
"""manage message of different type and in the context of path"""
m = Message(self, msg_id, location, msg)
if m.module not in self._modules:
if m.module:
self.writeln('************* Module %s' % m.module)
self._modules[m.module] = 1
2006-05-09 07:55:16 +00:00
else:
self.writeln('************* ')
self.write_message(m)
2006-04-26 10:48:09 +00:00
def _display(self, layout):
"""launch layouts display"""
2011-06-16 17:29:00 +00:00
print >> 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>
"""
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):
2013-12-20 16:32:30 +00:00
warnings.warn('%s output format is deprecated. This is equivalent '
'to --msg-template=%s' % (self.name, self.line_format))
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"""
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"""
name = 'colorized'
2006-04-26 10:48:09 +00:00
COLOR_MAPPING = {
"I" : ("green", None),
'C' : (None, "bold"),
'R' : ("magenta", "bold, italic"),
'W' : ("blue", None),
'E' : ("red", "bold"),
'F' : ("red", "bold, underline"),
'S' : ("yellow", "inverse"), # S stands for module Separator
}
def __init__(self, output=None, color_mapping=None):
2006-04-26 10:48:09 +00:00
TextReporter.__init__(self, output)
self.color_mapping = color_mapping or \
dict(ColorizedTextReporter.COLOR_MAPPING)
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 add_message(self, msg_id, location, msg):
"""manage message of different types, and colorize output
using ansi escape codes
"""
2013-07-31 07:05:01 +00:00
msg = Message(self, msg_id, location, msg)
if msg.module not in self._modules:
2006-04-26 10:48:09 +00:00
color, style = self._get_decoration('S')
2013-07-31 07:05:01 +00:00
if msg.module:
modsep = colorize_ansi('************* Module %s' % msg.module,
2006-05-09 07:55:16 +00:00
color, style)
else:
2013-07-31 07:05:01 +00:00
modsep = colorize_ansi('************* %s' % msg.module,
2006-05-09 07:55:16 +00:00
color, style)
2006-04-26 10:48:09 +00:00
self.writeln(modsep)
2013-07-31 07:05:01 +00:00
self._modules[msg.module] = 1
color, style = self._get_decoration(msg.C)
for attr in ('msg', 'symbol', 'category', 'C'):
2013-07-31 07:05:01 +00:00
setattr(msg, attr, colorize_ansi(getattr(msg, attr), color, style))
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)