OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE). |
| 2 # 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 # Foundation; either version 2 of the License, or (at your option) any later |
| 5 # version. |
| 6 # |
| 7 # 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 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 # |
| 11 # 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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 """utilities methods and classes for reporters""" |
| 15 from __future__ import print_function |
| 16 |
| 17 import sys |
| 18 import locale |
| 19 import os |
| 20 |
| 21 |
| 22 from pylint import utils |
| 23 |
| 24 CMPS = ['=', '-', '+'] |
| 25 |
| 26 # py3k has no more cmp builtin |
| 27 if sys.version_info >= (3, 0): |
| 28 def cmp(a, b): # pylint: disable=redefined-builtin |
| 29 return (a > b) - (a < b) |
| 30 |
| 31 def diff_string(old, new): |
| 32 """given a old and new int value, return a string representing the |
| 33 difference |
| 34 """ |
| 35 diff = abs(old - new) |
| 36 diff_str = "%s%s" % (CMPS[cmp(old, new)], diff and ('%.2f' % diff) or '') |
| 37 return diff_str |
| 38 |
| 39 |
| 40 class BaseReporter(object): |
| 41 """base class for reporters |
| 42 |
| 43 symbols: show short symbolic names for messages. |
| 44 """ |
| 45 |
| 46 extension = '' |
| 47 |
| 48 def __init__(self, output=None): |
| 49 self.linter = None |
| 50 # self.include_ids = None # Deprecated |
| 51 # self.symbols = None # Deprecated |
| 52 self.section = 0 |
| 53 self.out = None |
| 54 self.out_encoding = None |
| 55 self.encode = None |
| 56 self.set_output(output) |
| 57 # Build the path prefix to strip to get relative paths |
| 58 self.path_strip_prefix = os.getcwd() + os.sep |
| 59 |
| 60 def handle_message(self, msg): |
| 61 """Handle a new message triggered on the current file. |
| 62 |
| 63 Invokes the legacy add_message API by default.""" |
| 64 self.add_message( |
| 65 msg.msg_id, (msg.abspath, msg.module, msg.obj, msg.line, msg.column)
, |
| 66 msg.msg) |
| 67 |
| 68 def add_message(self, msg_id, location, msg): |
| 69 """Deprecated, do not use.""" |
| 70 raise NotImplementedError |
| 71 |
| 72 def set_output(self, output=None): |
| 73 """set output stream""" |
| 74 self.out = output or sys.stdout |
| 75 # py3k streams handle their encoding : |
| 76 if sys.version_info >= (3, 0): |
| 77 self.encode = lambda x: x |
| 78 return |
| 79 |
| 80 def encode(string): |
| 81 if not isinstance(string, unicode): |
| 82 return string |
| 83 encoding = (getattr(self.out, 'encoding', None) or |
| 84 locale.getdefaultlocale()[1] or |
| 85 sys.getdefaultencoding()) |
| 86 # errors=replace, we don't want to crash when attempting to show |
| 87 # source code line that can't be encoded with the current locale |
| 88 # settings |
| 89 return string.encode(encoding, 'replace') |
| 90 self.encode = encode |
| 91 |
| 92 def writeln(self, string=''): |
| 93 """write a line in the output buffer""" |
| 94 print(self.encode(string), file=self.out) |
| 95 |
| 96 def display_results(self, layout): |
| 97 """display results encapsulated in the layout tree""" |
| 98 self.section = 0 |
| 99 if hasattr(layout, 'report_id'): |
| 100 layout.children[0].children[0].data += ' (%s)' % layout.report_id |
| 101 self._display(layout) |
| 102 |
| 103 def _display(self, layout): |
| 104 """display the layout""" |
| 105 raise NotImplementedError() |
| 106 |
| 107 # Event callbacks |
| 108 |
| 109 def on_set_current_module(self, module, filepath): |
| 110 """starting analyzis of a module""" |
| 111 pass |
| 112 |
| 113 def on_close(self, stats, previous_stats): |
| 114 """global end of analyzis""" |
| 115 pass |
| 116 |
| 117 |
| 118 class CollectingReporter(BaseReporter): |
| 119 """collects messages""" |
| 120 |
| 121 name = 'collector' |
| 122 |
| 123 def __init__(self): |
| 124 BaseReporter.__init__(self) |
| 125 self.messages = [] |
| 126 |
| 127 def handle_message(self, msg): |
| 128 self.messages.append(msg) |
| 129 |
| 130 |
| 131 def initialize(linter): |
| 132 """initialize linter with reporters in this package """ |
| 133 utils.register_plugins(linter, __path__[0]) |
OLD | NEW |