UtilitiesPython2/pep8.py

Tue, 02 Oct 2012 19:53:31 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 02 Oct 2012 19:53:31 +0200
changeset 2087
795992a5c561
parent 1509
c0b5e693b0eb
child 2302
f29e9405c851
permissions
-rw-r--r--

Made the sixth set of Qt5 compatibility changes.

842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 # pep8.py - Check Python source code formatting, according to PEP 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5 # Copyright (C) 2006 Johann C. Rocholl <johann@rocholl.net>
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 # Permission is hereby granted, free of charge, to any person
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 # obtaining a copy of this software and associated documentation files
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 # (the "Software"), to deal in the Software without restriction,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 # including without limitation the rights to use, copy, modify, merge,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # publish, distribute, sublicense, and/or sell copies of the Software,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 # and to permit persons to whom the Software is furnished to do so,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 # subject to the following conditions:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 # The above copyright notice and this permission notice shall be
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 # included in all copies or substantial portions of the Software.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 # SOFTWARE.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 Check Python source code formatting, according to PEP 8:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 http://www.python.org/dev/peps/pep-0008/
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 For usage and a list of options, try this:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 $ python pep8.py -h
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 This program and its regression test suite live here:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 http://github.com/jcrocholl/pep8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 Groups of errors and warnings:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 E errors
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 W warnings
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 100 indentation
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 200 whitespace
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 300 blank lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 400 imports
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 500 line length
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 600 deprecation
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 700 statements
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 You can add checks to this program by writing plugins. Each plugin is
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 a simple function that is called for each line of source code, either
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 physical or logical.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 Physical line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 - Raw line of text from the input file.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 Logical line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 - Multi-line statements converted to a single line.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 - Stripped left and right.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 - Contents of strings replaced with 'xxx' of same length.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 - Comments removed.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 The check function requests physical or logical lines by the name of
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 the first argument:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 def maximum_line_length(physical_line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 def extraneous_whitespace(logical_line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 def blank_lines(logical_line, blank_lines, indent_level, line_number)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 The last example above demonstrates how check plugins can request
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 additional information with extra arguments. All attributes of the
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 Checker object are available. Some examples:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 lines: a list of the raw lines from the input file
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 tokens: the tokens that contribute to this logical line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 line_number: line number in the input file
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 blank_lines: blank lines before this one
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 indent_char: first indentation character in this file (' ' or '\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 indent_level: indentation (with tabs expanded to multiples of 8)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 previous_indent_level: indentation on previous line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 previous_logical: previous logical line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 The docstring of each check function shall be the relevant part of
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 text from PEP 8. It is printed if the user enables --show-pep8.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 Several docstrings contain examples directly from the PEP 8 document.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 Okay: spam(ham[1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 E201: spam( ham[1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 These examples are verified automatically when pep8.py is run with the
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 --doctest option. You can add examples for your own check functions.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 The format is simple: "Okay" or error/warning code followed by colon
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 and space, the rest of the line is example source code. If you put 'r'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 before the docstring, you can use \n for newline, \t for tab and \s
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 for space.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 # This is a modified version to make the original tabnanny better suitable
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 # for being called from within the eric5 IDE. The modifications are as
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 # follows:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 # - made messages translatable via Qt
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 #
1509
c0b5e693b0eb Updated copyright for 2012.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1145
diff changeset
104 # Copyright (c) 2011 - 2012 Detlev Offenbach <detlev@die-offenbachs.de>
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 __version__ = '0.6.1'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 import os
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 import sys
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 import re
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 import time
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 import inspect
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 import keyword
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 import tokenize
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 from optparse import OptionParser
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 from fnmatch import fnmatch
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 try:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 frozenset
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 except NameError:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 from sets import ImmutableSet as frozenset
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 DEFAULT_IGNORE = 'E24'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 MAX_LINE_LENGTH = 79
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 INDENT_REGEX = re.compile(r'([ \t]*)')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 RAISE_COMMA_REGEX = re.compile(r'raise\s+\w+\s*(,)')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 SELFTEST_REGEX = re.compile(r'(Okay|[EW]\d{3}):\s(.*)')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 ERRORCODE_REGEX = re.compile(r'[EW]\d{3}')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 DOCSTRING_REGEX = re.compile(r'u?r?["\']')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 WHITESPACE_AROUND_OPERATOR_REGEX = \
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 re.compile('([^\w\s]*)\s*(\t| )\s*([^\w\s]*)')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 WHITESPACE_AROUND_NAMED_PARAMETER_REGEX = \
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 re.compile(r'[()]|\s=[^=]|[^=!<>]=\s')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 WHITESPACE = ' \t'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 BINARY_OPERATORS = frozenset(['**=', '*=', '+=', '-=', '!=', '<>',
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 '%=', '^=', '&=', '|=', '==', '/=', '//=', '<=', '>=', '<<=', '>>=',
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 '%', '^', '&', '|', '=', '/', '//', '<', '>', '<<'])
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-'])
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 OPERATORS = BINARY_OPERATORS | UNARY_OPERATORS
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 SKIP_TOKENS = frozenset([tokenize.COMMENT, tokenize.NL, tokenize.INDENT,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 tokenize.DEDENT, tokenize.NEWLINE])
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 E225NOT_KEYWORDS = (frozenset(keyword.kwlist + ['print']) -
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 frozenset(['False', 'None', 'True']))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 BENCHMARK_KEYS = ('directories', 'files', 'logical lines', 'physical lines')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 options = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 args = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 # Plugins (check functions) for physical lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 def tabs_or_spaces(physical_line, indent_char):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 Never mix tabs and spaces.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 The most popular way of indenting Python is with spaces only. The
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 second-most popular way is with tabs only. Code indented with a mixture
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 of tabs and spaces should be converted to using spaces exclusively. When
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 invoking the Python command line interpreter with the -t option, it issues
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 warnings about code that illegally mixes tabs and spaces. When using -tt
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 these warnings become errors. These options are highly recommended!
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 Okay: if a == 0:\n a = 1\n b = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 E101: if a == 0:\n a = 1\n\tb = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 indent = INDENT_REGEX.match(physical_line).group(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 for offset, char in enumerate(indent):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 if char != indent_char:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 return offset, "E101"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 def tabs_obsolete(physical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 For new projects, spaces-only are strongly recommended over tabs. Most
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 editors have features that make this easy to do.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 Okay: if True:\n return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 W191: if True:\n\treturn
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 indent = INDENT_REGEX.match(physical_line).group(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 if indent.count('\t'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 return indent.index('\t'), "W191"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 def trailing_whitespace(physical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 JCR: Trailing whitespace is superfluous.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 FBM: Except when it occurs as part of a blank line (i.e. the line is
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 nothing but whitespace). According to Python docs[1] a line with only
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 whitespace is considered a blank line, and is to be ignored. However,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 matching a blank line to its indentation level avoids mistakenly
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 terminating a multi-line statement (e.g. class declaration) when
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 pasting code into the standard Python interpreter.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 [1] http://docs.python.org/reference/lexical_analysis.html#blank-lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 The warning returned varies on whether the line itself is blank, for easier
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 filtering for those who want to indent their blank lines.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 Okay: spam(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 W291: spam(1)\s
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 W293: class Foo(object):\n \n bang = 12
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 physical_line = physical_line.rstrip('\n') # chr(10), newline
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 physical_line = physical_line.rstrip('\r') # chr(13), carriage return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 physical_line = physical_line.rstrip('\x0c') # chr(12), form feed, ^L
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 stripped = physical_line.rstrip()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 if physical_line != stripped:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 if stripped:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 return len(stripped), "W291"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 return 0, "W293"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 def trailing_blank_lines(physical_line, lines, line_number):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 JCR: Trailing blank lines are superfluous.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 Okay: spam(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 W391: spam(1)\n
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 if physical_line.strip() == '' and line_number == len(lines):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 return 0, "W391"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 def missing_newline(physical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 JCR: The last line should have a newline.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 if physical_line.rstrip() == physical_line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 return len(physical_line), "W292"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 def maximum_line_length(physical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 Limit all lines to a maximum of 79 characters.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 There are still many devices around that are limited to 80 character
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 lines; plus, limiting windows to 80 characters makes it possible to have
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 several windows side-by-side. The default wrapping on such devices looks
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 ugly. Therefore, please limit all lines to a maximum of 79 characters.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 For flowing long blocks of text (docstrings or comments), limiting the
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 length to 72 characters is recommended.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 line = physical_line.rstrip()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 length = len(line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 if length > MAX_LINE_LENGTH:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 try:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 # The line could contain multi-byte characters
1145
2a16484e65c2 Fix for the PEP-8 checker to handle unicode better.
detlev@die-offenbachs.de
parents: 945
diff changeset
259 if hasattr(line, 'decode'): # Python 2 only
2a16484e65c2 Fix for the PEP-8 checker to handle unicode better.
detlev@die-offenbachs.de
parents: 945
diff changeset
260 length = len(line.decode('utf-8'))
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 except UnicodeDecodeError:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 if length > MAX_LINE_LENGTH:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 return MAX_LINE_LENGTH, "E501", length
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 # Plugins (check functions) for logical lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 def blank_lines(logical_line, blank_lines, indent_level, line_number,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 previous_logical, previous_indent_level,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 blank_lines_before_comment):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 Separate top-level function and class definitions with two blank lines.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 Method definitions inside a class are separated by a single blank line.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 Extra blank lines may be used (sparingly) to separate groups of related
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 functions. Blank lines may be omitted between a bunch of related
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 one-liners (e.g. a set of dummy implementations).
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 Use blank lines in functions, sparingly, to indicate logical sections.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 Okay: def a():\n pass\n\n\ndef b():\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 Okay: def a():\n pass\n\n\n# Foo\n# Bar\n\ndef b():\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 E301: class Foo:\n b = 0\n def bar():\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 E302: def a():\n pass\n\ndef b(n):\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 E303: def a():\n pass\n\n\n\ndef b(n):\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 E303: def a():\n\n\n\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 E304: @decorator\n\ndef a():\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 if line_number == 1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 return # Don't expect blank lines before the first line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 max_blank_lines = max(blank_lines, blank_lines_before_comment)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 if previous_logical.startswith('@'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 if max_blank_lines:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 return 0, "E304"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 elif max_blank_lines > 2 or (indent_level and max_blank_lines == 2):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 return 0, "E303", max_blank_lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 elif (logical_line.startswith('def ') or
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 logical_line.startswith('class ') or
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 logical_line.startswith('@')):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 if indent_level:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 if not (max_blank_lines or previous_indent_level < indent_level or
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 DOCSTRING_REGEX.match(previous_logical)):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 return 0, "E301"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 elif max_blank_lines != 2:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 return 0, "E302", max_blank_lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 def extraneous_whitespace(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 Avoid extraneous whitespace in the following situations:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 - Immediately inside parentheses, brackets or braces.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 - Immediately before a comma, semicolon, or colon.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322 Okay: spam(ham[1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 E201: spam( ham[1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 E201: spam(ham[ 1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 E201: spam(ham[1], { eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 E202: spam(ham[1], {eggs: 2} )
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 E202: spam(ham[1 ], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 E202: spam(ham[1], {eggs: 2 })
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 E203: if x == 4: print x, y; x, y = y , x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 E203: if x == 4: print x, y ; x, y = y, x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 E203: if x == 4 : print x, y; x, y = y, x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 for match in EXTRANEOUS_WHITESPACE_REGEX.finditer(line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 text = match.group()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 char = text.strip()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 found = match.start()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 if text == char + ' ' and char in '([{':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 return found + 1, "E201", char
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 if text == ' ' + char and line[found - 1] != ',':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 if char in '}])':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 return found, "E202", char
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 if char in ',;:':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 return found, "E203", char
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 def missing_whitespace(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 JCR: Each comma, semicolon or colon should be followed by whitespace.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 Okay: [a, b]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 Okay: (3,)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 Okay: a[1:4]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 Okay: a[:4]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 Okay: a[1:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 Okay: a[1:4:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 E231: ['a','b']
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 E231: foo(bar,baz)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 for index in range(len(line) - 1):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 char = line[index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 if char in ',;:' and line[index + 1] not in WHITESPACE:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 before = line[:index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 if char == ':' and before.count('[') > before.count(']'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 continue # Slice syntax, no space required
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 if char == ',' and line[index + 1] == ')':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 continue # Allow tuple with only one element: (3,)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 return index, "E231", char
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 def indentation(logical_line, previous_logical, indent_char,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 indent_level, previous_indent_level):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 Use 4 spaces per indentation level.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 For really old code that you don't want to mess up, you can continue to
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 use 8-space tabs.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 Okay: a = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 Okay: if a == 0:\n a = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 E111: a = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 Okay: for item in items:\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 E112: for item in items:\npass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 Okay: a = 1\nb = 2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 E113: a = 1\n b = 2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 if indent_char == ' ' and indent_level % 4:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 return 0, "E111"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 indent_expect = previous_logical.endswith(':')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 if indent_expect and indent_level <= previous_indent_level:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 return 0, "E112"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 if indent_level > previous_indent_level and not indent_expect:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 return 0, "E113"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 def whitespace_before_parameters(logical_line, tokens):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 Avoid extraneous whitespace in the following situations:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 - Immediately before the open parenthesis that starts the argument
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 list of a function call.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 - Immediately before the open parenthesis that starts an indexing or
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 slicing.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 Okay: spam(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 E211: spam (1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 Okay: dict['key'] = list[index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 E211: dict ['key'] = list[index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 E211: dict['key'] = list [index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 prev_type = tokens[0][0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 prev_text = tokens[0][1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 prev_end = tokens[0][3]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 for index in range(1, len(tokens)):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 token_type, text, start, end, line = tokens[index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 if (token_type == tokenize.OP and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 text in '([' and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 start != prev_end and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 (prev_type == tokenize.NAME or prev_text in '}])') and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 # Syntax "class A (B):" is allowed, but avoid it
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 (index < 2 or tokens[index - 2][1] != 'class') and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 # Allow "return (a.foo for a in range(5))"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 (not keyword.iskeyword(prev_text))):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 return prev_end, "E211", text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 prev_type = token_type
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 prev_text = text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 prev_end = end
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 def whitespace_around_operator(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 Avoid extraneous whitespace in the following situations:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 - More than one space around an assignment (or other) operator to
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 align it with another.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 Okay: a = 12 + 3
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 E221: a = 4 + 5
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 E222: a = 4 + 5
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 E223: a = 4\t+ 5
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 E224: a = 4 +\t5
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 for match in WHITESPACE_AROUND_OPERATOR_REGEX.finditer(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 before, whitespace, after = match.groups()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 tab = whitespace == '\t'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 offset = match.start(2)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 if before in OPERATORS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 return offset, (tab and "E224" or "E222")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 elif after in OPERATORS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 return offset, (tab and "E223" or "E221")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 def missing_whitespace_around_operator(logical_line, tokens):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 - Always surround these binary operators with a single space on
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 either side: assignment (=), augmented assignment (+=, -= etc.),
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not),
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 Booleans (and, or, not).
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 - Use spaces around arithmetic operators.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 Okay: i = i + 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 Okay: submitted += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 Okay: x = x * 2 - 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 Okay: hypot2 = x * x + y * y
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 Okay: c = (a + b) * (a - b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 Okay: foo(bar, key='word', *args, **kwargs)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 Okay: baz(**kwargs)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 Okay: negative = -1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 Okay: spam(-1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 Okay: alpha[:-i]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 Okay: if not -5 < x < +5:\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 Okay: lambda *args, **kw: (args, kw)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 E225: i=i+1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 E225: submitted +=1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 E225: x = x*2 - 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 E225: hypot2 = x*x + y*y
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 E225: c = (a+b) * (a-b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 E225: c = alpha -4
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 E225: z = x **y
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 parens = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 need_space = False
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 prev_type = tokenize.OP
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 prev_text = prev_end = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 for token_type, text, start, end, line in tokens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 if token_type in (tokenize.NL, tokenize.NEWLINE, tokenize.ERRORTOKEN):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 # ERRORTOKEN is triggered by backticks in Python 3000
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 if text in ('(', 'lambda'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 parens += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 elif text == ')':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 parens -= 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 if need_space:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 if start != prev_end:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 need_space = False
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 elif text == '>' and prev_text == '<':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 # Tolerate the "<>" operator, even if running Python 3
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 return prev_end, "E225"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 elif token_type == tokenize.OP and prev_end is not None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 if text == '=' and parens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 # Allow keyword args or defaults: foo(bar=None).
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 elif text in BINARY_OPERATORS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 need_space = True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 elif text in UNARY_OPERATORS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 # Allow unary operators: -123, -x, +1.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 # Allow argument unpacking: foo(*args, **kwargs).
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 if prev_type == tokenize.OP:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 if prev_text in '}])':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 need_space = True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 elif prev_type == tokenize.NAME:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 if prev_text not in E225NOT_KEYWORDS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 need_space = True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 need_space = True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 if need_space and start == prev_end:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 return prev_end, "E225"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 prev_type = token_type
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 prev_text = text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 prev_end = end
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 def whitespace_around_comma(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 Avoid extraneous whitespace in the following situations:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 - More than one space around an assignment (or other) operator to
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 align it with another.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 JCR: This should also be applied around comma etc.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 Note: these checks are disabled by default
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 Okay: a = (1, 2)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 E241: a = (1, 2)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 E242: a = (1,\t2)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 for separator in ',;:':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 found = line.find(separator + ' ')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 if found > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 return found + 1, "E241", separator
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
552 found = line.find(separator + '\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 if found > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 return found + 1, "E242", separator
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 def whitespace_around_named_parameter_equals(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 Don't use spaces around the '=' sign when used to indicate a
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 keyword argument or a default parameter value.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 Okay: def complex(real, imag=0.0):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 Okay: return magic(r=real, i=imag)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 Okay: boolean(a == b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 Okay: boolean(a != b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 Okay: boolean(a <= b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 Okay: boolean(a >= b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 E251: def complex(real, imag = 0.0):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 E251: return magic(r = real, i = imag)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 parens = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
573 for match in WHITESPACE_AROUND_NAMED_PARAMETER_REGEX.finditer(
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
574 logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 text = match.group()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
576 if parens and len(text) == 3:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577 issue = "E251"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 return match.start(), issue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 if text == '(':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 parens += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 elif text == ')':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582 parens -= 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 def whitespace_before_inline_comment(logical_line, tokens):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 Separate inline comments by at least two spaces.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 An inline comment is a comment on the same line as a statement. Inline
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 comments should be separated by at least two spaces from the statement.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 They should start with a # and a single space.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593 Okay: x = x + 1 # Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 Okay: x = x + 1 # Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 E261: x = x + 1 # Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 E262: x = x + 1 #Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597 E262: x = x + 1 # Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 prev_end = (0, 0)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 for token_type, text, start, end, line in tokens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 if token_type == tokenize.NL:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 if token_type == tokenize.COMMENT:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 if not line[:start[1]].strip():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 if prev_end[0] == start[0] and start[1] < prev_end[1] + 2:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 return (prev_end, "E261")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 if (len(text) > 1 and text.startswith('# ')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 or not text.startswith('# ')):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 return start, "E262"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 prev_end = end
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
614
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615 def imports_on_separate_lines(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 Imports should usually be on separate lines.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619 Okay: import os\nimport sys
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 E401: import sys, os
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 Okay: from subprocess import Popen, PIPE
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 Okay: from myclas import MyClass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 Okay: from foo.bar.yourclass import YourClass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 Okay: import myclass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 Okay: import foo.bar.yourclass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
629 if line.startswith('import '):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 found = line.find(',')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 if found > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 return found, "E401"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 def compound_statements(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 Compound statements (multiple statements on the same line) are
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 generally discouraged.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640 While sometimes it's okay to put an if/for/while with a small body
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 on the same line, never do this for multi-clause statements. Also
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 avoid folding such long lines!
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 Okay: if foo == 'blah':\n do_blah_thing()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645 Okay: do_one()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 Okay: do_two()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 Okay: do_three()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649 E701: if foo == 'blah': do_blah_thing()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 E701: for x in lst: total += x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 E701: while t < 10: t = delay()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 E701: if foo == 'blah': do_blah_thing()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 E701: else: do_non_blah_thing()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
654 E701: try: something()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655 E701: finally: cleanup()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 E701: if foo == 'blah': one(); two(); three()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658 E702: do_one(); do_two(); do_three()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 found = line.find(':')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 if -1 < found < len(line) - 1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663 before = line[:found]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664 if (before.count('{') <= before.count('}') and # {'a': 1} (dict)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 before.count('[') <= before.count(']') and # [1:2] (slice)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 not re.search(r'\blambda\b', before)): # lambda x: x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 return found, "E701"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 found = line.find(';')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 if -1 < found:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 return found, "E702"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 def python_3000_has_key(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 The {}.has_key() method will be removed in the future version of
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 Python. Use the 'in' operation instead, like:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 d = {"a": 1, "b": 2}
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678 if "b" in d:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679 print d["b"]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 pos = logical_line.find('.has_key(')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 if pos > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 return pos, "W601"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686 def python_3000_raise_comma(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 When raising an exception, use "raise ValueError('message')"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689 instead of the older form "raise ValueError, 'message'".
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691 The paren-using form is preferred because when the exception arguments
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
692 are long or include string formatting, you don't need to use line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 continuation characters thanks to the containing parentheses. The older
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
694 form will be removed in Python 3000.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
695 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
696 match = RAISE_COMMA_REGEX.match(logical_line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
697 if match:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 return match.start(1), "W602"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
701 def python_3000_not_equal(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
702 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 != can also be written <>, but this is an obsolete usage kept for
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704 backwards compatibility only. New code should always use !=.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705 The older syntax is removed in Python 3000.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 pos = logical_line.find('<>')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708 if pos > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709 return pos, "W603"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712 def python_3000_backticks(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
714 Backticks are removed in Python 3000.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
715 Use repr() instead.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 pos = logical_line.find('`')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 if pos > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 return pos, "W604"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 # Helper functions
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 if '' == ''.encode():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 # Python 2: implicit encoding.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 def readlines(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730 return open(filename).readlines()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 # Python 3: decode to latin-1.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 # This function is lazy, it does not read the encoding declaration.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 # XXX: use tokenize.detect_encoding()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 def readlines(filename): # __IGNORE_WARNING__
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 return open(filename, encoding='latin-1').readlines()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739 def expand_indent(line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
740 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741 Return the amount of indentation.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 Tabs are expanded to the next multiple of 8.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744 >>> expand_indent(' ')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 4
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
746 >>> expand_indent('\\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
747 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 >>> expand_indent(' \\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 >>> expand_indent(' \\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 >>> expand_indent(' \\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 16
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 result = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756 for char in line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757 if char == '\t':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758 result = result // 8 * 8 + 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 elif char == ' ':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
760 result += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
761 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762 break
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763 return result
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 def mute_string(text):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768 Replace contents with 'xxx' to prevent syntax matching.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 >>> mute_string('"abc"')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771 '"xxx"'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 >>> mute_string("'''abc'''")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 "'''xxx'''"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 >>> mute_string("r'abc'")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 "r'xxx'"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 start = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 end = len(text) - 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 # String modifiers (e.g. u or r)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 if text.endswith('"'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 start += text.index('"')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 elif text.endswith("'"):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783 start += text.index("'")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784 # Triple quotes
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 if text.endswith('"""') or text.endswith("'''"):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 start += 2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 end -= 2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 return text[:start] + 'x' * (end - start) + text[end:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791 def message(text):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
792 """Print a message."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 # print >> sys.stderr, options.prog + ': ' + text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 # print >> sys.stderr, text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 print(text)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 # Framework to run all checks
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803 def find_checks(argument_name):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
804 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 Find all globally visible functions where the first argument name
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806 starts with argument_name.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 checks = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
809 for name, function in globals().items():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810 if not inspect.isfunction(function):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
812 args = inspect.getargspec(function)[0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813 if args and args[0].startswith(argument_name):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
814 codes = ERRORCODE_REGEX.findall(inspect.getdoc(function) or '')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
815 for code in codes or ['']:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
816 if not code or not ignore_code(code):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 checks.append((name, function, args))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
818 break
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
819 checks.sort()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 return checks
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823 class Checker(object):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
824 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
825 Load a Python source file, tokenize it, check coding style.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
827
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
828 def __init__(self, filename, lines=None):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
829 self.filename = filename
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
830 if filename is None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
831 self.filename = 'stdin'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832 self.lines = lines or []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833 elif lines is None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
834 self.lines = readlines(filename)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
835 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
836 self.lines = lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
837 options.counters['physical lines'] += len(self.lines)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
838
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
839 def readline(self):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
840 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
841 Get the next line from the input buffer.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
842 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
843 self.line_number += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
844 if self.line_number > len(self.lines):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
845 return ''
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
846 return self.lines[self.line_number - 1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
847
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
848 def readline_check_physical(self):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
849 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
850 Check and return the next physical line. This method can be
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
851 used to feed tokenize.generate_tokens.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
852 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
853 line = self.readline()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
854 if line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
855 self.check_physical(line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
856 return line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
857
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
858 def run_check(self, check, argument_names):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
859 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
860 Run a check plugin.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
861 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
862 arguments = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 for name in argument_names:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
864 arguments.append(getattr(self, name))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
865 return check(*arguments)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
866
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
867 def check_physical(self, line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
868 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
869 Run all physical checks on a raw input line.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
870 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
871 self.physical_line = line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
872 if self.indent_char is None and len(line) and line[0] in ' \t':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
873 self.indent_char = line[0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
874 for name, check, argument_names in options.physical_checks:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
875 result = self.run_check(check, argument_names)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
876 if result is not None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
877 offset, code = result[:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
878 args = result[2:]
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 842
diff changeset
879 self.report_error_args(self.line_number, offset, code, check,
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
880 *args)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
881
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
882 def build_tokens_line(self):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
883 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
884 Build a logical line from tokens.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
885 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
886 self.mapping = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
887 logical = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
888 length = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
889 previous = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
890 for token in self.tokens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
891 token_type, text = token[0:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
892 if token_type in SKIP_TOKENS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
893 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
894 if token_type == tokenize.STRING:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
895 text = mute_string(text)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
896 if previous:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
897 end_line, end = previous[3]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
898 start_line, start = token[2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
899 if end_line != start_line: # different row
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
900 prev_text = self.lines[end_line - 1][end - 1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
901 if prev_text == ',' or (prev_text not in '{[('
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
902 and text not in '}])'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
903 logical.append(' ')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
904 length += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
905 elif end != start: # different column
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
906 fill = self.lines[end_line - 1][end:start]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
907 logical.append(fill)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
908 length += len(fill)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
909 self.mapping.append((length, token))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
910 logical.append(text)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
911 length += len(text)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
912 previous = token
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
913 self.logical_line = ''.join(logical)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
914 assert self.logical_line.lstrip() == self.logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
915 assert self.logical_line.rstrip() == self.logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
916
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
917 def check_logical(self):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
918 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
919 Build a line from tokens and run all logical checks on it.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
920 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
921 options.counters['logical lines'] += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
922 self.build_tokens_line()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
923 first_line = self.lines[self.mapping[0][1][2][0] - 1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
924 indent = first_line[:self.mapping[0][1][2][1]]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
925 self.previous_indent_level = self.indent_level
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
926 self.indent_level = expand_indent(indent)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
927 if options.verbose >= 2:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
928 print(self.logical_line[:80].rstrip())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
929 for name, check, argument_names in options.logical_checks:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
930 if options.verbose >= 4:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
931 print(' ' + name)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
932 result = self.run_check(check, argument_names)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
933 if result is not None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
934 offset, code = result[:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
935 args = result[2:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936 if isinstance(offset, tuple):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
937 original_number, original_offset = offset
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
938 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
939 for token_offset, token in self.mapping:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
940 if offset >= token_offset:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941 original_number = token[2][0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
942 original_offset = (token[2][1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
943 + offset - token_offset)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
944 self.report_error_args(original_number, original_offset,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
945 code, check, *args)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
946 self.previous_logical = self.logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
947
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
948 def check_all(self, expected=None, line_offset=0):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 Run all checks on the input file.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
951 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
952 self.expected = expected or ()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
953 self.line_offset = line_offset
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
954 self.line_number = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
955 self.file_errors = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
956 self.indent_char = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
957 self.indent_level = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
958 self.previous_logical = ''
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
959 self.blank_lines = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
960 self.blank_lines_before_comment = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
961 self.tokens = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
962 parens = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
963 for token in tokenize.generate_tokens(self.readline_check_physical):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
964 if options.verbose >= 3:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
965 if token[2][0] == token[3][0]:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
966 pos = '[%s:%s]' % (token[2][1] or '', token[3][1])
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
967 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
968 pos = 'l.%s' % token[3][0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
969 print('l.%s\t%s\t%s\t%r' %
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
970 (token[2][0], pos, tokenize.tok_name[token[0]], token[1]))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
971 self.tokens.append(token)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
972 token_type, text = token[0:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
973 if token_type == tokenize.OP and text in '([{':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
974 parens += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
975 if token_type == tokenize.OP and text in '}])':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
976 parens -= 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
977 if token_type == tokenize.NEWLINE and not parens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
978 self.check_logical()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
979 self.blank_lines = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
980 self.blank_lines_before_comment = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
981 self.tokens = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
982 if token_type == tokenize.NL and not parens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
983 if len(self.tokens) <= 1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
984 # The physical line contains only this token.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
985 self.blank_lines += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
986 self.tokens = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
987 if token_type == tokenize.COMMENT:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
988 source_line = token[4]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
989 token_start = token[2][1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
990 if source_line[:token_start].strip() == '':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
991 self.blank_lines_before_comment = max(self.blank_lines,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
992 self.blank_lines_before_comment)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
993 self.blank_lines = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
994 if text.endswith('\n') and not parens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
995 # The comment also ends a physical line. This works around
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
996 # Python < 2.6 behaviour, which does not generate NL after
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
997 # a comment which is on a line by itself.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
998 self.tokens = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
999 return self.file_errors
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1000
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1001 def report_error(self, line_number, offset, text, check):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1002 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1003 Report an error, according to options.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1004 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1005 code = text[:4]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1006 if ignore_code(code):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1007 return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1008 if options.quiet == 1 and not self.file_errors:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1009 message(self.filename)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1010 if code in options.counters:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1011 options.counters[code] += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1012 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1013 options.counters[code] = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1014 options.messages[code] = text[5:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1015 if options.quiet or code in self.expected:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1016 # Don't care about expected errors or warnings
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1017 return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1018 self.file_errors += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1019 if options.counters[code] == 1 or options.repeat:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1020 message("%s:%s:%d: %s" %
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1021 (self.filename, self.line_offset + line_number,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1022 offset + 1, text))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1023 if options.show_source:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1024 line = self.lines[line_number - 1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1025 message(line.rstrip())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1026 message(' ' * offset + '^')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1027 if options.show_pep8:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1028 message(check.__doc__.lstrip('\n').rstrip())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1029
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1030
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1031 def input_file(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1032 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1033 Run all checks on a Python source file.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1034 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1035 if options.verbose:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1036 message('checking ' + filename)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1037 Checker(filename).check_all()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1038
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1039
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1040 def input_dir(dirname, runner=None):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1041 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1042 Check all Python source files in this directory and all subdirectories.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1043 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1044 dirname = dirname.rstrip('/')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1045 if excluded(dirname):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1046 return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1047 if runner is None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1048 runner = input_file
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1049 for root, dirs, files in os.walk(dirname):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1050 if options.verbose:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1051 message('directory ' + root)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1052 options.counters['directories'] += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1053 dirs.sort()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1054 for subdir in dirs:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1055 if excluded(subdir):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1056 dirs.remove(subdir)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1057 files.sort()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1058 for filename in files:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1059 if filename_match(filename) and not excluded(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1060 options.counters['files'] += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1061 runner(os.path.join(root, filename))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1062
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1063
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1064 def excluded(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1065 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1066 Check if options.exclude contains a pattern that matches filename.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1067 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1068 basename = os.path.basename(filename)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1069 for pattern in options.exclude:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1070 if fnmatch(basename, pattern):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1071 # print basename, 'excluded because it matches', pattern
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1072 return True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1073
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1074
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1075 def filename_match(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1076 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1077 Check if options.filename contains a pattern that matches filename.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1078 If options.filename is unspecified, this always returns True.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1079 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1080 if not options.filename:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1081 return True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1082 for pattern in options.filename:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1083 if fnmatch(filename, pattern):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1084 return True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1085
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1086
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1087 def ignore_code(code):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1088 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1089 Check if options.ignore contains a prefix of the error code.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1090 If options.select contains a prefix of the error code, do not ignore it.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1091 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1092 for select in options.select:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1093 if code.startswith(select):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1094 return False
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1095 for ignore in options.ignore:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1096 if code.startswith(ignore):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1097 return True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1098
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1099
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1100 def reset_counters():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1101 for key in list(options.counters.keys()):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1102 if key not in BENCHMARK_KEYS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1103 del options.counters[key]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1104 options.messages = {}
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1105
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1106
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1107 def get_error_statistics():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1108 """Get error statistics."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1109 return get_statistics("E")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1110
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1111
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1112 def get_warning_statistics():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1113 """Get warning statistics."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1114 return get_statistics("W")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1115
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1116
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1117 def get_statistics(prefix=''):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1118 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1119 Get statistics for message codes that start with the prefix.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1120
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1121 prefix='' matches all errors and warnings
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1122 prefix='E' matches all errors
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1123 prefix='W' matches all warnings
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1124 prefix='E4' matches all errors that have to do with imports
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1125 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1126 stats = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1127 keys = list(options.messages.keys())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1128 keys.sort()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1129 for key in keys:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1130 if key.startswith(prefix):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1131 stats.append('%-7s %s %s' %
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1132 (options.counters[key], key, options.messages[key]))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1133 return stats
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1134
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1135
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1136 def get_count(prefix=''):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1137 """Return the total count of errors and warnings."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1138 keys = list(options.messages.keys())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1139 count = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1140 for key in keys:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1141 if key.startswith(prefix):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1142 count += options.counters[key]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1143 return count
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1144
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1145
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1146 def print_statistics(prefix=''):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1147 """Print overall statistics (number of errors and warnings)."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1148 for line in get_statistics(prefix):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1149 print(line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1150
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1151
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1152 def print_benchmark(elapsed):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1153 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1154 Print benchmark numbers.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1155 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1156 print('%-7.2f %s' % (elapsed, 'seconds elapsed'))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1157 for key in BENCHMARK_KEYS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1158 print('%-7d %s per second (%d total)' % (
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1159 options.counters[key] / elapsed, key,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1160 options.counters[key]))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1161
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1162
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1163 def run_tests(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1164 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1165 Run all the tests from a file.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1166
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1167 A test file can provide many tests. Each test starts with a declaration.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1168 This declaration is a single line starting with '#:'.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1169 It declares codes of expected failures, separated by spaces or 'Okay'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1170 if no failure is expected.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1171 If the file does not contain such declaration, it should pass all tests.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1172 If the declaration is empty, following lines are not checked, until next
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1173 declaration.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1174
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1175 Examples:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1176
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1177 * Only E224 and W701 are expected: #: E224 W701
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1178 * Following example is conform: #: Okay
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1179 * Don't check these lines: #:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1180 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1181 lines = readlines(filename) + ['#:\n']
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1182 line_offset = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1183 codes = ['Okay']
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1184 testcase = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1185 for index, line in enumerate(lines):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1186 if not line.startswith('#:'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1187 if codes:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1188 # Collect the lines of the test case
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1189 testcase.append(line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1190 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1191 if codes and index > 0:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1192 label = '%s:%s:1' % (filename, line_offset + 1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1193 codes = [c for c in codes if c != 'Okay']
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1194 # Run the checker
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1195 errors = Checker(filename, testcase).check_all(codes, line_offset)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1196 # Check if the expected errors were found
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1197 for code in codes:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1198 if not options.counters.get(code):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1199 errors += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1200 message('%s: error %s not found' % (label, code))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1201 if options.verbose and not errors:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1202 message('%s: passed (%s)' % (label, ' '.join(codes)))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1203 # Keep showing errors for multiple tests
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1204 reset_counters()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1205 # output the real line numbers
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1206 line_offset = index
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1207 # configure the expected errors
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1208 codes = line.split()[1:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1209 # empty the test case buffer
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1210 del testcase[:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1211
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1212
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1213 def selftest():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1214 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1215 Test all check functions with test cases in docstrings.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1216 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1217 count_passed = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: