OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2003-2007 Sylvain Thenault (thenault@gmail.com). |
| 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 |
| 4 # 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 |
| 6 # version. |
| 7 # |
| 8 # 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 |
| 10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 11 # |
| 12 # 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., |
| 14 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 15 """Plain text reporters: |
| 16 |
| 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) |
| 21 :colorized: an ANSI colorized text reporter |
| 22 |
| 23 """ |
| 24 |
| 25 import os |
| 26 import sys |
| 27 |
| 28 from logilab.common.ureports import TextWriter |
| 29 from logilab.common.textutils import colorize_ansi |
| 30 |
| 31 from pylint.interfaces import IReporter |
| 32 from pylint.reporters import BaseReporter |
| 33 |
| 34 TITLE_UNDERLINES = ['', '=', '-', '.'] |
| 35 |
| 36 |
| 37 class TextReporter(BaseReporter): |
| 38 """reports messages and layouts in plain text |
| 39 """ |
| 40 |
| 41 __implements__ = IReporter |
| 42 extension = 'txt' |
| 43 |
| 44 def __init__(self, output=sys.stdout): |
| 45 BaseReporter.__init__(self, output) |
| 46 self._modules = {} |
| 47 |
| 48 def add_message(self, msg_id, location, msg): |
| 49 """manage message of different type and in the context of path""" |
| 50 module, obj, line, col_offset = location[1:] |
| 51 if module not in self._modules: |
| 52 if module: |
| 53 self.writeln('************* Module %s' % module) |
| 54 self._modules[module] = 1 |
| 55 else: |
| 56 self.writeln('************* %s' % module) |
| 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)) |
| 64 |
| 65 def _display(self, layout): |
| 66 """launch layouts display""" |
| 67 print >> self.out |
| 68 TextWriter().format(layout, self.out) |
| 69 |
| 70 |
| 71 class ParseableTextReporter(TextReporter): |
| 72 """a reporter very similar to TextReporter, but display messages in a form |
| 73 recognized by most text editors : |
| 74 |
| 75 <filename>:<linenum>:<msg> |
| 76 """ |
| 77 line_format = '%(path)s:%(line)s: [%(sigle)s%(obj)s] %(msg)s' |
| 78 |
| 79 def __init__(self, output=sys.stdout, relative=True): |
| 80 TextReporter.__init__(self, output) |
| 81 if relative: |
| 82 self._prefix = os.getcwd() + os.sep |
| 83 else: |
| 84 self._prefix = '' |
| 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()) |
| 98 |
| 99 class VSTextReporter(ParseableTextReporter): |
| 100 """Visual studio text reporter""" |
| 101 line_format = '%(path)s(%(line)s): [%(sigle)s%(obj)s] %(msg)s' |
| 102 |
| 103 class ColorizedTextReporter(TextReporter): |
| 104 """Simple TextReporter that colorizes text output""" |
| 105 |
| 106 COLOR_MAPPING = { |
| 107 "I" : ("green", None), |
| 108 'C' : (None, "bold"), |
| 109 'R' : ("magenta", "bold, italic"), |
| 110 'W' : ("blue", None), |
| 111 'E' : ("red", "bold"), |
| 112 'F' : ("red", "bold, underline"), |
| 113 'S' : ("yellow", "inverse"), # S stands for module Separator |
| 114 } |
| 115 |
| 116 def __init__(self, output=sys.stdout, color_mapping = None): |
| 117 TextReporter.__init__(self, output) |
| 118 self.color_mapping = color_mapping or \ |
| 119 dict(ColorizedTextReporter.COLOR_MAPPING) |
| 120 |
| 121 |
| 122 def _get_decoration(self, msg_id): |
| 123 """Returns the tuple color, style associated with msg_id as defined |
| 124 in self.color_mapping |
| 125 """ |
| 126 try: |
| 127 return self.color_mapping[msg_id[0]] |
| 128 except KeyError: |
| 129 return None, None |
| 130 |
| 131 def add_message(self, msg_id, location, msg): |
| 132 """manage message of different types, and colorize output |
| 133 using ansi escape codes |
| 134 """ |
| 135 module, obj, line, _ = location[1:] |
| 136 if module not in self._modules: |
| 137 color, style = self._get_decoration('S') |
| 138 if module: |
| 139 modsep = colorize_ansi('************* Module %s' % module, |
| 140 color, style) |
| 141 else: |
| 142 modsep = colorize_ansi('************* %s' % module, |
| 143 color, style) |
| 144 self.writeln(modsep) |
| 145 self._modules[module] = 1 |
| 146 if obj: |
| 147 obj = ':%s' % obj |
| 148 if self.include_ids: |
| 149 sigle = msg_id |
| 150 else: |
| 151 sigle = msg_id[0] |
| 152 color, style = self._get_decoration(sigle) |
| 153 msg = colorize_ansi(msg, color, style) |
| 154 sigle = colorize_ansi(sigle, color, style) |
| 155 self.writeln('%s:%3s%s: %s' % (sigle, line, obj, msg)) |
OLD | NEW |