OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2003-2010 Sylvain Thenault (thenault@gmail.com). |
| 2 # Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE). |
| 3 # This program is free software; you can redistribute it and/or modify it under |
| 4 # the terms of the GNU General Public License as published by the Free Software |
| 5 # Foundation; either version 2 of the License, or (at your option) any later |
| 6 # version. |
| 7 # |
| 8 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 11 # |
| 12 # You should have received a copy of the GNU General Public License along with |
| 13 # this program; if not, write to the Free Software Foundation, Inc., |
| 14 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 15 """Python code format's checker. |
| 16 |
| 17 By default try to follow Guido's style guide : |
| 18 |
| 19 http://www.python.org/doc/essays/styleguide.html |
| 20 |
| 21 Some parts of the process_token method is based from The Tab Nanny std module. |
| 22 """ |
| 23 |
| 24 import re, sys |
| 25 import tokenize |
| 26 if not hasattr(tokenize, 'NL'): |
| 27 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") |
| 28 |
| 29 from logilab.common.textutils import pretty_match |
| 30 from logilab.astng import nodes |
| 31 |
| 32 from pylint.interfaces import IRawChecker, IASTNGChecker |
| 33 from pylint.checkers import BaseRawChecker |
| 34 from pylint.checkers.utils import check_messages |
| 35 |
| 36 MSGS = { |
| 37 'C0301': ('Line too long (%s/%s)', |
| 38 'Used when a line is longer than a given number of characters.'), |
| 39 'C0302': ('Too many lines in module (%s)', # was W0302 |
| 40 'Used when a module has too much lines, reducing its readability.' |
| 41 ), |
| 42 |
| 43 'W0311': ('Bad indentation. Found %s %s, expected %s', |
| 44 'Used when an unexpected number of indentation\'s tabulations or ' |
| 45 'spaces has been found.'), |
| 46 'W0312': ('Found indentation with %ss instead of %ss', |
| 47 'Used when there are some mixed tabs and spaces in a module.'), |
| 48 'W0301': ('Unnecessary semicolon', # was W0106 |
| 49 'Used when a statement is ended by a semi-colon (";"), which \ |
| 50 isn\'t necessary (that\'s python, not C ;).'), |
| 51 'C0321': ('More than one statement on a single line', |
| 52 'Used when more than on statement are found on the same line.'), |
| 53 'C0322': ('Operator not preceded by a space\n%s', |
| 54 'Used when one of the following operator (!= | <= | == | >= | < ' |
| 55 '| > | = | \+= | -= | \*= | /= | %) is not preceded by a space.'), |
| 56 'C0323': ('Operator not followed by a space\n%s', |
| 57 'Used when one of the following operator (!= | <= | == | >= | < ' |
| 58 '| > | = | \+= | -= | \*= | /= | %) is not followed by a space.'), |
| 59 'C0324': ('Comma not followed by a space\n%s', |
| 60 'Used when a comma (",") is not followed by a space.'), |
| 61 } |
| 62 |
| 63 if sys.version_info < (3, 0): |
| 64 |
| 65 MSGS.update({ |
| 66 'W0331': ('Use of the <> operator', |
| 67 'Used when the deprecated "<>" operator is used instead \ |
| 68 of "!=".'), |
| 69 'W0332': ('Use l as long integer identifier', |
| 70 'Used when a lower case "l" is used to mark a long integer. You ' |
| 71 'should use a upper case "L" since the letter "l" looks too much ' |
| 72 'like the digit "1"'), |
| 73 'W0333': ('Use of the `` operator', |
| 74 'Used when the deprecated "``" (backtick) operator is used ' |
| 75 'instead of the str() function.'), |
| 76 }) |
| 77 |
| 78 # simple quoted string rgx |
| 79 SQSTRING_RGX = r'"([^"\\]|\\.)*?"' |
| 80 # simple apostrophed rgx |
| 81 SASTRING_RGX = r"'([^'\\]|\\.)*?'" |
| 82 # triple quoted string rgx |
| 83 TQSTRING_RGX = r'"""([^"]|("(?!"")))*?(""")' |
| 84 # triple apostrophed string rgx # FIXME english please |
| 85 TASTRING_RGX = r"'''([^']|('(?!'')))*?(''')" |
| 86 |
| 87 # finally, the string regular expression |
| 88 STRING_RGX = re.compile('(%s)|(%s)|(%s)|(%s)' % (TQSTRING_RGX, TASTRING_RGX, |
| 89 SQSTRING_RGX, SASTRING_RGX), |
| 90 re.MULTILINE|re.DOTALL) |
| 91 |
| 92 COMMENT_RGX = re.compile("#.*$", re.M) |
| 93 |
| 94 OPERATORS = r'!=|<=|==|>=|<|>|=|\+=|-=|\*=|/=|%' |
| 95 |
| 96 OP_RGX_MATCH_1 = r'[^(]*(?<!\s|\^|<|>|=|\+|-|\*|/|!|%%|&|\|)(%s).*' % OPERATORS |
| 97 OP_RGX_SEARCH_1 = r'(?<!\s|\^|<|>|=|\+|-|\*|/|!|%%|&|\|)(%s)' % OPERATORS |
| 98 |
| 99 OP_RGX_MATCH_2 = r'[^(]*(%s)(?!\s|=|>|<).*' % OPERATORS |
| 100 OP_RGX_SEARCH_2 = r'(%s)(?!\s|=|>)' % OPERATORS |
| 101 |
| 102 BAD_CONSTRUCT_RGXS = ( |
| 103 |
| 104 (re.compile(OP_RGX_MATCH_1, re.M), |
| 105 re.compile(OP_RGX_SEARCH_1, re.M), |
| 106 'C0322'), |
| 107 |
| 108 (re.compile(OP_RGX_MATCH_2, re.M), |
| 109 re.compile(OP_RGX_SEARCH_2, re.M), |
| 110 'C0323'), |
| 111 |
| 112 (re.compile(r'.*,[^(\s|\]|}|\))].*', re.M), |
| 113 re.compile(r',[^\s)]', re.M), |
| 114 'C0324'), |
| 115 ) |
| 116 |
| 117 |
| 118 def get_string_coords(line): |
| 119 """return a list of string positions (tuple (start, end)) in the line |
| 120 """ |
| 121 result = [] |
| 122 for match in re.finditer(STRING_RGX, line): |
| 123 result.append( (match.start(), match.end()) ) |
| 124 return result |
| 125 |
| 126 def in_coords(match, string_coords): |
| 127 """return true if the match is in the string coord""" |
| 128 mstart = match.start() |
| 129 for start, end in string_coords: |
| 130 if mstart >= start and mstart < end: |
| 131 return True |
| 132 return False |
| 133 |
| 134 def check_line(line): |
| 135 """check a line for a bad construction |
| 136 if it founds one, return a message describing the problem |
| 137 else return None |
| 138 """ |
| 139 cleanstr = COMMENT_RGX.sub('', STRING_RGX.sub('', line)) |
| 140 for rgx_match, rgx_search, msg_id in BAD_CONSTRUCT_RGXS: |
| 141 if rgx_match.match(cleanstr): |
| 142 string_positions = get_string_coords(line) |
| 143 for match in re.finditer(rgx_search, line): |
| 144 if not in_coords(match, string_positions): |
| 145 return msg_id, pretty_match(match, line.rstrip()) |
| 146 |
| 147 |
| 148 class FormatChecker(BaseRawChecker): |
| 149 """checks for : |
| 150 * unauthorized constructions |
| 151 * strict indentation |
| 152 * line length |
| 153 * use of <> instead of != |
| 154 """ |
| 155 |
| 156 __implements__ = (IRawChecker, IASTNGChecker) |
| 157 |
| 158 # configuration section name |
| 159 name = 'format' |
| 160 # messages |
| 161 msgs = MSGS |
| 162 # configuration options |
| 163 # for available dict keys/values see the optik parser 'add_option' method |
| 164 options = (('max-line-length', |
| 165 {'default' : 80, 'type' : "int", 'metavar' : '<int>', |
| 166 'help' : 'Maximum number of characters on a single line.'}), |
| 167 ('max-module-lines', |
| 168 {'default' : 1000, 'type' : 'int', 'metavar' : '<int>', |
| 169 'help': 'Maximum number of lines in a module'} |
| 170 ), |
| 171 ('indent-string', |
| 172 {'default' : ' ', 'type' : "string", 'metavar' : '<string>', |
| 173 'help' : 'String used as indentation unit. This is usually \ |
| 174 " " (4 spaces) or "\\t" (1 tab).'}), |
| 175 ) |
| 176 def __init__(self, linter=None): |
| 177 BaseRawChecker.__init__(self, linter) |
| 178 self._lines = None |
| 179 self._visited_lines = None |
| 180 |
| 181 def process_module(self, node): |
| 182 """extracts encoding from the stream and decodes each line, so that |
| 183 international text's length is properly calculated. |
| 184 """ |
| 185 stream = node.file_stream |
| 186 stream.seek(0) # XXX may be removed with astng > 0.23 |
| 187 readline = stream.readline |
| 188 if sys.version_info < (3, 0): |
| 189 if node.file_encoding is not None: |
| 190 readline = lambda: stream.readline().decode(node.file_encoding,
'replace') |
| 191 self.process_tokens(tokenize.generate_tokens(readline)) |
| 192 |
| 193 def new_line(self, tok_type, line, line_num, junk): |
| 194 """a new line has been encountered, process it if necessary""" |
| 195 if not tok_type in junk: |
| 196 self._lines[line_num] = line.split('\n')[0] |
| 197 self.check_lines(line, line_num) |
| 198 |
| 199 def process_tokens(self, tokens): |
| 200 """process tokens and search for : |
| 201 |
| 202 _ non strict indentation (i.e. not always using the <indent> parameter
as |
| 203 indent unit) |
| 204 _ too long lines (i.e. longer than <max_chars>) |
| 205 _ optionally bad construct (if given, bad_construct must be a compiled |
| 206 regular expression). |
| 207 """ |
| 208 indent = tokenize.INDENT |
| 209 dedent = tokenize.DEDENT |
| 210 newline = tokenize.NEWLINE |
| 211 junk = (tokenize.COMMENT, tokenize.NL) |
| 212 indents = [0] |
| 213 check_equal = 0 |
| 214 line_num = 0 |
| 215 previous = None |
| 216 self._lines = {} |
| 217 self._visited_lines = {} |
| 218 for (tok_type, token, start, _, line) in tokens: |
| 219 if start[0] != line_num: |
| 220 if previous is not None and previous[0] == tokenize.OP and previ
ous[1] == ';': |
| 221 self.add_message('W0301', line=previous[2]) |
| 222 previous = None |
| 223 line_num = start[0] |
| 224 self.new_line(tok_type, line, line_num, junk) |
| 225 if tok_type not in (indent, dedent, newline) + junk: |
| 226 previous = tok_type, token, start[0] |
| 227 |
| 228 if tok_type == tokenize.OP: |
| 229 if token == '<>': |
| 230 self.add_message('W0331', line=line_num) |
| 231 elif tok_type == tokenize.NUMBER: |
| 232 if token.endswith('l'): |
| 233 self.add_message('W0332', line=line_num) |
| 234 |
| 235 elif tok_type == newline: |
| 236 # a program statement, or ENDMARKER, will eventually follow, |
| 237 # after some (possibly empty) run of tokens of the form |
| 238 # (NL | COMMENT)* (INDENT | DEDENT+)? |
| 239 # If an INDENT appears, setting check_equal is wrong, and will |
| 240 # be undone when we see the INDENT. |
| 241 check_equal = 1 |
| 242 |
| 243 elif tok_type == indent: |
| 244 check_equal = 0 |
| 245 self.check_indent_level(token, indents[-1]+1, line_num) |
| 246 indents.append(indents[-1]+1) |
| 247 |
| 248 elif tok_type == dedent: |
| 249 # there's nothing we need to check here! what's important is |
| 250 # that when the run of DEDENTs ends, the indentation of the |
| 251 # program statement (or ENDMARKER) that triggered the run is |
| 252 # equal to what's left at the top of the indents stack |
| 253 check_equal = 1 |
| 254 if len(indents) > 1: |
| 255 del indents[-1] |
| 256 |
| 257 elif check_equal and tok_type not in junk: |
| 258 # this is the first "real token" following a NEWLINE, so it |
| 259 # must be the first token of the next program statement, or an |
| 260 # ENDMARKER; the "line" argument exposes the leading whitespace |
| 261 # for this statement; in the case of ENDMARKER, line is an empty |
| 262 # string, so will properly match the empty string with which the |
| 263 # "indents" stack was seeded |
| 264 check_equal = 0 |
| 265 self.check_indent_level(line, indents[-1], line_num) |
| 266 |
| 267 line_num -= 1 # to be ok with "wc -l" |
| 268 if line_num > self.config.max_module_lines: |
| 269 self.add_message('C0302', args=line_num, line=1) |
| 270 |
| 271 @check_messages('C0321' ,'C03232', 'C0323', 'C0324') |
| 272 def visit_default(self, node): |
| 273 """check the node line number and check it if not yet done""" |
| 274 if not node.is_statement: |
| 275 return |
| 276 if not node.root().pure_python: |
| 277 return # XXX block visit of child nodes |
| 278 prev_sibl = node.previous_sibling() |
| 279 if prev_sibl is not None: |
| 280 prev_line = prev_sibl.fromlineno |
| 281 else: |
| 282 prev_line = node.parent.statement().fromlineno |
| 283 line = node.fromlineno |
| 284 assert line, node |
| 285 if prev_line == line and self._visited_lines.get(line) != 2: |
| 286 # py2.5 try: except: finally: |
| 287 if not (isinstance(node, nodes.TryExcept) |
| 288 and isinstance(node.parent, nodes.TryFinally) |
| 289 and node.fromlineno == node.parent.fromlineno): |
| 290 self.add_message('C0321', node=node) |
| 291 self._visited_lines[line] = 2 |
| 292 return |
| 293 if line in self._visited_lines: |
| 294 return |
| 295 try: |
| 296 tolineno = node.blockstart_tolineno |
| 297 except AttributeError: |
| 298 tolineno = node.tolineno |
| 299 assert tolineno, node |
| 300 lines = [] |
| 301 for line in xrange(line, tolineno + 1): |
| 302 self._visited_lines[line] = 1 |
| 303 try: |
| 304 lines.append(self._lines[line].rstrip()) |
| 305 except KeyError: |
| 306 lines.append('') |
| 307 try: |
| 308 msg_def = check_line('\n'.join(lines)) |
| 309 if msg_def: |
| 310 self.add_message(msg_def[0], node=node, args=msg_def[1]) |
| 311 except KeyError: |
| 312 # FIXME: internal error ! |
| 313 pass |
| 314 |
| 315 @check_messages('W0333') |
| 316 def visit_backquote(self, node): |
| 317 self.add_message('W0333', node=node) |
| 318 |
| 319 def check_lines(self, lines, i): |
| 320 """check lines have less than a maximum number of characters |
| 321 """ |
| 322 max_chars = self.config.max_line_length |
| 323 for line in lines.splitlines(): |
| 324 if len(line) > max_chars: |
| 325 self.add_message('C0301', line=i, args=(len(line), max_chars)) |
| 326 i += 1 |
| 327 |
| 328 def check_indent_level(self, string, expected, line_num): |
| 329 """return the indent level of the string |
| 330 """ |
| 331 indent = self.config.indent_string |
| 332 if indent == '\\t': # \t is not interpreted in the configuration file |
| 333 indent = '\t' |
| 334 level = 0 |
| 335 unit_size = len(indent) |
| 336 while string[:unit_size] == indent: |
| 337 string = string[unit_size:] |
| 338 level += 1 |
| 339 suppl = '' |
| 340 while string and string[0] in ' \t': |
| 341 if string[0] != indent[0]: |
| 342 if string[0] == '\t': |
| 343 args = ('tab', 'space') |
| 344 else: |
| 345 args = ('space', 'tab') |
| 346 self.add_message('W0312', args=args, line=line_num) |
| 347 return level |
| 348 suppl += string[0] |
| 349 string = string [1:] |
| 350 if level != expected or suppl: |
| 351 i_type = 'spaces' |
| 352 if indent[0] == '\t': |
| 353 i_type = 'tabs' |
| 354 self.add_message('W0311', line=line_num, |
| 355 args=(level * unit_size + len(suppl), i_type, |
| 356 expected * unit_size)) |
| 357 |
| 358 |
| 359 def register(linter): |
| 360 """required method to auto register this checker """ |
| 361 linter.register_checker(FormatChecker(linter)) |
OLD | NEW |