PageRenderTime 39ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/pylint/checkers/format.py

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