Plugins/CheckerPlugins/Pep8/Pep8Checker.py

branch
Py2 comp.
changeset 2680
110ac646a3a0
parent 2525
8b507a9a2d40
equal deleted inserted replaced
2679:122a9ffdeefb 2680:110ac646a3a0
10 try: 10 try:
11 str = unicode 11 str = unicode
12 except (NameError): 12 except (NameError):
13 pass 13 pass
14 14
15 import os
16 import optparse 15 import optparse
17 16
18 from PyQt4.QtCore import QProcess, QCoreApplication
19
20 from . import pep8 17 from . import pep8
21
22 import Preferences
23 import Utilities
24
25 from eric5config import getConfig
26 18
27 19
28 class Pep8Checker(pep8.Checker): 20 class Pep8Checker(pep8.Checker):
29 """ 21 """
30 Class implementing the PEP 8 checker. 22 Class implementing the PEP 8 checker.
117 if self.statistics[code] == 1 or pep8.options.repeat: 109 if self.statistics[code] == 1 or pep8.options.repeat:
118 self.messages.append( 110 self.messages.append(
119 (self.filename, self.line_offset + line_number, 111 (self.filename, self.line_offset + line_number,
120 offset + 1, text) 112 offset + 1, text)
121 ) 113 )
122
123
124 class Pep8Py2Checker(object):
125 """
126 Class implementing the PEP 8 checker interface for Python 2.
127 """
128 def __init__(self, filename, lines, repeat=False,
129 select="", ignore=""):
130 """
131 Constructor
132
133 @param filename name of the file to check (string)
134 @param lines source of the file (list of strings) (ignored)
135 @keyparam repeat flag indicating to repeat message categories (boolean)
136 @keyparam select list of message IDs to check for
137 (comma separated string)
138 @keyparam ignore list of message IDs to ignore
139 (comma separated string)
140 """
141 self.messages = []
142 self.statistics = {}
143
144 interpreter = Preferences.getDebugger("PythonInterpreter")
145 if interpreter == "" or not Utilities.isExecutable(interpreter):
146 self.messages.append((filename, 1, 1,
147 QCoreApplication.translate("Pep8Py2Checker",
148 "Python2 interpreter not configured.")))
149 return
150
151 checker = os.path.join(getConfig('ericDir'),
152 "UtilitiesPython2", "Pep8Checker.py")
153
154 args = [checker]
155 if repeat:
156 args.append("-r")
157 if select:
158 args.append("-s")
159 args.append(select)
160 if ignore:
161 args.append("-i")
162 args.append(ignore)
163 args.append("-f")
164 args.append(filename)
165
166 proc = QProcess()
167 proc.setProcessChannelMode(QProcess.MergedChannels)
168 proc.start(interpreter, args)
169 finished = proc.waitForFinished(15000)
170 if finished:
171 output = \
172 str(proc.readAllStandardOutput(),
173 Preferences.getSystem("IOEncoding"),
174 'replace').splitlines()
175 if output[0] == "ERROR":
176 self.messages.append((filename, 1, 1, output[2]))
177 return
178
179 if output[0] == "NO_PEP8":
180 return
181
182 index = 0
183 while index < len(output):
184 if output[index] == "PEP8_STATISTICS":
185 index += 1
186 break
187
188 fname = output[index + 1]
189 lineno = int(output[index + 2])
190 position = int(output[index + 3])
191 code = output[index + 4]
192 arglen = int(output[index + 5])
193 args = []
194 argindex = 0
195 while argindex < arglen:
196 args.append(output[index + 6 + argindex])
197 argindex += 1
198 index += 6 + arglen
199
200 text = pep8.getMessage(code, *args)
201 self.messages.append((fname, lineno, position, text))
202 while index < len(output):
203 code, countStr = output[index].split(None, 1)
204 self.statistics[code] = int(countStr)
205 index += 1
206 else:
207 self.messages.append((filename, 1, 1,
208 QCoreApplication.translate("Pep8Py2Checker",
209 "Python2 interpreter did not finish within 15s.")))

eric ide

mercurial