|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Class implementing the PEP 8 checker for Python2. |
|
8 """ |
|
9 |
|
10 import sys |
|
11 import getopt |
|
12 |
|
13 from Tools import readEncodedFile, normalizeCode |
|
14 |
|
15 import pep8 |
|
16 from NamingStyleCheckerPy2 import NamingStyleChecker |
|
17 |
|
18 # register the name checker |
|
19 pep8.register_check(NamingStyleChecker, NamingStyleChecker.Codes) |
|
20 |
|
21 from DocStyleCheckerPy2 import DocStyleChecker |
|
22 |
|
23 |
|
24 class CodeStyleReport(pep8.BaseReport): |
|
25 """ |
|
26 Class implementing a special report to be used with our dialog. |
|
27 """ |
|
28 def __init__(self, options): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param options options for the report (optparse.Values) |
|
33 """ |
|
34 super(CodeStyleReport, self).__init__(options) |
|
35 |
|
36 self.__repeat = options.repeat |
|
37 self.errors = [] |
|
38 |
|
39 def error_args(self, line_number, offset, code, check, *args): |
|
40 """ |
|
41 Public method to collect the error messages. |
|
42 |
|
43 @param line_number line number of the issue (integer) |
|
44 @param offset position within line of the issue (integer) |
|
45 @param code message code (string) |
|
46 @param check reference to the checker function (function) |
|
47 @param args arguments for the message (list) |
|
48 @return error code (string) |
|
49 """ |
|
50 code = super(CodeStyleReport, self).error_args( |
|
51 line_number, offset, code, check, *args) |
|
52 if code and (self.counters[code] == 1 or self.__repeat): |
|
53 self.errors.append( |
|
54 (self.filename, line_number, offset, code, args) |
|
55 ) |
|
56 return code |
|
57 |
|
58 |
|
59 if __name__ == "__main__": |
|
60 repeat = False |
|
61 select = "" |
|
62 ignore = "" |
|
63 filename = "" |
|
64 max_line_length = 79 |
|
65 hang_closing = False |
|
66 docType = "pep257" |
|
67 |
|
68 if "-f" not in sys.argv: |
|
69 print "ERROR" |
|
70 print "" |
|
71 print "No file name given." |
|
72 else: |
|
73 try: |
|
74 optlist, args = getopt.getopt(sys.argv[1:], "d:f:hi:m:rs:") |
|
75 except getopt.GetoptError: |
|
76 print "ERROR" |
|
77 print "" |
|
78 print "Wrong arguments given" |
|
79 sys.exit(1) |
|
80 |
|
81 for opt, arg in optlist: |
|
82 if opt == "-r": |
|
83 repeat = True |
|
84 elif opt == "-f": |
|
85 filename = arg |
|
86 elif opt == "-i": |
|
87 ignore = arg |
|
88 elif opt == "-s": |
|
89 select = arg |
|
90 elif opt == "-m": |
|
91 try: |
|
92 max_line_length = int(arg) |
|
93 except ValueError: |
|
94 # ignore silently |
|
95 pass |
|
96 elif opt == "-h": |
|
97 hang_closing = True |
|
98 elif opt == "-d": |
|
99 if arg in ("pep257", "eric"): |
|
100 docType = arg |
|
101 |
|
102 try: |
|
103 source = readEncodedFile(filename)[0] |
|
104 source = normalizeCode(source) |
|
105 source = source.splitlines(True) |
|
106 except IOError, msg: |
|
107 print "ERROR" |
|
108 print filename |
|
109 print "I/O Error: %s" % unicode(msg) |
|
110 sys.exit(1) |
|
111 |
|
112 if select: |
|
113 select = [s.strip() for s in select.split(',') |
|
114 if s.strip()] |
|
115 else: |
|
116 select = [] |
|
117 if ignore: |
|
118 ignore = [i.strip() for i in ignore.split(',') |
|
119 if i.strip()] |
|
120 else: |
|
121 ignore = [] |
|
122 |
|
123 # check PEP-8 |
|
124 styleGuide = pep8.StyleGuide( |
|
125 reporter=CodeStyleReport, |
|
126 repeat=repeat, |
|
127 select=select, |
|
128 ignore=ignore, |
|
129 max_line_length=max_line_length, |
|
130 hang_closing=hang_closing, |
|
131 ) |
|
132 report = styleGuide.check_files([filename]) |
|
133 |
|
134 # check PEP-257 |
|
135 pep257Checker = DocStyleChecker( |
|
136 source, filename, select, ignore, [], repeat, |
|
137 maxLineLength=max_line_length, docType=docType) |
|
138 pep257Checker.run() |
|
139 |
|
140 |
|
141 errors = report.errors + pep257Checker.errors |
|
142 |
|
143 if len(errors) > 0: |
|
144 errors.sort(key=lambda a: a[1]) |
|
145 for error in errors: |
|
146 fname, lineno, position, code, args = error |
|
147 print "PEP8" |
|
148 print fname |
|
149 print lineno |
|
150 print position |
|
151 print code |
|
152 print len(args) |
|
153 for a in args: |
|
154 print a |
|
155 print "PEP8_STATISTICS" |
|
156 for key in report.counters: |
|
157 if key.startswith(("E", "N", "W")): |
|
158 print key, report.counters[key] |
|
159 for key in pep257Checker.counters: |
|
160 if key.startswith("D"): |
|
161 print key, pep257Checker.counters[key] |
|
162 else: |
|
163 print "NO_PEP8" |
|
164 print filename |
|
165 |
|
166 # |
|
167 # eflag: FileType = Python2 |