6 """ |
6 """ |
7 Class implementing the PEP 8 checker for Python2. |
7 Class implementing the PEP 8 checker for Python2. |
8 """ |
8 """ |
9 |
9 |
10 import sys |
10 import sys |
11 import optparse |
|
12 import getopt |
11 import getopt |
13 |
12 |
14 from Tools import readEncodedFile, normalizeCode |
13 from Tools import readEncodedFile, normalizeCode |
15 |
14 |
16 import pep8 |
15 import pep8 |
17 |
16 |
18 |
17 |
19 class Pep8Checker(pep8.Checker): |
18 class Pep8Report(pep8.BaseReport): |
20 """ |
19 """ |
21 Class implementing the PEP 8 checker for Python2. |
20 Class implementing a special report to be used with our dialog. |
22 """ |
21 """ |
23 def __init__(self, filename, lines, repeat=False, |
22 def __init__(self, options): |
24 select="", ignore=""): |
|
25 """ |
23 """ |
26 Constructor |
24 Constructor |
27 |
25 |
28 @param filename name of the file to check (string) |
26 @param options options for the report (optparse.Values) |
29 @param lines source of the file (list of strings) |
|
30 @keyparam repeat flag indicating to repeat message categories (boolean) |
|
31 @keyparam select list of message IDs to check for |
|
32 (comma separated string) |
|
33 @keyparam ignore list of message IDs to ignore |
|
34 (comma separated string) |
|
35 """ |
27 """ |
36 pep8.options = optparse.Values() |
28 super(Pep8Report, self).__init__(options) |
37 |
29 |
38 pep8.options.verbose = 0 |
30 self.__repeat = options.repeat |
39 |
31 self.errors = [] |
40 pep8.options.repeat = repeat |
|
41 if select: |
|
42 pep8.options.select = [s.strip() for s in select.split(',') |
|
43 if s.strip()] |
|
44 else: |
|
45 pep8.options.select = [] |
|
46 if ignore: |
|
47 pep8.options.ignore = [i.strip() for i in ignore.split(',') |
|
48 if i.strip()] |
|
49 else: |
|
50 pep8.options.ignore = [] |
|
51 pep8.options.physical_checks = pep8.find_checks('physical_line') |
|
52 pep8.options.logical_checks = pep8.find_checks('logical_line') |
|
53 pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0) |
|
54 pep8.options.messages = {} |
|
55 |
|
56 pep8.Checker.__init__(self, filename, lines) |
|
57 |
|
58 self.messages = [] |
|
59 self.statistics = {} |
|
60 |
32 |
61 def __ignore_code(self, code): |
33 def error_args(self, line_number, offset, code, check, *args): |
62 """ |
|
63 Private method to check, if the message for the given code should |
|
64 be ignored. |
|
65 |
|
66 If codes are selected and the code has a selected prefix and does not |
|
67 have an ignored prefix, it is not ignored. If codes are selected and |
|
68 the code does not have a selected prefix, it is ignored. If no codes |
|
69 are selected, the code is ignored, if it has a prefix, that is |
|
70 contained in the ignored codes. |
|
71 |
|
72 @param code code to be checked (string) |
|
73 @return flag indicating, that the code should be ignored (boolean) |
|
74 """ |
|
75 if pep8.options.select: |
|
76 if code.startswith(tuple(pep8.options.select)): |
|
77 if code.startswith(tuple(pep8.options.ignore)): |
|
78 return True |
|
79 else: |
|
80 return False |
|
81 else: |
|
82 return True |
|
83 else: |
|
84 if code.startswith(tuple(pep8.options.ignore)): |
|
85 return True |
|
86 else: |
|
87 return False |
|
88 |
|
89 def report_error_args(self, line_number, offset, code, check, *args): |
|
90 """ |
34 """ |
91 Public method to collect the error messages. |
35 Public method to collect the error messages. |
92 |
36 |
93 @param line_number line number of the issue (integer) |
37 @param line_number line number of the issue (integer) |
94 @param offset position within line of the issue (integer) |
38 @param offset position within line of the issue (integer) |
95 @param code message code (string) |
39 @param code message code (string) |
96 @param check reference to the checker function (function) |
40 @param check reference to the checker function (function) |
97 @param args arguments for the message (list) |
41 @param args arguments for the message (list) |
98 """ |
42 """ |
99 if self.__ignore_code(code): |
43 code = super(Pep8Report, self).error_args(line_number, offset, code, check, *args) |
100 return |
44 if code and (self.counters[code] == 1 or self.__repeat): |
101 |
45 self.errors.append( |
102 if code in self.statistics: |
46 (self.filename, line_number, offset, code, args) |
103 self.statistics[code] += 1 |
|
104 else: |
|
105 self.statistics[code] = 1 |
|
106 self.file_errors += 1 |
|
107 if self.statistics[code] == 1 or pep8.options.repeat: |
|
108 self.messages.append( |
|
109 (self.filename, self.line_offset + line_number, |
|
110 offset + 1, code, args) |
|
111 ) |
47 ) |
|
48 return code |
|
49 |
112 |
50 |
113 if __name__ == "__main__": |
51 if __name__ == "__main__": |
114 repeat = False |
52 repeat = False |
115 select = "" |
53 select = "" |
116 ignore = "" |
54 ignore = "" |
117 filename = "" |
55 filename = "" |
|
56 max_line_length = 79 |
|
57 hang_closing = False |
118 |
58 |
119 if "-f" not in sys.argv: |
59 if "-f" not in sys.argv: |
120 print "ERROR" |
60 print "ERROR" |
121 print "" |
61 print "" |
122 print "No file name given." |
62 print "No file name given." |
123 else: |
63 else: |
124 try: |
64 try: |
125 optlist, args = getopt.getopt(sys.argv[1:], "rf:i:s:") |
65 optlist, args = getopt.getopt(sys.argv[1:], "f:hi:m:rs:") |
126 except getopt.GetoptError: |
66 except getopt.GetoptError: |
127 print "ERROR" |
67 print "ERROR" |
128 print "" |
68 print "" |
129 print "Wrong arguments given" |
69 print "Wrong arguments given" |
130 sys.exit(1) |
70 sys.exit(1) |
147 print "ERROR" |
95 print "ERROR" |
148 print filename |
96 print filename |
149 print "I/O Error: %s" % unicode(msg) |
97 print "I/O Error: %s" % unicode(msg) |
150 sys.exit(1) |
98 sys.exit(1) |
151 |
99 |
152 checker = Pep8Checker(filename, codestring, repeat=repeat, |
100 if select: |
153 select=select, ignore=ignore) |
101 select = [s.strip() for s in select.split(',') |
154 checker.check_all() |
102 if s.strip()] |
155 if len(checker.messages) > 0: |
103 else: |
156 checker.messages.sort(key=lambda a: a[1]) |
104 select = [] |
157 for message in checker.messages: |
105 if ignore: |
158 fname, lineno, position, code, args = message |
106 ignore = [i.strip() for i in ignore.split(',') |
|
107 if i.strip()] |
|
108 else: |
|
109 ignore = [] |
|
110 styleGuide = pep8.StyleGuide( |
|
111 reporter=Pep8Report, |
|
112 repeat=repeat, |
|
113 select=select, |
|
114 ignore=ignore, |
|
115 max_line_length=max_line_length, |
|
116 hang_closing=hang_closing, |
|
117 ) |
|
118 report = styleGuide.check_files([filename]) |
|
119 report.errors.sort(key=lambda a: a[1]) |
|
120 if len(report.errors) > 0: |
|
121 report.errors.sort(key=lambda a: a[1]) |
|
122 for error in report.errors: |
|
123 fname, lineno, position, code, args = error |
159 print "PEP8" |
124 print "PEP8" |
160 print fname |
125 print fname |
161 print lineno |
126 print lineno |
162 print position |
127 print position |
163 print code |
128 print code |
164 print len(args) |
129 print len(args) |
165 for a in args: |
130 for a in args: |
166 print a |
131 print a |
167 print "PEP8_STATISTICS" |
132 print "PEP8_STATISTICS" |
168 for key in checker.statistics: |
133 for key in report.counters: |
169 print key, checker.statistics[key] |
134 if key.startswith(("E", "W")): |
|
135 print key, report.counters[key] |
170 else: |
136 else: |
171 print "NO_PEP8" |
137 print "NO_PEP8" |
172 print filename |
138 print filename |
173 |
139 |
174 # |
140 # |