1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to show the output of the tabnanny command |
|
8 process. |
|
9 """ |
|
10 |
|
11 |
|
12 import os |
|
13 import fnmatch |
|
14 |
|
15 from PyQt5.QtCore import pyqtSlot, Qt, QTimer |
|
16 from PyQt5.QtWidgets import ( |
|
17 QDialog, QDialogButtonBox, QTreeWidgetItem, QApplication, QHeaderView |
|
18 ) |
|
19 |
|
20 from E5Gui.E5Application import e5App |
|
21 |
|
22 from .Ui_TabnannyDialog import Ui_TabnannyDialog |
|
23 |
|
24 import Utilities |
|
25 import Preferences |
|
26 |
|
27 |
|
28 class TabnannyDialog(QDialog, Ui_TabnannyDialog): |
|
29 """ |
|
30 Class implementing a dialog to show the results of the tabnanny check run. |
|
31 """ |
|
32 filenameRole = Qt.UserRole + 1 |
|
33 |
|
34 def __init__(self, indentCheckService, parent=None): |
|
35 """ |
|
36 Constructor |
|
37 |
|
38 @param indentCheckService reference to the service (IndentCheckService) |
|
39 @param parent The parent widget (QWidget). |
|
40 """ |
|
41 super(TabnannyDialog, self).__init__(parent) |
|
42 self.setupUi(self) |
|
43 self.setWindowFlags(Qt.Window) |
|
44 |
|
45 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) |
|
46 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) |
|
47 |
|
48 self.resultList.headerItem().setText(self.resultList.columnCount(), "") |
|
49 self.resultList.header().setSortIndicator(0, Qt.AscendingOrder) |
|
50 |
|
51 self.indentCheckService = indentCheckService |
|
52 self.indentCheckService.indentChecked.connect(self.__processResult) |
|
53 self.indentCheckService.batchFinished.connect(self.__batchFinished) |
|
54 self.indentCheckService.error.connect(self.__processError) |
|
55 self.filename = None |
|
56 |
|
57 self.noResults = True |
|
58 self.cancelled = False |
|
59 self.__finished = True |
|
60 self.__errorItem = None |
|
61 |
|
62 self.__fileList = [] |
|
63 self.__project = None |
|
64 self.filterFrame.setVisible(False) |
|
65 |
|
66 self.checkProgress.setVisible(False) |
|
67 self.checkProgressLabel.setVisible(False) |
|
68 self.checkProgressLabel.setMaximumWidth(600) |
|
69 |
|
70 def __resort(self): |
|
71 """ |
|
72 Private method to resort the tree. |
|
73 """ |
|
74 self.resultList.sortItems( |
|
75 self.resultList.sortColumn(), |
|
76 self.resultList.header().sortIndicatorOrder()) |
|
77 |
|
78 def __createErrorItem(self, filename, message): |
|
79 """ |
|
80 Private slot to create a new error item in the result list. |
|
81 |
|
82 @param filename name of the file |
|
83 @type str |
|
84 @param message error message |
|
85 @type str |
|
86 """ |
|
87 if self.__errorItem is None: |
|
88 self.__errorItem = QTreeWidgetItem(self.resultList, [ |
|
89 self.tr("Errors")]) |
|
90 self.__errorItem.setExpanded(True) |
|
91 self.__errorItem.setForeground(0, Qt.red) |
|
92 |
|
93 msg = "{0} ({1})".format(self.__project.getRelativePath(filename), |
|
94 message) |
|
95 if not self.resultList.findItems(msg, Qt.MatchExactly): |
|
96 itm = QTreeWidgetItem(self.__errorItem, [msg]) |
|
97 itm.setForeground(0, Qt.red) |
|
98 itm.setFirstColumnSpanned(True) |
|
99 |
|
100 def __createResultItem(self, filename, line, sourcecode): |
|
101 """ |
|
102 Private method to create an entry in the result list. |
|
103 |
|
104 @param filename filename of file (string) |
|
105 @param line linenumber of faulty source (integer or string) |
|
106 @param sourcecode faulty line of code (string) |
|
107 """ |
|
108 itm = QTreeWidgetItem(self.resultList) |
|
109 itm.setData(0, Qt.DisplayRole, |
|
110 self.__project.getRelativePath(filename)) |
|
111 itm.setData(1, Qt.DisplayRole, line) |
|
112 itm.setData(2, Qt.DisplayRole, sourcecode) |
|
113 itm.setTextAlignment(1, Qt.AlignRight) |
|
114 itm.setData(0, self.filenameRole, filename) |
|
115 |
|
116 def prepare(self, fileList, project): |
|
117 """ |
|
118 Public method to prepare the dialog with a list of filenames. |
|
119 |
|
120 @param fileList list of filenames (list of strings) |
|
121 @param project reference to the project object (Project) |
|
122 """ |
|
123 self.__fileList = fileList[:] |
|
124 self.__project = project |
|
125 |
|
126 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) |
|
127 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) |
|
128 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) |
|
129 |
|
130 self.filterFrame.setVisible(True) |
|
131 |
|
132 self.__data = self.__project.getData("CHECKERSPARMS", "Tabnanny") |
|
133 if self.__data is None or "ExcludeFiles" not in self.__data: |
|
134 self.__data = {"ExcludeFiles": ""} |
|
135 self.excludeFilesEdit.setText(self.__data["ExcludeFiles"]) |
|
136 |
|
137 def start(self, fn): |
|
138 """ |
|
139 Public slot to start the tabnanny check. |
|
140 |
|
141 @param fn File or list of files or directory to be checked |
|
142 (string or list of strings) |
|
143 """ |
|
144 if self.__project is None: |
|
145 self.__project = e5App().getObject("Project") |
|
146 |
|
147 self.cancelled = False |
|
148 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) |
|
149 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True) |
|
150 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) |
|
151 self.checkProgress.setVisible(True) |
|
152 QApplication.processEvents() |
|
153 |
|
154 if isinstance(fn, list): |
|
155 self.files = fn |
|
156 elif os.path.isdir(fn): |
|
157 self.files = [] |
|
158 extensions = set(Preferences.getPython("Python3Extensions")) |
|
159 for ext in extensions: |
|
160 self.files.extend( |
|
161 Utilities.direntries(fn, True, '*{0}'.format(ext), 0)) |
|
162 else: |
|
163 self.files = [fn] |
|
164 |
|
165 self.__errorItem = None |
|
166 |
|
167 if len(self.files) > 0: |
|
168 self.checkProgress.setMaximum(len(self.files)) |
|
169 self.checkProgress.setVisible(len(self.files) > 1) |
|
170 self.checkProgressLabel.setVisible(len(self.files) > 1) |
|
171 QApplication.processEvents() |
|
172 |
|
173 # now go through all the files |
|
174 self.progress = 0 |
|
175 self.files.sort() |
|
176 if len(self.files) == 1: |
|
177 self.__batch = False |
|
178 self.check() |
|
179 else: |
|
180 self.__batch = True |
|
181 self.checkBatch() |
|
182 |
|
183 def check(self, codestring=''): |
|
184 """ |
|
185 Public method to start an indentation check for one file. |
|
186 |
|
187 The results are reported to the __processResult slot. |
|
188 @keyparam codestring optional sourcestring (str) |
|
189 """ |
|
190 if not self.files: |
|
191 self.checkProgressLabel.setPath("") |
|
192 self.checkProgress.setMaximum(1) |
|
193 self.checkProgress.setValue(1) |
|
194 self.__finish() |
|
195 return |
|
196 |
|
197 self.filename = self.files.pop(0) |
|
198 self.checkProgress.setValue(self.progress) |
|
199 self.checkProgressLabel.setPath(self.filename) |
|
200 QApplication.processEvents() |
|
201 self.__resort() |
|
202 |
|
203 if self.cancelled: |
|
204 return |
|
205 |
|
206 try: |
|
207 self.source = Utilities.readEncodedFile(self.filename)[0] |
|
208 self.source = Utilities.normalizeCode(self.source) |
|
209 except (UnicodeError, IOError) as msg: |
|
210 self.noResults = False |
|
211 self.__createResultItem( |
|
212 self.filename, 1, |
|
213 "Error: {0}".format(str(msg)).rstrip()) |
|
214 self.progress += 1 |
|
215 # Continue with next file |
|
216 self.check() |
|
217 return |
|
218 |
|
219 self.__finished = False |
|
220 self.indentCheckService.indentCheck( |
|
221 None, self.filename, self.source) |
|
222 |
|
223 def checkBatch(self): |
|
224 """ |
|
225 Public method to start an indentation check batch job. |
|
226 |
|
227 The results are reported to the __processResult slot. |
|
228 """ |
|
229 self.__lastFileItem = None |
|
230 |
|
231 self.checkProgressLabel.setPath(self.tr("Preparing files...")) |
|
232 progress = 0 |
|
233 |
|
234 argumentsList = [] |
|
235 for filename in self.files: |
|
236 progress += 1 |
|
237 self.checkProgress.setValue(progress) |
|
238 QApplication.processEvents() |
|
239 |
|
240 try: |
|
241 source = Utilities.readEncodedFile(filename)[0] |
|
242 source = Utilities.normalizeCode(source) |
|
243 except (UnicodeError, IOError) as msg: |
|
244 self.noResults = False |
|
245 self.__createResultItem( |
|
246 filename, 1, |
|
247 "Error: {0}".format(str(msg)).rstrip()) |
|
248 continue |
|
249 |
|
250 argumentsList.append((filename, source)) |
|
251 |
|
252 # reset the progress bar to the checked files |
|
253 self.checkProgress.setValue(self.progress) |
|
254 self.checkProgressLabel.setPath(self.tr("Transferring data...")) |
|
255 QApplication.processEvents() |
|
256 |
|
257 self.__finished = False |
|
258 self.indentCheckService.indentBatchCheck(argumentsList) |
|
259 |
|
260 def __batchFinished(self): |
|
261 """ |
|
262 Private slot handling the completion of a batch job. |
|
263 """ |
|
264 self.checkProgressLabel.setPath("") |
|
265 self.checkProgress.setMaximum(1) |
|
266 self.checkProgress.setValue(1) |
|
267 self.__finish() |
|
268 |
|
269 def __processError(self, fn, msg): |
|
270 """ |
|
271 Private slot to process an error indication from the service. |
|
272 |
|
273 @param fn filename of the file |
|
274 @type str |
|
275 @param msg error message |
|
276 @type str |
|
277 """ |
|
278 self.__createErrorItem(fn, msg) |
|
279 |
|
280 if not self.__batch: |
|
281 self.check() |
|
282 |
|
283 def __processResult(self, fn, nok, line, error): |
|
284 """ |
|
285 Private slot called after perfoming a style check on one file. |
|
286 |
|
287 @param fn filename of the just checked file (str) |
|
288 @param nok flag if a problem was found (bool) |
|
289 @param line line number (str) |
|
290 @param error text of the problem (str) |
|
291 """ |
|
292 if self.__finished: |
|
293 return |
|
294 |
|
295 # Check if it's the requested file, otherwise ignore signal if not |
|
296 # in batch mode |
|
297 if not self.__batch and fn != self.filename: |
|
298 return |
|
299 |
|
300 if nok: |
|
301 self.noResults = False |
|
302 self.__createResultItem(fn, line, error.rstrip()) |
|
303 self.progress += 1 |
|
304 |
|
305 self.checkProgress.setValue(self.progress) |
|
306 self.checkProgressLabel.setPath(fn) |
|
307 QApplication.processEvents() |
|
308 self.__resort() |
|
309 |
|
310 if not self.__batch: |
|
311 self.check() |
|
312 |
|
313 def __finish(self): |
|
314 """ |
|
315 Private slot called when the action or the user pressed the button. |
|
316 """ |
|
317 if not self.__finished: |
|
318 self.__finished = True |
|
319 |
|
320 self.cancelled = True |
|
321 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) |
|
322 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) |
|
323 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) |
|
324 |
|
325 if self.noResults: |
|
326 self.__createResultItem( |
|
327 self.tr('No indentation errors found.'), "", "") |
|
328 QApplication.processEvents() |
|
329 self.resultList.header().resizeSections( |
|
330 QHeaderView.ResizeToContents) |
|
331 self.resultList.header().setStretchLastSection(True) |
|
332 |
|
333 self.checkProgress.setVisible(False) |
|
334 self.checkProgressLabel.setVisible(False) |
|
335 |
|
336 def on_buttonBox_clicked(self, button): |
|
337 """ |
|
338 Private slot called by a button of the button box clicked. |
|
339 |
|
340 @param button button that was clicked (QAbstractButton) |
|
341 """ |
|
342 if button == self.buttonBox.button(QDialogButtonBox.Close): |
|
343 self.close() |
|
344 elif button == self.buttonBox.button(QDialogButtonBox.Cancel): |
|
345 if self.__batch: |
|
346 self.indentCheckService.cancelIndentBatchCheck() |
|
347 QTimer.singleShot(1000, self.__finish) |
|
348 else: |
|
349 self.__finish() |
|
350 |
|
351 @pyqtSlot() |
|
352 def on_startButton_clicked(self): |
|
353 """ |
|
354 Private slot to start a code metrics run. |
|
355 """ |
|
356 fileList = self.__fileList[:] |
|
357 |
|
358 filterString = self.excludeFilesEdit.text() |
|
359 if ( |
|
360 "ExcludeFiles" not in self.__data or |
|
361 filterString != self.__data["ExcludeFiles"] |
|
362 ): |
|
363 self.__data["ExcludeFiles"] = filterString |
|
364 self.__project.setData("CHECKERSPARMS", "Tabnanny", self.__data) |
|
365 filterList = [f.strip() for f in filterString.split(",") |
|
366 if f.strip()] |
|
367 if filterList: |
|
368 for fileFilter in filterList: |
|
369 fileList = [ |
|
370 f for f in fileList if not fnmatch.fnmatch(f, fileFilter) |
|
371 ] |
|
372 |
|
373 self.resultList.clear() |
|
374 self.noResults = True |
|
375 self.cancelled = False |
|
376 self.start(fileList) |
|
377 |
|
378 def on_resultList_itemActivated(self, itm, col): |
|
379 """ |
|
380 Private slot to handle the activation of an item. |
|
381 |
|
382 @param itm reference to the activated item (QTreeWidgetItem) |
|
383 @param col column the item was activated in (integer) |
|
384 """ |
|
385 if self.noResults: |
|
386 return |
|
387 |
|
388 fn = Utilities.normabspath(itm.data(0, self.filenameRole)) |
|
389 lineno = int(itm.text(1)) |
|
390 |
|
391 e5App().getObject("ViewManager").openSourceFile(fn, lineno) |
|