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