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