Plugins/CheckerPlugins/Pep8/Pep8Dialog.py

changeset 945
8cd4d08fa9f6
parent 940
0f5461fe69d4
child 1130
3e9f0330f833
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
26 from .Ui_Pep8Dialog import Ui_Pep8Dialog 26 from .Ui_Pep8Dialog import Ui_Pep8Dialog
27 27
28 import UI.PixmapCache 28 import UI.PixmapCache
29 import Preferences 29 import Preferences
30 import Utilities 30 import Utilities
31
31 32
32 class Pep8Dialog(QDialog, Ui_Pep8Dialog): 33 class Pep8Dialog(QDialog, Ui_Pep8Dialog):
33 """ 34 """
34 Class implementing a dialog to show the results of the PEP 8 check. 35 Class implementing a dialog to show the results of the PEP 8 check.
35 """ 36 """
36 filenameRole = Qt.UserRole + 1 37 filenameRole = Qt.UserRole + 1
37 lineRole = Qt.UserRole + 2 38 lineRole = Qt.UserRole + 2
38 positionRole = Qt.UserRole + 3 39 positionRole = Qt.UserRole + 3
39 messageRole = Qt.UserRole + 4 40 messageRole = Qt.UserRole + 4
40 41
41 settingsKey = "PEP8/" 42 settingsKey = "PEP8/"
42 43
43 def __init__(self, parent = None): 44 def __init__(self, parent=None):
44 """ 45 """
45 Constructor 46 Constructor
46 47
47 @param parent reference to the parent widget (QWidget) 48 @param parent reference to the parent widget (QWidget)
48 """ 49 """
87 88
88 def __resort(self): 89 def __resort(self):
89 """ 90 """
90 Private method to resort the tree. 91 Private method to resort the tree.
91 """ 92 """
92 self.resultList.sortItems(self.resultList.sortColumn(), 93 self.resultList.sortItems(self.resultList.sortColumn(),
93 self.resultList.header().sortIndicatorOrder() 94 self.resultList.header().sortIndicatorOrder()
94 ) 95 )
95 96
96 def __createResultItem(self, file, line, pos, message, fixed): 97 def __createResultItem(self, file, line, pos, message, fixed):
97 """ 98 """
109 self.__lastFileItem.setFirstColumnSpanned(True) 110 self.__lastFileItem.setFirstColumnSpanned(True)
110 self.__lastFileItem.setExpanded(True) 111 self.__lastFileItem.setExpanded(True)
111 self.__lastFileItem.setData(0, self.filenameRole, file) 112 self.__lastFileItem.setData(0, self.filenameRole, file)
112 113
113 code, message = message.split(None, 1) 114 code, message = message.split(None, 1)
114 itm = QTreeWidgetItem(self.__lastFileItem, 115 itm = QTreeWidgetItem(self.__lastFileItem,
115 ["{0:6}".format(line), code, message]) 116 ["{0:6}".format(line), code, message])
116 if code.startswith("W"): 117 if code.startswith("W"):
117 itm.setIcon(1, UI.PixmapCache.getIcon("warning.png")) 118 itm.setIcon(1, UI.PixmapCache.getIcon("warning.png"))
118 else: 119 else:
119 itm.setIcon(1, UI.PixmapCache.getIcon("syntaxError.png")) 120 itm.setIcon(1, UI.PixmapCache.getIcon("syntaxError.png"))
181 if self.__data is None or \ 182 if self.__data is None or \
182 "ExcludeFiles" not in self.__data or \ 183 "ExcludeFiles" not in self.__data or \
183 len(self.__data) != 6: 184 len(self.__data) != 6:
184 # initialize the data structure 185 # initialize the data structure
185 self.__data = { 186 self.__data = {
186 "ExcludeFiles" : "", 187 "ExcludeFiles": "",
187 "ExcludeMessages" : pep8.DEFAULT_IGNORE, 188 "ExcludeMessages": pep8.DEFAULT_IGNORE,
188 "IncludeMessages" : "", 189 "IncludeMessages": "",
189 "RepeatMessages" : False, 190 "RepeatMessages": False,
190 "FixCodes" : "", 191 "FixCodes": "",
191 "FixIssues" : False, 192 "FixIssues": False,
192 } 193 }
193 self.excludeFilesEdit.setText(self.__data["ExcludeFiles"]) 194 self.excludeFilesEdit.setText(self.__data["ExcludeFiles"])
194 self.excludeMessagesEdit.setText(self.__data["ExcludeMessages"]) 195 self.excludeMessagesEdit.setText(self.__data["ExcludeMessages"])
195 self.includeMessagesEdit.setText(self.__data["IncludeMessages"]) 196 self.includeMessagesEdit.setText(self.__data["IncludeMessages"])
196 self.repeatCheckBox.setChecked(self.__data["RepeatMessages"]) 197 self.repeatCheckBox.setChecked(self.__data["RepeatMessages"])
197 self.fixIssuesEdit.setText(self.__data["FixCodes"]) 198 self.fixIssuesEdit.setText(self.__data["FixCodes"])
198 self.fixIssuesCheckBox.setChecked(self.__data["FixIssues"]) 199 self.fixIssuesCheckBox.setChecked(self.__data["FixIssues"])
199 200
200 def start(self, fn, save = False, repeat = None): 201 def start(self, fn, save=False, repeat=None):
201 """ 202 """
202 Public slot to start the PEP 8 check. 203 Public slot to start the PEP 8 check.
203 204
204 @param fn file or list of files or directory to be checked 205 @param fn file or list of files or directory to be checked
205 (string or list of strings) 206 (string or list of strings)
206 @keyparam save flag indicating to save the given 207 @keyparam save flag indicating to save the given
207 file/file list/directory (boolean) 208 file/file list/directory (boolean)
208 @keyparam repeat state of the repeat check box if it is not None 209 @keyparam repeat state of the repeat check box if it is not None
209 (None or boolean) 210 (None or boolean)
210 """ 211 """
211 if self.__project is None: 212 if self.__project is None:
240 files = [fn] 241 files = [fn]
241 242
242 # filter the list depending on the filter string 243 # filter the list depending on the filter string
243 if files: 244 if files:
244 filterString = self.excludeFilesEdit.text() 245 filterString = self.excludeFilesEdit.text()
245 filterList = [f.strip() for f in filterString.split(",") 246 filterList = [f.strip() for f in filterString.split(",")
246 if f.strip()] 247 if f.strip()]
247 for filter in filterList: 248 for filter in filterList:
248 files = \ 249 files = \
249 [f for f in files 250 [f for f in files
250 if not fnmatch.fnmatch(f, filter.strip())] 251 if not fnmatch.fnmatch(f, filter.strip())]
251 252
252 py3files = [f for f in files \ 253 py3files = [f for f in files \
253 if f.endswith( 254 if f.endswith(
254 tuple(Preferences.getPython("Python3Extensions")))] 255 tuple(Preferences.getPython("Python3Extensions")))]
287 try: 288 try:
288 source, encoding = Utilities.readEncodedFile(file) 289 source, encoding = Utilities.readEncodedFile(file)
289 source = source.splitlines(True) 290 source = source.splitlines(True)
290 except (UnicodeError, IOError) as msg: 291 except (UnicodeError, IOError) as msg:
291 self.noResults = False 292 self.noResults = False
292 self.__createResultItem(file, "1", "1", 293 self.__createResultItem(file, "1", "1",
293 self.trUtf8("Error: {0}").format(str(msg))\ 294 self.trUtf8("Error: {0}").format(str(msg))\
294 .rstrip()[1:-1], False) 295 .rstrip()[1:-1], False)
295 progress += 1 296 progress += 1
296 continue 297 continue
297 298
298 flags = Utilities.extractFlags(source) 299 flags = Utilities.extractFlags(source)
299 ext = os.path.splitext(file)[1] 300 ext = os.path.splitext(file)[1]
300 if fixIssues: 301 if fixIssues:
301 fixer = Pep8Fixer(self.__project, file, source, 302 fixer = Pep8Fixer(self.__project, file, source,
302 fixCodes, True) # always fix in place 303 fixCodes, True) # always fix in place
303 else: 304 else:
304 fixer = None 305 fixer = None
305 if ("FileType" in flags and 306 if ("FileType" in flags and
306 flags["FileType"] in ["Python", "Python2"]) or \ 307 flags["FileType"] in ["Python", "Python2"]) or \
307 file in py2files or \ 308 file in py2files or \
308 (ext in [".py", ".pyw"] and \ 309 (ext in [".py", ".pyw"] and \
309 Preferences.getProject("DeterminePyFromProject") and \ 310 Preferences.getProject("DeterminePyFromProject") and \
310 self.__project.isOpen() and \ 311 self.__project.isOpen() and \
311 self.__project.isProjectFile(file) and \ 312 self.__project.isProjectFile(file) and \
312 self.__project.getProjectLanguage() in ["Python", 313 self.__project.getProjectLanguage() in ["Python",
313 "Python2"]): 314 "Python2"]):
314 checker = Pep8Py2Checker(file, [], 315 checker = Pep8Py2Checker(file, [],
315 repeat = repeatMessages, 316 repeat=repeatMessages,
316 select = includeMessages, 317 select=includeMessages,
317 ignore = excludeMessages) 318 ignore=excludeMessages)
318 else: 319 else:
319 checker = Pep8Checker(file, source, 320 checker = Pep8Checker(file, source,
320 repeat = repeatMessages, 321 repeat=repeatMessages,
321 select = includeMessages, 322 select=includeMessages,
322 ignore = excludeMessages) 323 ignore=excludeMessages)
323 checker.check_all() 324 checker.check_all()
324 checker.messages.sort(key = lambda a: a[1]) 325 checker.messages.sort(key=lambda a: a[1])
325 for message in checker.messages: 326 for message in checker.messages:
326 fname, lineno, position, text = message 327 fname, lineno, position, text = message
327 if not source[lineno - 1].strip()\ 328 if not source[lineno - 1].strip()\
328 .endswith("__IGNORE_WARNING__"): 329 .endswith("__IGNORE_WARNING__"):
329 self.noResults = False 330 self.noResults = False
379 """ 380 """
380 Private slot to start a PEP 8 check run. 381 Private slot to start a PEP 8 check run.
381 """ 382 """
382 if self.__forProject: 383 if self.__forProject:
383 data = { 384 data = {
384 "ExcludeFiles" : self.excludeFilesEdit.text(), 385 "ExcludeFiles": self.excludeFilesEdit.text(),
385 "ExcludeMessages" : self.excludeMessagesEdit.text(), 386 "ExcludeMessages": self.excludeMessagesEdit.text(),
386 "IncludeMessages" : self.includeMessagesEdit.text(), 387 "IncludeMessages": self.includeMessagesEdit.text(),
387 "RepeatMessages" : self.repeatCheckBox.isChecked(), 388 "RepeatMessages": self.repeatCheckBox.isChecked(),
388 "FixCodes" : self.fixIssuesEdit.text(), 389 "FixCodes": self.fixIssuesEdit.text(),
389 "FixIssues" : self.fixIssuesCheckBox.isChecked(), 390 "FixIssues": self.fixIssuesCheckBox.isChecked(),
390 } 391 }
391 if data != self.__data: 392 if data != self.__data:
392 self.__data = data 393 self.__data = data
393 self.__project.setData("CHECKERSPARMS", "Pep8Checker", 394 self.__project.setData("CHECKERSPARMS", "Pep8Checker",
394 self.__data) 395 self.__data)
395 396
396 self.resultList.clear() 397 self.resultList.clear()
397 self.noResults = True 398 self.noResults = True
398 self.cancelled = False 399 self.cancelled = False
432 self.fixIssuesEdit.setText(dlg.getSelectedCodes()) 433 self.fixIssuesEdit.setText(dlg.getSelectedCodes())
433 434
434 @pyqtSlot(QTreeWidgetItem, int) 435 @pyqtSlot(QTreeWidgetItem, int)
435 def on_resultList_itemActivated(self, item, column): 436 def on_resultList_itemActivated(self, item, column):
436 """ 437 """
437 Private slot to handle the activation of an item. 438 Private slot to handle the activation of an item.
438 439
439 @param item reference to the activated item (QTreeWidgetItem) 440 @param item reference to the activated item (QTreeWidgetItem)
440 @param column column the item was activated in (integer) 441 @param column column the item was activated in (integer)
441 """ 442 """
442 if self.noResults: 443 if self.noResults:
447 lineno = item.data(0, self.lineRole) 448 lineno = item.data(0, self.lineRole)
448 position = item.data(0, self.positionRole) 449 position = item.data(0, self.positionRole)
449 message = item.data(0, self.messageRole) 450 message = item.data(0, self.messageRole)
450 451
451 vm = e5App().getObject("ViewManager") 452 vm = e5App().getObject("ViewManager")
452 vm.openSourceFile(fn, lineno = lineno, pos = position) 453 vm.openSourceFile(fn, lineno=lineno, pos=position)
453 editor = vm.getOpenEditor(fn) 454 editor = vm.getOpenEditor(fn)
454 455
455 editor.toggleFlakesWarning(lineno, True, message) 456 editor.toggleFlakesWarning(lineno, True, message)
456 457
457 @pyqtSlot() 458 @pyqtSlot()

eric ide

mercurial