OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE). |
| 2 # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 3 # |
| 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 |
| 6 # Foundation; either version 2 of the License, or (at your option) any later |
| 7 # version. |
| 8 # |
| 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 |
| 11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 # |
| 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., |
| 15 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 """utilities methods and classes for checkers |
| 17 |
| 18 Base id of standard checkers (used in msg and report ids): |
| 19 01: base |
| 20 02: classes |
| 21 03: format |
| 22 04: import |
| 23 05: misc |
| 24 06: variables |
| 25 07: exceptions |
| 26 08: similar |
| 27 09: design_analysis |
| 28 10: newstyle |
| 29 11: typecheck |
| 30 12: logging |
| 31 13: string_format |
| 32 14-50: not yet used: reserved for future internal checkers. |
| 33 51-99: perhaps used: reserved for external checkers |
| 34 |
| 35 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 |
| 38 """ |
| 39 |
| 40 import tokenize |
| 41 from os import listdir |
| 42 from os.path import dirname, join, isdir, splitext |
| 43 |
| 44 from logilab.astng.utils import ASTWalker |
| 45 from logilab.common.configuration import OptionsProviderMixIn |
| 46 |
| 47 from pylint.reporters import diff_string, EmptyReport |
| 48 |
| 49 def table_lines_from_stats(stats, old_stats, columns): |
| 50 """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 ureport.Table object |
| 53 """ |
| 54 lines = [] |
| 55 for m_type in columns: |
| 56 new = stats[m_type] |
| 57 format = str |
| 58 if isinstance(new, float): |
| 59 format = lambda num: '%.3f' % num |
| 60 old = old_stats.get(m_type) |
| 61 if old is not None: |
| 62 diff_str = diff_string(old, new) |
| 63 old = format(old) |
| 64 else: |
| 65 old, diff_str = 'NC', 'NC' |
| 66 lines += (m_type.replace('_', ' '), format(new), old, diff_str) |
| 67 return lines |
| 68 |
| 69 |
| 70 class BaseChecker(OptionsProviderMixIn, ASTWalker): |
| 71 """base class for checkers""" |
| 72 # checker name (you may reuse an existing one) |
| 73 name = None |
| 74 # options level (0 will be displaying in --help, 1 in --long-help) |
| 75 level = 1 |
| 76 # ordered list of options to control the ckecker behaviour |
| 77 options = () |
| 78 # messages issued by this checker |
| 79 msgs = {} |
| 80 # reports issued by this checker |
| 81 reports = () |
| 82 |
| 83 def __init__(self, linter=None): |
| 84 """checker instances should have the linter as argument |
| 85 |
| 86 linter is an object implementing ILinter |
| 87 """ |
| 88 ASTWalker.__init__(self, self) |
| 89 self.name = self.name.lower() |
| 90 OptionsProviderMixIn.__init__(self) |
| 91 self.linter = linter |
| 92 # messages that are active for the current check |
| 93 self.active_msgs = set() |
| 94 |
| 95 def add_message(self, msg_id, line=None, node=None, args=None): |
| 96 """add a message of a given type""" |
| 97 self.linter.add_message(msg_id, line, node, args) |
| 98 |
| 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 |
| 105 |
| 106 def open(self): |
| 107 """called before visiting project (i.e set of modules)""" |
| 108 |
| 109 def close(self): |
| 110 """called after visiting project (i.e set of modules)""" |
| 111 |
| 112 class BaseRawChecker(BaseChecker): |
| 113 """base class for raw checkers""" |
| 114 |
| 115 def process_module(self, node): |
| 116 """process a module |
| 117 |
| 118 the module's content is accessible via the stream object |
| 119 |
| 120 stream must implement the readline method |
| 121 """ |
| 122 stream = node.file_stream |
| 123 stream.seek(0) # XXX may be removed with astng > 0.23 |
| 124 self.process_tokens(tokenize.generate_tokens(stream.readline)) |
| 125 |
| 126 def process_tokens(self, tokens): |
| 127 """should be overridden by subclasses""" |
| 128 raise NotImplementedError() |
| 129 |
| 130 |
| 131 PY_EXTS = ('.py', '.pyc', '.pyo', '.pyw', '.so', '.dll') |
| 132 |
| 133 def initialize(linter): |
| 134 """initialize linter with checkers in this package """ |
| 135 package_load(linter, __path__[0]) |
| 136 |
| 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 |