|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the PEP 8 checker. |
|
8 """ |
|
9 |
|
10 try: |
|
11 str = unicode |
|
12 except (NameError): |
|
13 pass |
|
14 |
|
15 import os |
|
16 |
|
17 from PyQt4.QtCore import QProcess, QCoreApplication |
|
18 |
|
19 from . import pep8 |
|
20 from .Pep8NamingChecker import Pep8NamingChecker |
|
21 from .Pep257Checker import Pep257Checker |
|
22 |
|
23 import Preferences |
|
24 import Utilities |
|
25 |
|
26 from eric5config import getConfig |
|
27 |
|
28 |
|
29 class Pep8Py2Checker(object): |
|
30 """ |
|
31 Class implementing the PEP 8 checker interface for Python 2. |
|
32 """ |
|
33 def __init__(self, filename, lines, repeat=False, |
|
34 select="", ignore="", max_line_length=79, |
|
35 hang_closing=False, docType="pep257"): |
|
36 """ |
|
37 Constructor |
|
38 |
|
39 @param filename name of the file to check (string) |
|
40 @param lines source of the file (list of strings) (ignored) |
|
41 @keyparam repeat flag indicating to repeat message categories (boolean) |
|
42 @keyparam select list of message IDs to check for |
|
43 (comma separated string) |
|
44 @keyparam ignore list of message IDs to ignore |
|
45 (comma separated string) |
|
46 @keyparam max_line_length maximum allowed line length (integer) |
|
47 @keyparam hang_closing flag indicating to allow hanging closing |
|
48 brackets (boolean) |
|
49 @keyparam docType type of the documentation strings |
|
50 (string, one of 'eric' or 'pep257') |
|
51 """ |
|
52 assert docType in ("eric", "pep257") |
|
53 |
|
54 self.errors = [] |
|
55 self.counters = {} |
|
56 |
|
57 interpreter = Preferences.getDebugger("PythonInterpreter") |
|
58 if interpreter == "" or not Utilities.isExecutable(interpreter): |
|
59 self.errors.append((filename, 1, 1, |
|
60 QCoreApplication.translate("Pep8Py2Checker", |
|
61 "Python2 interpreter not configured."))) |
|
62 return |
|
63 |
|
64 checker = os.path.join(getConfig('ericDir'), |
|
65 "UtilitiesPython2", "Pep8Checker.py") |
|
66 |
|
67 args = [checker] |
|
68 if repeat: |
|
69 args.append("-r") |
|
70 if select: |
|
71 args.append("-s") |
|
72 args.append(select) |
|
73 if ignore: |
|
74 args.append("-i") |
|
75 args.append(ignore) |
|
76 args.append("-m") |
|
77 args.append(str(max_line_length)) |
|
78 if hang_closing: |
|
79 args.append("-h") |
|
80 args.append("-d") |
|
81 args.append(docType) |
|
82 args.append("-f") |
|
83 args.append(filename) |
|
84 |
|
85 proc = QProcess() |
|
86 proc.setProcessChannelMode(QProcess.MergedChannels) |
|
87 proc.start(interpreter, args) |
|
88 finished = proc.waitForFinished(15000) |
|
89 if finished: |
|
90 output = \ |
|
91 str(proc.readAllStandardOutput(), |
|
92 Preferences.getSystem("IOEncoding"), |
|
93 'replace').splitlines() |
|
94 if output[0] == "ERROR": |
|
95 self.errors.append((filename, 1, 1, output[2])) |
|
96 return |
|
97 |
|
98 if output[0] == "NO_PEP8": |
|
99 return |
|
100 |
|
101 index = 0 |
|
102 while index < len(output): |
|
103 if output[index] == "PEP8_STATISTICS": |
|
104 index += 1 |
|
105 break |
|
106 |
|
107 fname = output[index + 1] |
|
108 lineno = int(output[index + 2]) |
|
109 position = int(output[index + 3]) |
|
110 code = output[index + 4] |
|
111 arglen = int(output[index + 5]) |
|
112 args = [] |
|
113 argindex = 0 |
|
114 while argindex < arglen: |
|
115 args.append(output[index + 6 + argindex]) |
|
116 argindex += 1 |
|
117 index += 6 + arglen |
|
118 |
|
119 if code in Pep8NamingChecker.Codes: |
|
120 text = Pep8NamingChecker.getMessage(code, *args) |
|
121 elif code in Pep257Checker.Codes: |
|
122 text = Pep257Checker.getMessage(code, *args) |
|
123 else: |
|
124 text = pep8.getMessage(code, *args) |
|
125 self.errors.append((fname, lineno, position, text)) |
|
126 while index < len(output): |
|
127 code, countStr = output[index].split(None, 1) |
|
128 self.counters[code] = int(countStr) |
|
129 index += 1 |
|
130 else: |
|
131 self.errors.append((filename, 1, 1, |
|
132 QCoreApplication.translate("Pep8Py2Checker", |
|
133 "Python2 interpreter did not finish within 15s."))) |