| OLD | NEW |
| 1 # Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE). | 1 # Copyright (c) 2003-2013 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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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-50: not yet used: reserved for future internal checkers. | 32 14: string_constant |
| 33 15-50: not yet used: reserved for future internal checkers. |
| 33 51-99: perhaps used: reserved for external checkers | 34 51-99: perhaps used: reserved for external checkers |
| 34 | 35 |
| 35 The raw_metrics checker has no number associated since it doesn't emit any | 36 The raw_metrics checker has no number associated since it doesn't emit any |
| 36 messages nor reports. XXX not true, emit a 07 report ! | 37 messages nor reports. XXX not true, emit a 07 report ! |
| 37 | 38 |
| 38 """ | 39 """ |
| 39 | 40 |
| 41 import sys |
| 40 import tokenize | 42 import tokenize |
| 41 from os import listdir | 43 import warnings |
| 42 from os.path import dirname, join, isdir, splitext | |
| 43 | 44 |
| 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, EmptyReport | 47 from pylint.reporters import diff_string |
| 48 from pylint.utils import register_plugins |
| 48 | 49 |
| 49 def table_lines_from_stats(stats, old_stats, columns): | 50 def table_lines_from_stats(stats, old_stats, columns): |
| 50 """get values listed in <columns> from <stats> and <old_stats>, | 51 """get values listed in <columns> from <stats> and <old_stats>, |
| 51 and return a formated list of values, designed to be given to a | 52 and return a formated list of values, designed to be given to a |
| 52 ureport.Table object | 53 ureport.Table object |
| 53 """ | 54 """ |
| 54 lines = [] | 55 lines = [] |
| 55 for m_type in columns: | 56 for m_type in columns: |
| 56 new = stats[m_type] | 57 new = stats[m_type] |
| 57 format = str | 58 format = str |
| 58 if isinstance(new, float): | 59 if isinstance(new, float): |
| 59 format = lambda num: '%.3f' % num | 60 format = lambda num: '%.3f' % num |
| 60 old = old_stats.get(m_type) | 61 old = old_stats.get(m_type) |
| 61 if old is not None: | 62 if old is not None: |
| 62 diff_str = diff_string(old, new) | 63 diff_str = diff_string(old, new) |
| 63 old = format(old) | 64 old = format(old) |
| 64 else: | 65 else: |
| 65 old, diff_str = 'NC', 'NC' | 66 old, diff_str = 'NC', 'NC' |
| 66 lines += (m_type.replace('_', ' '), format(new), old, diff_str) | 67 lines += (m_type.replace('_', ' '), format(new), old, diff_str) |
| 67 return lines | 68 return lines |
| 68 | 69 |
| 69 | 70 |
| 70 class BaseChecker(OptionsProviderMixIn, ASTWalker): | 71 class BaseChecker(OptionsProviderMixIn): |
| 71 """base class for checkers""" | 72 """base class for checkers""" |
| 72 # checker name (you may reuse an existing one) | 73 # checker name (you may reuse an existing one) |
| 73 name = None | 74 name = None |
| 74 # options level (0 will be displaying in --help, 1 in --long-help) | 75 # options level (0 will be displaying in --help, 1 in --long-help) |
| 75 level = 1 | 76 level = 1 |
| 76 # ordered list of options to control the ckecker behaviour | 77 # ordered list of options to control the ckecker behaviour |
| 77 options = () | 78 options = () |
| 78 # messages issued by this checker | 79 # messages issued by this checker |
| 79 msgs = {} | 80 msgs = {} |
| 80 # reports issued by this checker | 81 # reports issued by this checker |
| 81 reports = () | 82 reports = () |
| 82 | 83 |
| 83 def __init__(self, linter=None): | 84 def __init__(self, linter=None): |
| 84 """checker instances should have the linter as argument | 85 """checker instances should have the linter as argument |
| 85 | 86 |
| 86 linter is an object implementing ILinter | 87 linter is an object implementing ILinter |
| 87 """ | 88 """ |
| 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() | |
| 94 | 92 |
| 95 def add_message(self, msg_id, line=None, node=None, args=None): | 93 def add_message(self, msg_id, line=None, node=None, args=None): |
| 96 """add a message of a given type""" | 94 """add a message of a given type""" |
| 97 self.linter.add_message(msg_id, line, node, args) | 95 self.linter.add_message(msg_id, line, node, args) |
| 98 | 96 |
| 99 def package_dir(self): | |
| 100 """return the base directory for the analysed package""" | |
| 101 return dirname(self.linter.base_file) | |
| 102 | |
| 103 | |
| 104 # dummy methods implementing the IChecker interface | 97 # dummy methods implementing the IChecker interface |
| 105 | 98 |
| 106 def open(self): | 99 def open(self): |
| 107 """called before visiting project (i.e set of modules)""" | 100 """called before visiting project (i.e set of modules)""" |
| 108 | 101 |
| 109 def close(self): | 102 def close(self): |
| 110 """called after visiting project (i.e set of modules)""" | 103 """called after visiting project (i.e set of modules)""" |
| 111 | 104 |
| 105 |
| 112 class BaseRawChecker(BaseChecker): | 106 class BaseRawChecker(BaseChecker): |
| 113 """base class for raw checkers""" | 107 """base class for raw checkers""" |
| 114 | 108 |
| 115 def process_module(self, node): | 109 def process_module(self, node): |
| 116 """process a module | 110 """process a module |
| 117 | 111 |
| 118 the module's content is accessible via the stream object | 112 the module's content is accessible via the stream object |
| 119 | 113 |
| 120 stream must implement the readline method | 114 stream must implement the readline method |
| 121 """ | 115 """ |
| 116 warnings.warn("Modules that need access to the tokens should " |
| 117 "use the ITokenChecker interface.", |
| 118 DeprecationWarning) |
| 122 stream = node.file_stream | 119 stream = node.file_stream |
| 123 stream.seek(0) # XXX may be removed with astng > 0.23 | 120 stream.seek(0) # XXX may be removed with astroid > 0.23 |
| 124 self.process_tokens(tokenize.generate_tokens(stream.readline)) | 121 if sys.version_info <= (3, 0): |
| 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 PY_EXTS = ('.py', '.pyc', '.pyo', '.pyw', '.so', '.dll') | 131 class BaseTokenChecker(BaseChecker): |
| 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 |
| 132 | 138 |
| 133 def initialize(linter): | 139 def initialize(linter): |
| 134 """initialize linter with checkers in this package """ | 140 """initialize linter with checkers in this package """ |
| 135 package_load(linter, __path__[0]) | 141 register_plugins(linter, __path__[0]) |
| 136 | 142 |
| 137 def package_load(linter, directory): | 143 __all__ = ('BaseChecker', 'initialize') |
| 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 |