| OLD | NEW |
| 1 # Copyright (c) 2003-2007 Sylvain Thenault (thenault@gmail.com). | 1 # Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE). |
| 2 # Copyright (c) 2003-2011 LOGILAB S.A. (Paris, FRANCE). | |
| 3 # This program is free software; you can redistribute it and/or modify it under | 2 # This program is free software; you can redistribute it and/or modify it under |
| 4 # the terms of the GNU General Public License as published by the Free Software | 3 # the terms of the GNU General Public License as published by the Free Software |
| 5 # Foundation; either version 2 of the License, or (at your option) any later | 4 # Foundation; either version 2 of the License, or (at your option) any later |
| 6 # version. | 5 # version. |
| 7 # | 6 # |
| 8 # This program is distributed in the hope that it will be useful, but WITHOUT | 7 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | 9 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 11 # | 10 # |
| 12 # You should have received a copy of the GNU General Public License along with | 11 # You should have received a copy of the GNU General Public License along with |
| 13 # this program; if not, write to the Free Software Foundation, Inc., | 12 # this program; if not, write to the Free Software Foundation, Inc., |
| 14 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 13 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 15 """Plain text reporters: | 14 """Plain text reporters: |
| 16 | 15 |
| 17 :text: the default one grouping messages by module | 16 :text: the default one grouping messages by module |
| 18 :parseable: | |
| 19 standard parseable output with full module path on each message (for | |
| 20 editor integration) | |
| 21 :colorized: an ANSI colorized text reporter | 17 :colorized: an ANSI colorized text reporter |
| 22 | |
| 23 """ | 18 """ |
| 24 | 19 |
| 25 import os | 20 import warnings |
| 26 import sys | |
| 27 | 21 |
| 28 from logilab.common.ureports import TextWriter | 22 from logilab.common.ureports import TextWriter |
| 29 from logilab.common.textutils import colorize_ansi | 23 from logilab.common.textutils import colorize_ansi |
| 30 | 24 |
| 31 from pylint.interfaces import IReporter | 25 from pylint.interfaces import IReporter |
| 32 from pylint.reporters import BaseReporter | 26 from pylint.reporters import BaseReporter, Message |
| 33 | 27 |
| 34 TITLE_UNDERLINES = ['', '=', '-', '.'] | 28 TITLE_UNDERLINES = ['', '=', '-', '.'] |
| 35 | 29 |
| 36 | 30 |
| 37 class TextReporter(BaseReporter): | 31 class TextReporter(BaseReporter): |
| 38 """reports messages and layouts in plain text | 32 """reports messages and layouts in plain text""" |
| 39 """ | |
| 40 | 33 |
| 41 __implements__ = IReporter | 34 __implements__ = IReporter |
| 35 name = 'text' |
| 42 extension = 'txt' | 36 extension = 'txt' |
| 37 line_format = '{C}:{line:3d},{column:2d}: {msg} ({symbol})' |
| 43 | 38 |
| 44 def __init__(self, output=sys.stdout): | 39 def __init__(self, output=None): |
| 45 BaseReporter.__init__(self, output) | 40 BaseReporter.__init__(self, output) |
| 46 self._modules = {} | 41 self._modules = set() |
| 42 self._template = None |
| 43 |
| 44 def on_set_current_module(self, module, filepath): |
| 45 self._template = unicode(self.linter.config.msg_template or self.line_fo
rmat) |
| 46 |
| 47 def write_message(self, msg): |
| 48 """Convenience method to write a formated message with class default tem
plate""" |
| 49 self.writeln(msg.format(self._template)) |
| 47 | 50 |
| 48 def add_message(self, msg_id, location, msg): | 51 def add_message(self, msg_id, location, msg): |
| 49 """manage message of different type and in the context of path""" | 52 """manage message of different type and in the context of path""" |
| 50 path, module, obj, line, col_offset = location | 53 m = Message(self, msg_id, location, msg) |
| 51 if module not in self._modules: | 54 if m.module not in self._modules: |
| 52 if module: | 55 if m.module: |
| 53 self.writeln('************* Module %s' % (path if path else modu
le)) | 56 self.writeln('************* Module %s' % m.module) |
| 54 self._modules[module] = 1 | 57 self._modules.add(m.module) |
| 55 else: | 58 else: |
| 56 self.writeln('************* %s' % module) | 59 self.writeln('************* ') |
| 57 if obj: | 60 self.write_message(m) |
| 58 obj = ':%s' % obj | |
| 59 if self.include_ids: | |
| 60 sigle = msg_id | |
| 61 else: | |
| 62 sigle = msg_id[0] | |
| 63 self.writeln('%s:%3s,%s%s: %s' % (sigle, line, col_offset, obj, msg)) | |
| 64 | 61 |
| 65 def _display(self, layout): | 62 def _display(self, layout): |
| 66 """launch layouts display""" | 63 """launch layouts display""" |
| 67 print >> self.out | 64 print >> self.out |
| 68 TextWriter().format(layout, self.out) | 65 TextWriter().format(layout, self.out) |
| 69 | 66 |
| 70 | 67 |
| 71 class ParseableTextReporter(TextReporter): | 68 class ParseableTextReporter(TextReporter): |
| 72 """a reporter very similar to TextReporter, but display messages in a form | 69 """a reporter very similar to TextReporter, but display messages in a form |
| 73 recognized by most text editors : | 70 recognized by most text editors : |
| 74 | 71 |
| 75 <filename>:<linenum>:<msg> | 72 <filename>:<linenum>:<msg> |
| 76 """ | 73 """ |
| 77 line_format = '%(path)s:%(line)s: [%(sigle)s%(obj)s] %(msg)s' | 74 name = 'parseable' |
| 75 line_format = '{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}' |
| 78 | 76 |
| 79 def __init__(self, output=sys.stdout, relative=True): | 77 def __init__(self, output=None): |
| 78 warnings.warn('%s output format is deprecated. This is equivalent ' |
| 79 'to --msg-template=%s' % (self.name, self.line_format)) |
| 80 TextReporter.__init__(self, output) | 80 TextReporter.__init__(self, output) |
| 81 if relative: | |
| 82 self._prefix = os.getcwd() + os.sep | |
| 83 else: | |
| 84 self._prefix = '' | |
| 85 | 81 |
| 86 def add_message(self, msg_id, location, msg): | |
| 87 """manage message of different type and in the context of path""" | |
| 88 path, _, obj, line, _ = location | |
| 89 if obj: | |
| 90 obj = ', %s' % obj | |
| 91 if self.include_ids: | |
| 92 sigle = msg_id | |
| 93 else: | |
| 94 sigle = msg_id[0] | |
| 95 if self._prefix: | |
| 96 path = path.replace(self._prefix, '') | |
| 97 self.writeln(self.line_format % locals()) | |
| 98 | 82 |
| 99 class VSTextReporter(ParseableTextReporter): | 83 class VSTextReporter(ParseableTextReporter): |
| 100 """Visual studio text reporter""" | 84 """Visual studio text reporter""" |
| 101 line_format = '%(path)s(%(line)s): [%(sigle)s%(obj)s] %(msg)s' | 85 name = 'msvs' |
| 86 line_format = '{path}({line}): [{msg_id}({symbol}){obj}] {msg}' |
| 87 |
| 102 | 88 |
| 103 class ColorizedTextReporter(TextReporter): | 89 class ColorizedTextReporter(TextReporter): |
| 104 """Simple TextReporter that colorizes text output""" | 90 """Simple TextReporter that colorizes text output""" |
| 105 | 91 |
| 92 name = 'colorized' |
| 106 COLOR_MAPPING = { | 93 COLOR_MAPPING = { |
| 107 "I" : ("green", None), | 94 "I" : ("green", None), |
| 108 'C' : (None, "bold"), | 95 'C' : (None, "bold"), |
| 109 'R' : ("magenta", "bold, italic"), | 96 'R' : ("magenta", "bold, italic"), |
| 110 'W' : ("blue", None), | 97 'W' : ("blue", None), |
| 111 'E' : ("red", "bold"), | 98 'E' : ("red", "bold"), |
| 112 'F' : ("red", "bold, underline"), | 99 'F' : ("red", "bold, underline"), |
| 113 'S' : ("yellow", "inverse"), # S stands for module Separator | 100 'S' : ("yellow", "inverse"), # S stands for module Separator |
| 114 } | 101 } |
| 115 | 102 |
| 116 def __init__(self, output=sys.stdout, color_mapping = None): | 103 def __init__(self, output=None, color_mapping=None): |
| 117 TextReporter.__init__(self, output) | 104 TextReporter.__init__(self, output) |
| 118 self.color_mapping = color_mapping or \ | 105 self.color_mapping = color_mapping or \ |
| 119 dict(ColorizedTextReporter.COLOR_MAPPING) | 106 dict(ColorizedTextReporter.COLOR_MAPPING) |
| 120 | 107 |
| 121 | |
| 122 def _get_decoration(self, msg_id): | 108 def _get_decoration(self, msg_id): |
| 123 """Returns the tuple color, style associated with msg_id as defined | 109 """Returns the tuple color, style associated with msg_id as defined |
| 124 in self.color_mapping | 110 in self.color_mapping |
| 125 """ | 111 """ |
| 126 try: | 112 try: |
| 127 return self.color_mapping[msg_id[0]] | 113 return self.color_mapping[msg_id[0]] |
| 128 except KeyError: | 114 except KeyError: |
| 129 return None, None | 115 return None, None |
| 130 | 116 |
| 131 def add_message(self, msg_id, location, msg): | 117 def add_message(self, msg_id, location, msg): |
| 132 """manage message of different types, and colorize output | 118 """manage message of different types, and colorize output |
| 133 using ansi escape codes | 119 using ansi escape codes |
| 134 """ | 120 """ |
| 135 module, obj, line, _ = location[1:] | 121 msg = Message(self, msg_id, location, msg) |
| 136 if module not in self._modules: | 122 if msg.module not in self._modules: |
| 137 color, style = self._get_decoration('S') | 123 color, style = self._get_decoration('S') |
| 138 if module: | 124 if msg.module: |
| 139 modsep = colorize_ansi('************* Module %s' % module, | 125 modsep = colorize_ansi('************* Module %s' % msg.module, |
| 140 color, style) | 126 color, style) |
| 141 else: | 127 else: |
| 142 modsep = colorize_ansi('************* %s' % module, | 128 modsep = colorize_ansi('************* %s' % msg.module, |
| 143 color, style) | 129 color, style) |
| 144 self.writeln(modsep) | 130 self.writeln(modsep) |
| 145 self._modules[module] = 1 | 131 self._modules.add(msg.module) |
| 146 if obj: | 132 color, style = self._get_decoration(msg.C) |
| 147 obj = ':%s' % obj | 133 for attr in ('msg', 'symbol', 'category', 'C'): |
| 148 if self.include_ids: | 134 setattr(msg, attr, colorize_ansi(getattr(msg, attr), color, style)) |
| 149 sigle = msg_id | 135 self.write_message(msg) |
| 150 else: | 136 |
| 151 sigle = msg_id[0] | 137 |
| 152 color, style = self._get_decoration(sigle) | 138 def register(linter): |
| 153 msg = colorize_ansi(msg, color, style) | 139 """Register the reporter classes with the linter.""" |
| 154 sigle = colorize_ansi(sigle, color, style) | 140 linter.register_reporter(TextReporter) |
| 155 self.writeln('%s:%3s%s: %s' % (sigle, line, obj, msg)) | 141 linter.register_reporter(ParseableTextReporter) |
| 142 linter.register_reporter(VSTextReporter) |
| 143 linter.register_reporter(ColorizedTextReporter) |
| OLD | NEW |