OLD | NEW |
1 # Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE). | 1 # Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE). |
2 # http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or modify it under | 4 # This program is free software; you can redistribute it and/or modify it under |
5 # the terms of the GNU General Public License as published by the Free Software | 5 # the terms of the GNU General Public License as published by the Free Software |
6 # Foundation; either version 2 of the License, or (at your option) any later | 6 # Foundation; either version 2 of the License, or (at your option) any later |
7 # version. | 7 # version. |
8 # | 8 # |
9 # This program is distributed in the hope that it will be useful, but WITHOUT | 9 # This program is distributed in the hope that it will be useful, but WITHOUT |
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | 11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License along with | 13 # You should have received a copy of the GNU General Public License along with |
14 # this program; if not, write to the Free Software Foundation, Inc., | 14 # this program; if not, write to the Free Software Foundation, Inc., |
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 15 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
16 """utilities methods and classes for checkers | 16 """utilities methods and classes for checkers |
17 | 17 |
18 Base id of standard checkers (used in msg and report ids): | 18 Base id of standard checkers (used in msg and report ids): |
19 01: base | 19 01: base |
20 02: classes | 20 02: classes |
21 03: format | 21 03: format |
22 04: import | 22 04: import |
23 05: misc | 23 05: misc |
24 06: variables | 24 06: variables |
25 07: exceptions | 25 07: exceptions |
26 08: similar | 26 08: similar |
27 09: design_analysis | 27 09: design_analysis |
28 10: newstyle | 28 10: newstyle |
29 11: typecheck | 29 11: typecheck |
30 12: logging | 30 12: logging |
31 13: string_format | 31 13: string_format |
32 14: string_constant | 32 14-50: not yet used: reserved for future internal checkers. |
33 15-50: not yet used: reserved for future internal checkers. | |
34 51-99: perhaps used: reserved for external checkers | 33 51-99: perhaps used: reserved for external checkers |
35 | 34 |
36 The raw_metrics checker has no number associated since it doesn't emit any | 35 The raw_metrics checker has no number associated since it doesn't emit any |
37 messages nor reports. XXX not true, emit a 07 report ! | 36 messages nor reports. XXX not true, emit a 07 report ! |
38 | 37 |
39 """ | 38 """ |
40 | 39 |
41 import sys | |
42 import tokenize | 40 import tokenize |
43 import warnings | 41 from os import listdir |
| 42 from os.path import dirname, join, isdir, splitext |
44 | 43 |
| 44 from logilab.astng.utils import ASTWalker |
45 from logilab.common.configuration import OptionsProviderMixIn | 45 from logilab.common.configuration import OptionsProviderMixIn |
46 | 46 |
47 from pylint.reporters import diff_string | 47 from pylint.reporters import diff_string, EmptyReport |
48 from pylint.utils import register_plugins | |
49 | 48 |
50 def table_lines_from_stats(stats, old_stats, columns): | 49 def table_lines_from_stats(stats, old_stats, columns): |
51 """get values listed in <columns> from <stats> and <old_stats>, | 50 """get values listed in <columns> from <stats> and <old_stats>, |
52 and return a formated list of values, designed to be given to a | 51 and return a formated list of values, designed to be given to a |
53 ureport.Table object | 52 ureport.Table object |
54 """ | 53 """ |
55 lines = [] | 54 lines = [] |
56 for m_type in columns: | 55 for m_type in columns: |
57 new = stats[m_type] | 56 new = stats[m_type] |
58 format = str | 57 format = str |
59 if isinstance(new, float): | 58 if isinstance(new, float): |
60 format = lambda num: '%.3f' % num | 59 format = lambda num: '%.3f' % num |
61 old = old_stats.get(m_type) | 60 old = old_stats.get(m_type) |
62 if old is not None: | 61 if old is not None: |
63 diff_str = diff_string(old, new) | 62 diff_str = diff_string(old, new) |
64 old = format(old) | 63 old = format(old) |
65 else: | 64 else: |
66 old, diff_str = 'NC', 'NC' | 65 old, diff_str = 'NC', 'NC' |
67 lines += (m_type.replace('_', ' '), format(new), old, diff_str) | 66 lines += (m_type.replace('_', ' '), format(new), old, diff_str) |
68 return lines | 67 return lines |
69 | 68 |
70 | 69 |
71 class BaseChecker(OptionsProviderMixIn): | 70 class BaseChecker(OptionsProviderMixIn, ASTWalker): |
72 """base class for checkers""" | 71 """base class for checkers""" |
73 # checker name (you may reuse an existing one) | 72 # checker name (you may reuse an existing one) |
74 name = None | 73 name = None |
75 # options level (0 will be displaying in --help, 1 in --long-help) | 74 # options level (0 will be displaying in --help, 1 in --long-help) |
76 level = 1 | 75 level = 1 |
77 # ordered list of options to control the ckecker behaviour | 76 # ordered list of options to control the ckecker behaviour |
78 options = () | 77 options = () |
79 # messages issued by this checker | 78 # messages issued by this checker |
80 msgs = {} | 79 msgs = {} |
81 # reports issued by this checker | 80 # reports issued by this checker |
82 reports = () | 81 reports = () |
83 | 82 |
84 def __init__(self, linter=None): | 83 def __init__(self, linter=None): |
85 """checker instances should have the linter as argument | 84 """checker instances should have the linter as argument |
86 | 85 |
87 linter is an object implementing ILinter | 86 linter is an object implementing ILinter |
88 """ | 87 """ |
| 88 ASTWalker.__init__(self, self) |
89 self.name = self.name.lower() | 89 self.name = self.name.lower() |
90 OptionsProviderMixIn.__init__(self) | 90 OptionsProviderMixIn.__init__(self) |
91 self.linter = linter | 91 self.linter = linter |
| 92 # messages that are active for the current check |
| 93 self.active_msgs = set() |
92 | 94 |
93 def add_message(self, msg_id, line=None, node=None, args=None): | 95 def add_message(self, msg_id, line=None, node=None, args=None): |
94 """add a message of a given type""" | 96 """add a message of a given type""" |
95 self.linter.add_message(msg_id, line, node, args) | 97 self.linter.add_message(msg_id, line, node, args) |
96 | 98 |
| 99 def package_dir(self): |
| 100 """return the base directory for the analysed package""" |
| 101 return dirname(self.linter.base_file) |
| 102 |
| 103 |
97 # dummy methods implementing the IChecker interface | 104 # dummy methods implementing the IChecker interface |
98 | 105 |
99 def open(self): | 106 def open(self): |
100 """called before visiting project (i.e set of modules)""" | 107 """called before visiting project (i.e set of modules)""" |
101 | 108 |
102 def close(self): | 109 def close(self): |
103 """called after visiting project (i.e set of modules)""" | 110 """called after visiting project (i.e set of modules)""" |
104 | 111 |
105 | |
106 class BaseRawChecker(BaseChecker): | 112 class BaseRawChecker(BaseChecker): |
107 """base class for raw checkers""" | 113 """base class for raw checkers""" |
108 | 114 |
109 def process_module(self, node): | 115 def process_module(self, node): |
110 """process a module | 116 """process a module |
111 | 117 |
112 the module's content is accessible via the stream object | 118 the module's content is accessible via the stream object |
113 | 119 |
114 stream must implement the readline method | 120 stream must implement the readline method |
115 """ | 121 """ |
116 warnings.warn("Modules that need access to the tokens should " | |
117 "use the ITokenChecker interface.", | |
118 DeprecationWarning) | |
119 stream = node.file_stream | 122 stream = node.file_stream |
120 stream.seek(0) # XXX may be removed with astroid > 0.23 | 123 stream.seek(0) # XXX may be removed with astng > 0.23 |
121 if sys.version_info <= (3, 0): | 124 self.process_tokens(tokenize.generate_tokens(stream.readline)) |
122 self.process_tokens(tokenize.generate_tokens(stream.readline)) | |
123 else: | |
124 self.process_tokens(tokenize.tokenize(stream.readline)) | |
125 | 125 |
126 def process_tokens(self, tokens): | 126 def process_tokens(self, tokens): |
127 """should be overridden by subclasses""" | 127 """should be overridden by subclasses""" |
128 raise NotImplementedError() | 128 raise NotImplementedError() |
129 | 129 |
130 | 130 |
131 class BaseTokenChecker(BaseChecker): | 131 PY_EXTS = ('.py', '.pyc', '.pyo', '.pyw', '.so', '.dll') |
132 """Base class for checkers that want to have access to the token stream.""" | |
133 | |
134 def process_tokens(self, tokens): | |
135 """Should be overridden by subclasses.""" | |
136 raise NotImplementedError() | |
137 | |
138 | 132 |
139 def initialize(linter): | 133 def initialize(linter): |
140 """initialize linter with checkers in this package """ | 134 """initialize linter with checkers in this package """ |
141 register_plugins(linter, __path__[0]) | 135 package_load(linter, __path__[0]) |
142 | 136 |
143 __all__ = ('BaseChecker', 'initialize') | 137 def package_load(linter, directory): |
| 138 """load all module and package in the given directory, looking for a |
| 139 'register' function in each one, used to register pylint checkers |
| 140 """ |
| 141 globs = globals() |
| 142 imported = {} |
| 143 for filename in listdir(directory): |
| 144 basename, extension = splitext(filename) |
| 145 if basename in imported or basename == '__pycache__': |
| 146 continue |
| 147 if extension in PY_EXTS and basename != '__init__' or ( |
| 148 not extension and basename != 'CVS' and |
| 149 isdir(join(directory, basename))): |
| 150 try: |
| 151 module = __import__(basename, globs, globs, None) |
| 152 except ValueError: |
| 153 # empty module name (usually emacs auto-save files) |
| 154 continue |
| 155 except ImportError, exc: |
| 156 import sys |
| 157 print >> sys.stderr, "Problem importing module %s: %s" % (filena
me, exc) |
| 158 else: |
| 159 if hasattr(module, 'register'): |
| 160 module.register(linter) |
| 161 imported[basename] = 1 |
| 162 |
| 163 __all__ = ('CheckerHandler', 'BaseChecker', 'initialize', 'package_load') |
OLD | NEW |