Plugins/CheckerPlugins/Tabnanny/TabnannyDialog.py

changeset 805
83ca4d1ff648
parent 793
cd183f89874b
child 830
6caa4436dee2
equal deleted inserted replaced
804:3465556892de 805:83ca4d1ff648
19 19
20 from . import Tabnanny 20 from . import Tabnanny
21 import Utilities 21 import Utilities
22 import Preferences 22 import Preferences
23 import UI.PixmapCache 23 import UI.PixmapCache
24
25 from eric5config import getConfig
24 26
25 class TabnannyDialog(QDialog, Ui_TabnannyDialog): 27 class TabnannyDialog(QDialog, Ui_TabnannyDialog):
26 """ 28 """
27 Class implementing a dialog to show the results of the tabnanny check run. 29 Class implementing a dialog to show the results of the tabnanny check run.
28 """ 30 """
93 Public slot to start the tabnanny check. 95 Public slot to start the tabnanny check.
94 96
95 @param fn File or list of files or directory to be checked 97 @param fn File or list of files or directory to be checked
96 (string or list of strings) 98 (string or list of strings)
97 """ 99 """
100 if self.__project is None:
101 self.__project = e5App().getObject("Project")
102
98 self.cancelled = False 103 self.cancelled = False
99 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) 104 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
100 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True) 105 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True)
101 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) 106 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
102 QApplication.processEvents() 107 QApplication.processEvents()
105 files = fn 110 files = fn
106 elif os.path.isdir(fn): 111 elif os.path.isdir(fn):
107 files = [] 112 files = []
108 for ext in Preferences.getPython("Python3Extensions"): 113 for ext in Preferences.getPython("Python3Extensions"):
109 files.extend(Utilities.direntries(fn, 1, '*{0}'.format(ext), 0)) 114 files.extend(Utilities.direntries(fn, 1, '*{0}'.format(ext), 0))
115 for ext in Preferences.getPython("PythonExtensions"):
116 files.extend(Utilities.direntries(fn, 1, '*{0}'.format(ext), 0))
110 else: 117 else:
111 files = [fn] 118 files = [fn]
112 files = [f for f in files \ 119 files = [f for f in files \
113 if f.endswith(tuple(Preferences.getPython("Python3Extensions")))] 120 if f.endswith(tuple(Preferences.getPython("Python3Extensions")))]
114 121 py2files = [f for f in files \
115 if len(files) > 0: 122 if f.endswith(tuple(Preferences.getPython("PythonExtensions")))]
116 self.checkProgress.setMaximum(len(files)) 123
124 if len(files) + len(py2files) > 0:
125 self.checkProgress.setMaximum(len(files) + len(py2files))
117 QApplication.processEvents() 126 QApplication.processEvents()
118 127
119 # now go through all the files 128 # now go through all the files
120 progress = 0 129 progress = 0
121 for file in files: 130 for file in files + py2files:
122 self.checkProgress.setValue(progress) 131 self.checkProgress.setValue(progress)
123 QApplication.processEvents() 132 QApplication.processEvents()
124 self.__resort() 133 self.__resort()
125 134
126 if self.cancelled: 135 if self.cancelled:
136 "Error: {0}".format(str(msg)).rstrip()[1:-1]) 145 "Error: {0}".format(str(msg)).rstrip()[1:-1])
137 progress += 1 146 progress += 1
138 continue 147 continue
139 148
140 flags = Utilities.extractFlags(source) 149 flags = Utilities.extractFlags(source)
141 if "FileType" in flags and flags["FileType"] != "Python3": 150 ext = os.path.splitext(file)[1]
142 # skip non Python 3 files 151 if ("FileType" in flags and
143 progress += 1 152 flags["FileType"] in ["Python", "Python2"]) or \
144 continue 153 file in py2files or \
145 154 (ext in [".py", ".pyw"] and \
146 nok, fname, line, error = Tabnanny.check(file, source) 155 Preferences.getProject("DeterminePyFromProject") and \
156 self.__project.isOpen() and \
157 self.__project.isProjectFile(file) and \
158 self.__project.getProjectLanguage() in ["Python", "Python2"]):
159 nok, fname, line, error = self.__py2check(file)
160 else:
161 nok, fname, line, error = Tabnanny.check(file, source)
147 if nok: 162 if nok:
148 self.noResults = False 163 self.noResults = False
149 self.__createResultItem(fname, line, error.rstrip()[1:-1]) 164 self.__createResultItem(fname, line, error.rstrip())
150 progress += 1 165 progress += 1
151 166
152 self.checkProgress.setValue(progress) 167 self.checkProgress.setValue(progress)
153 QApplication.processEvents() 168 QApplication.processEvents()
154 self.__resort() 169 self.__resort()
218 233
219 fn = Utilities.normabspath(itm.text(0)) 234 fn = Utilities.normabspath(itm.text(0))
220 lineno = int(itm.text(1)) 235 lineno = int(itm.text(1))
221 236
222 e5App().getObject("ViewManager").openSourceFile(fn, lineno) 237 e5App().getObject("ViewManager").openSourceFile(fn, lineno)
238
239 ############################################################################
240 ## Python 2 interface below
241 ############################################################################
242
243 def __py2check(self, filename):
244 """
245 Private method to perform the indentation check for Python 2 files.
246
247 @param filename name of the file to be checked (string)
248 @return A tuple indicating status (True = an error was found), the
249 filename, the linenumber and the error message
250 (boolean, string, string, string). The values are only
251 valid, if the status is True.
252 """
253 interpreter = Preferences.getDebugger("PythonInterpreter")
254 if interpreter == "" or not Utilities.isExecutable(interpreter):
255 return (True, filename, "1",
256 self.trUtf8("Python2 interpreter not configured."))
257
258 checker = os.path.join(getConfig('ericDir'),
259 "UtilitiesPython2", "TabnannyChecker.py")
260
261 proc = QProcess()
262 proc.setProcessChannelMode(QProcess.MergedChannels)
263 proc.start(interpreter, [checker, filename])
264 finished = proc.waitForFinished(15000)
265 if finished:
266 output = \
267 str(proc.readAllStandardOutput(),
268 Preferences.getSystem("IOEncoding"),
269 'replace').splitlines()
270
271 nok = output[0] == "ERROR"
272 if nok:
273 fn = output[1]
274 line = output[2]
275 error = output[3]
276 return (True, fn, line, error)
277 else:
278 return (False, None, None, None)
279
280 return (True, filename, "1",
281 self.trUtf8("Python2 interpreter did not finish within 15s."))

eric ide

mercurial