Plugins/CheckerPlugins/Tabnanny/TabnannyDialog.py

branch
Py2 comp.
changeset 2680
110ac646a3a0
parent 2525
8b507a9a2d40
child 3057
10516539f238
equal deleted inserted replaced
2679:122a9ffdeefb 2680:110ac646a3a0
7 Module implementing a dialog to show the output of the tabnanny command process. 7 Module implementing a dialog to show the output of the tabnanny command process.
8 """ 8 """
9 9
10 from __future__ import unicode_literals # __IGNORE_WARNING__ 10 from __future__ import unicode_literals # __IGNORE_WARNING__
11 try: 11 try:
12 str = unicode 12 str = unicode # __IGNORE_WARNING__
13 except (NameError): 13 except (NameError):
14 pass 14 pass
15 15
16 import os 16 import os
17 import fnmatch 17 import fnmatch
18 18
19 from PyQt4.QtCore import pyqtSlot, Qt, QProcess 19 from PyQt4.QtCore import pyqtSlot, Qt
20 from PyQt4.QtGui import QDialog, QDialogButtonBox, QTreeWidgetItem, QApplication, \ 20 from PyQt4.QtGui import QDialog, QDialogButtonBox, QTreeWidgetItem, QApplication, \
21 QHeaderView 21 QHeaderView
22 22
23 from E5Gui.E5Application import e5App 23 from E5Gui.E5Application import e5App
24 24
25 from .Ui_TabnannyDialog import Ui_TabnannyDialog 25 from .Ui_TabnannyDialog import Ui_TabnannyDialog
26 26
27 import Utilities 27 import Utilities
28 import Preferences 28 import Preferences
29
30 from eric5config import getConfig
31 29
32 30
33 class TabnannyDialog(QDialog, Ui_TabnannyDialog): 31 class TabnannyDialog(QDialog, Ui_TabnannyDialog):
34 """ 32 """
35 Class implementing a dialog to show the results of the tabnanny check run. 33 Class implementing a dialog to show the results of the tabnanny check run.
116 114
117 if isinstance(fn, list): 115 if isinstance(fn, list):
118 files = fn 116 files = fn
119 elif os.path.isdir(fn): 117 elif os.path.isdir(fn):
120 files = [] 118 files = []
121 for ext in Preferences.getPython("Python3Extensions"): 119 extensions = set(Preferences.getPython("PythonExtensions") +
122 files.extend(Utilities.direntries(fn, 1, '*{0}'.format(ext), 0)) 120 Preferences.getPython("Python3Extensions"))
123 for ext in Preferences.getPython("PythonExtensions"): 121 for ext in extensions:
124 files.extend(Utilities.direntries(fn, 1, '*{0}'.format(ext), 0)) 122 files.extend(Utilities.direntries(fn, True, '*{0}'.format(ext), 0))
125 else: 123 else:
126 files = [fn] 124 files = [fn]
127 py3files = [f for f in files \ 125
128 if f.endswith(tuple(Preferences.getPython("Python3Extensions")))] 126 if len(files) > 0:
129 py2files = [f for f in files \ 127 self.checkProgress.setMaximum(len(files))
130 if f.endswith(tuple(Preferences.getPython("PythonExtensions")))]
131
132 if len(py3files) + len(py2files) > 0:
133 self.checkProgress.setMaximum(len(py3files) + len(py2files))
134 QApplication.processEvents() 128 QApplication.processEvents()
135 129
136 # now go through all the files 130 # now go through all the files
137 progress = 0 131 progress = 0
138 for file in py3files + py2files: 132 for file in files:
139 self.checkProgress.setValue(progress) 133 self.checkProgress.setValue(progress)
140 QApplication.processEvents() 134 QApplication.processEvents()
141 self.__resort() 135 self.__resort()
142 136
143 if self.cancelled: 137 if self.cancelled:
144 return 138 return
145 139
146 try: 140 try:
147 source = Utilities.readEncodedFile(file)[0] 141 source = Utilities.readEncodedFile(file)[0]
148 # convert eols 142 source = Utilities.normalizeCode(source)
149 source = Utilities.convertLineEnds(source, "\n")
150 except (UnicodeError, IOError) as msg: 143 except (UnicodeError, IOError) as msg:
151 self.noResults = False 144 self.noResults = False
152 self.__createResultItem(file, "1", 145 self.__createResultItem(file, 1,
153 "Error: {0}".format(str(msg)).rstrip()[1:-1]) 146 "Error: {0}".format(str(msg)).rstrip()[1:-1])
154 progress += 1 147 progress += 1
155 continue 148 continue
156 149
157 flags = Utilities.extractFlags(source) 150 from . import Tabnanny
158 ext = os.path.splitext(file)[1] 151 nok, fname, line, error = Tabnanny.check(file, source)
159 if ("FileType" in flags and
160 flags["FileType"] in ["Python", "Python2"]) or \
161 file in py2files or \
162 (ext in [".py", ".pyw"] and \
163 Preferences.getProject("DeterminePyFromProject") and \
164 self.__project.isOpen() and \
165 self.__project.isProjectFile(file) and \
166 self.__project.getProjectLanguage() in ["Python", "Python2"]):
167 nok, fname, line, error = self.__py2check(file)
168 else:
169 from . import Tabnanny
170 nok, fname, line, error = Tabnanny.check(file, source)
171 if nok: 152 if nok:
172 self.noResults = False 153 self.noResults = False
173 self.__createResultItem(fname, line, error.rstrip()) 154 self.__createResultItem(fname, line, error.rstrip())
174 progress += 1 155 progress += 1
175 156
243 224
244 fn = Utilities.normabspath(itm.text(0)) 225 fn = Utilities.normabspath(itm.text(0))
245 lineno = int(itm.text(1)) 226 lineno = int(itm.text(1))
246 227
247 e5App().getObject("ViewManager").openSourceFile(fn, lineno) 228 e5App().getObject("ViewManager").openSourceFile(fn, lineno)
248
249 ############################################################################
250 ## Python 2 interface below
251 ############################################################################
252
253 def __py2check(self, filename):
254 """
255 Private method to perform the indentation check for Python 2 files.
256
257 @param filename name of the file to be checked (string)
258 @return A tuple indicating status (True = an error was found), the
259 filename, the linenumber and the error message
260 (boolean, string, string, string). The values are only
261 valid, if the status is True.
262 """
263 interpreter = Preferences.getDebugger("PythonInterpreter")
264 if interpreter == "" or not Utilities.isExecutable(interpreter):
265 return (True, filename, "1",
266 self.trUtf8("Python2 interpreter not configured."))
267
268 checker = os.path.join(getConfig('ericDir'),
269 "UtilitiesPython2", "TabnannyChecker.py")
270
271 proc = QProcess()
272 proc.setProcessChannelMode(QProcess.MergedChannels)
273 proc.start(interpreter, [checker, filename])
274 finished = proc.waitForFinished(15000)
275 if finished:
276 output = \
277 str(proc.readAllStandardOutput(),
278 Preferences.getSystem("IOEncoding"),
279 'replace').splitlines()
280
281 nok = output[0] == "ERROR"
282 if nok:
283 fn = output[1]
284 line = output[2]
285 error = output[3]
286 return (True, fn, line, error)
287 else:
288 return (False, None, None, None)
289
290 return (True, filename, "1",
291 self.trUtf8("Python2 interpreter did not finish within 15s."))

eric ide

mercurial