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