OLD | NEW |
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 | 1 # 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 | 2 # 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 | 3 # Foundation; either version 2 of the License, or (at your option) any later |
5 # version. | 4 # version. |
6 # | 5 # |
7 # This program is distributed in the hope that it will be useful, but WITHOUT | 6 # 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 | 7 # 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. | 8 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
10 # | 9 # |
11 # You should have received a copy of the GNU General Public License along with | 10 # 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., | 11 # this program; if not, write to the Free Software Foundation, Inc., |
13 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 12 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
14 """utilities methods and classes for reporters""" | 13 """utilities methods and classes for reporters |
15 | 14 |
16 import sys | 15 Copyright (c) 2000-2003 LOGILAB S.A. (Paris, FRANCE). |
17 import locale | 16 http://www.logilab.fr/ -- mailto:contact@logilab.fr |
18 import os | 17 """ |
19 | 18 |
20 from pylint.utils import MSG_TYPES | 19 import sys, locale |
21 | |
22 from pylint import utils | |
23 | 20 |
24 CMPS = ['=', '-', '+'] | 21 CMPS = ['=', '-', '+'] |
25 | 22 |
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 | |
35 def diff_string(old, new): | 23 def diff_string(old, new): |
36 """given a old and new int value, return a string representing the | 24 """given a old and new int value, return a string representing the |
37 difference | 25 difference |
38 """ | 26 """ |
39 diff = abs(old - new) | 27 diff = abs(old - new) |
40 diff_str = "%s%s" % (CMPS[cmp(old, new)], diff and ('%.2f' % diff) or '') | 28 diff_str = "%s%s" % (CMPS[cmp(old, new)], diff and ('%.2f' % diff) or '') |
41 return diff_str | 29 return diff_str |
42 | 30 |
43 | 31 |
44 class Message(object): | 32 class EmptyReport(Exception): |
45 """This class represent a message to be issued by the reporters""" | 33 """raised when a report is empty and so should not be displayed""" |
46 | 34 |
47 def __init__(self, reporter, msg_id, location, msg): | 35 class BaseReporter: |
48 self.msg_id = msg_id | 36 """base class for reporters""" |
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 """ | |
70 | 37 |
71 extension = '' | 38 extension = '' |
72 | 39 |
73 def __init__(self, output=None): | 40 def __init__(self, output=None): |
74 self.linter = None | 41 self.linter = None |
75 # self.include_ids = None # Deprecated | 42 self.include_ids = None |
76 # self.symbols = None # Deprecated | |
77 self.section = 0 | 43 self.section = 0 |
78 self.out = None | 44 self.out = None |
79 self.out_encoding = None | 45 self.out_encoding = None |
80 self.encode = None | |
81 self.set_output(output) | 46 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 | |
89 | 47 |
90 def set_output(self, output=None): | 48 def set_output(self, output=None): |
91 """set output stream""" | 49 """set output stream""" |
92 self.out = output or sys.stdout | 50 self.out = output or sys.stdout |
93 # py3k streams handle their encoding : | 51 # py3k streams handle their encoding : |
94 if sys.version_info >= (3, 0): | 52 if sys.version_info >= (3, 0): |
95 self.encode = lambda x: x | 53 self.encode = lambda x: x |
96 return | 54 return |
97 | 55 |
98 def encode(string): | 56 def encode(string): |
99 if not isinstance(string, unicode): | 57 if not isinstance(string, unicode): |
100 return string | 58 return string |
101 encoding = (getattr(self.out, 'encoding', None) or | 59 encoding = (getattr(self.out, 'encoding', None) or |
102 locale.getdefaultlocale()[1] or | 60 locale.getdefaultlocale()[1] or |
103 sys.getdefaultencoding()) | 61 sys.getdefaultencoding()) |
104 # errors=replace, we don't want to crash when attempting to show | 62 return string.encode(encoding) |
105 # source code line that can't be encoded with the current locale | |
106 # settings | |
107 return string.encode(encoding, 'replace') | |
108 self.encode = encode | 63 self.encode = encode |
109 | 64 |
110 def writeln(self, string=''): | 65 def writeln(self, string=''): |
111 """write a line in the output buffer""" | 66 """write a line in the output buffer""" |
112 print >> self.out, self.encode(string) | 67 print >> self.out, self.encode(string) |
113 | 68 |
114 def display_results(self, layout): | 69 def display_results(self, layout): |
115 """display results encapsulated in the layout tree""" | 70 """display results encapsulated in the layout tree""" |
116 self.section = 0 | 71 self.section = 0 |
117 if hasattr(layout, 'report_id'): | 72 if self.include_ids and hasattr(layout, 'report_id'): |
118 layout.children[0].children[0].data += ' (%s)' % layout.report_id | 73 layout.children[0].children[0].data += ' (%s)' % layout.report_id |
119 self._display(layout) | 74 self._display(layout) |
120 | 75 |
121 def _display(self, layout): | 76 def _display(self, layout): |
122 """display the layout""" | 77 """display the layout""" |
123 raise NotImplementedError() | 78 raise NotImplementedError() |
124 | 79 |
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 |