OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2003-2013 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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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: string_constant |
| 33 15: stdlib |
| 34 16: python3 |
| 35 17-50: not yet used: reserved for future internal checkers. |
| 36 51-99: perhaps used: reserved for external checkers |
| 37 |
| 38 The raw_metrics checker has no number associated since it doesn't emit any |
| 39 messages nor reports. XXX not true, emit a 07 report ! |
| 40 |
| 41 """ |
| 42 |
| 43 import sys |
| 44 import tokenize |
| 45 import warnings |
| 46 |
| 47 from logilab.common.configuration import OptionsProviderMixIn |
| 48 |
| 49 from pylint.reporters import diff_string |
| 50 from pylint.utils import register_plugins |
| 51 from pylint.interfaces import UNDEFINED |
| 52 |
| 53 |
| 54 def table_lines_from_stats(stats, old_stats, columns): |
| 55 """get values listed in <columns> from <stats> and <old_stats>, |
| 56 and return a formated list of values, designed to be given to a |
| 57 ureport.Table object |
| 58 """ |
| 59 lines = [] |
| 60 for m_type in columns: |
| 61 new = stats[m_type] |
| 62 format = str # pylint: disable=redefined-builtin |
| 63 if isinstance(new, float): |
| 64 format = lambda num: '%.3f' % num |
| 65 old = old_stats.get(m_type) |
| 66 if old is not None: |
| 67 diff_str = diff_string(old, new) |
| 68 old = format(old) |
| 69 else: |
| 70 old, diff_str = 'NC', 'NC' |
| 71 lines += (m_type.replace('_', ' '), format(new), old, diff_str) |
| 72 return lines |
| 73 |
| 74 |
| 75 class BaseChecker(OptionsProviderMixIn): |
| 76 """base class for checkers""" |
| 77 # checker name (you may reuse an existing one) |
| 78 name = None |
| 79 # options level (0 will be displaying in --help, 1 in --long-help) |
| 80 level = 1 |
| 81 # ordered list of options to control the ckecker behaviour |
| 82 options = () |
| 83 # messages issued by this checker |
| 84 msgs = {} |
| 85 # reports issued by this checker |
| 86 reports = () |
| 87 # mark this checker as enabled or not. |
| 88 enabled = True |
| 89 |
| 90 def __init__(self, linter=None): |
| 91 """checker instances should have the linter as argument |
| 92 |
| 93 linter is an object implementing ILinter |
| 94 """ |
| 95 self.name = self.name.lower() |
| 96 OptionsProviderMixIn.__init__(self) |
| 97 self.linter = linter |
| 98 |
| 99 def add_message(self, msg_id, line=None, node=None, args=None, confidence=UN
DEFINED): |
| 100 """add a message of a given type""" |
| 101 self.linter.add_message(msg_id, line, node, args, confidence) |
| 102 |
| 103 # dummy methods implementing the IChecker interface |
| 104 |
| 105 def open(self): |
| 106 """called before visiting project (i.e set of modules)""" |
| 107 |
| 108 def close(self): |
| 109 """called after visiting project (i.e set of modules)""" |
| 110 |
| 111 |
| 112 class BaseTokenChecker(BaseChecker): |
| 113 """Base class for checkers that want to have access to the token stream.""" |
| 114 |
| 115 def process_tokens(self, tokens): |
| 116 """Should be overridden by subclasses.""" |
| 117 raise NotImplementedError() |
| 118 |
| 119 |
| 120 def initialize(linter): |
| 121 """initialize linter with checkers in this package """ |
| 122 register_plugins(linter, __path__[0]) |
| 123 |
| 124 __all__ = ('BaseChecker', 'initialize') |
OLD | NEW |