Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py

changeset 945
8cd4d08fa9f6
parent 915
c1e052773c08
child 1112
8a7d1b9d18db
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
21 from Utilities.py3flakes.checker import Checker 21 from Utilities.py3flakes.checker import Checker
22 from Utilities.py3flakes.messages import ImportStarUsed 22 from Utilities.py3flakes.messages import ImportStarUsed
23 import Preferences 23 import Preferences
24 import UI.PixmapCache 24 import UI.PixmapCache
25 25
26
26 class SyntaxCheckerDialog(QDialog, Ui_SyntaxCheckerDialog): 27 class SyntaxCheckerDialog(QDialog, Ui_SyntaxCheckerDialog):
27 """ 28 """
28 Class implementing a dialog to display the results of a syntax check run. 29 Class implementing a dialog to display the results of a syntax check run.
29 """ 30 """
30 filenameRole = Qt.UserRole + 1 31 filenameRole = Qt.UserRole + 1
31 lineRole = Qt.UserRole + 2 32 lineRole = Qt.UserRole + 2
32 indexRole = Qt.UserRole + 3 33 indexRole = Qt.UserRole + 3
33 errorRole = Qt.UserRole + 4 34 errorRole = Qt.UserRole + 4
34 warningRole = Qt.UserRole + 5 35 warningRole = Qt.UserRole + 5
35 36
36 def __init__(self, parent = None): 37 def __init__(self, parent=None):
37 """ 38 """
38 Constructor 39 Constructor
39 40
40 @param parent The parent widget. (QWidget) 41 @param parent The parent widget. (QWidget)
41 """ 42 """
63 64
64 def __resort(self): 65 def __resort(self):
65 """ 66 """
66 Private method to resort the tree. 67 Private method to resort the tree.
67 """ 68 """
68 self.resultList.sortItems(self.resultList.sortColumn(), 69 self.resultList.sortItems(self.resultList.sortColumn(),
69 self.resultList.header().sortIndicatorOrder() 70 self.resultList.header().sortIndicatorOrder()
70 ) 71 )
71 72
72 def __createResultItem(self, file, line, index, error, sourcecode, 73 def __createResultItem(self, file, line, index, error, sourcecode,
73 isWarning = False): 74 isWarning=False):
74 """ 75 """
75 Private method to create an entry in the result list. 76 Private method to create an entry in the result list.
76 77
77 @param file file name of file (string) 78 @param file file name of file (string)
78 @param line line number of faulty source (integer or string) 79 @param line line number of faulty source (integer or string)
86 self.__lastFileItem = QTreeWidgetItem(self.resultList, [file]) 87 self.__lastFileItem = QTreeWidgetItem(self.resultList, [file])
87 self.__lastFileItem.setFirstColumnSpanned(True) 88 self.__lastFileItem.setFirstColumnSpanned(True)
88 self.__lastFileItem.setExpanded(True) 89 self.__lastFileItem.setExpanded(True)
89 self.__lastFileItem.setData(0, self.filenameRole, file) 90 self.__lastFileItem.setData(0, self.filenameRole, file)
90 91
91 itm = QTreeWidgetItem(self.__lastFileItem, 92 itm = QTreeWidgetItem(self.__lastFileItem,
92 ["{0:6}".format(line), error, sourcecode]) 93 ["{0:6}".format(line), error, sourcecode])
93 if isWarning: 94 if isWarning:
94 itm.setIcon(0, UI.PixmapCache.getIcon("warning.png")) 95 itm.setIcon(0, UI.PixmapCache.getIcon("warning.png"))
95 else: 96 else:
96 itm.setIcon(0, UI.PixmapCache.getIcon("syntaxError.png")) 97 itm.setIcon(0, UI.PixmapCache.getIcon("syntaxError.png"))
116 117
117 self.filterFrame.setVisible(True) 118 self.filterFrame.setVisible(True)
118 119
119 self.__data = self.__project.getData("CHECKERSPARMS", "SyntaxChecker") 120 self.__data = self.__project.getData("CHECKERSPARMS", "SyntaxChecker")
120 if self.__data is None or "ExcludeFiles" not in self.__data: 121 if self.__data is None or "ExcludeFiles" not in self.__data:
121 self.__data = {"ExcludeFiles" : ""} 122 self.__data = {"ExcludeFiles": ""}
122 self.excludeFilesEdit.setText(self.__data["ExcludeFiles"]) 123 self.excludeFilesEdit.setText(self.__data["ExcludeFiles"])
123 124
124 def start(self, fn, codestring = ""): 125 def start(self, fn, codestring=""):
125 """ 126 """
126 Public slot to start the syntax check. 127 Public slot to start the syntax check.
127 128
128 @param fn file or list of files or directory to be checked 129 @param fn file or list of files or directory to be checked
129 (string or list of strings) 130 (string or list of strings)
186 source = Utilities.readEncodedFile(file)[0] 187 source = Utilities.readEncodedFile(file)[0]
187 # convert eols 188 # convert eols
188 source = Utilities.convertLineEnds(source, "\n") 189 source = Utilities.convertLineEnds(source, "\n")
189 except (UnicodeError, IOError) as msg: 190 except (UnicodeError, IOError) as msg:
190 self.noResults = False 191 self.noResults = False
191 self.__createResultItem(file, "1", 0, 192 self.__createResultItem(file, "1", 0,
192 self.trUtf8("Error: {0}").format(str(msg))\ 193 self.trUtf8("Error: {0}").format(str(msg))\
193 .rstrip()[1:-1], "") 194 .rstrip()[1:-1], "")
194 progress += 1 195 progress += 1
195 continue 196 continue
196 197
197 flags = Utilities.extractFlags(source) 198 flags = Utilities.extractFlags(source)
198 ext = os.path.splitext(file)[1] 199 ext = os.path.splitext(file)[1]
199 if ("FileType" in flags and 200 if ("FileType" in flags and
200 flags["FileType"] in ["Python", "Python2"]) or \ 201 flags["FileType"] in ["Python", "Python2"]) or \
201 file in py2files or \ 202 file in py2files or \
202 (ext in [".py", ".pyw"] and \ 203 (ext in [".py", ".pyw"] and \
203 Preferences.getProject("DeterminePyFromProject") and \ 204 Preferences.getProject("DeterminePyFromProject") and \
204 self.__project.isOpen() and \ 205 self.__project.isOpen() and \
205 self.__project.isProjectFile(file) and \ 206 self.__project.isProjectFile(file) and \
206 self.__project.getProjectLanguage() in ["Python", 207 self.__project.getProjectLanguage() in ["Python",
207 "Python2"]): 208 "Python2"]):
208 isPy3 = False 209 isPy3 = False
209 nok, fname, line, index, code, error, warnings = \ 210 nok, fname, line, index, code, error, warnings = \
210 Utilities.py2compile(file, 211 Utilities.py2compile(file,
211 checkFlakes = \ 212 checkFlakes=Preferences.getFlakes("IncludeInSyntaxCheck"))
212 Preferences.getFlakes("IncludeInSyntaxCheck"))
213 else: 213 else:
214 isPy3 = True 214 isPy3 = True
215 nok, fname, line, index, code, error = \ 215 nok, fname, line, index, code, error = \
216 Utilities.compile(file, source) 216 Utilities.compile(file, source)
217 if nok: 217 if nok:
221 if Preferences.getFlakes("IncludeInSyntaxCheck"): 221 if Preferences.getFlakes("IncludeInSyntaxCheck"):
222 if isPy3: 222 if isPy3:
223 try: 223 try:
224 sourceLines = source.splitlines() 224 sourceLines = source.splitlines()
225 warnings = Checker(source, file) 225 warnings = Checker(source, file)
226 warnings.messages.sort(key = lambda a: a.lineno) 226 warnings.messages.sort(key=lambda a: a.lineno)
227 for warning in warnings.messages: 227 for warning in warnings.messages:
228 if ignoreStarImportWarnings and \ 228 if ignoreStarImportWarnings and \
229 isinstance(warning, ImportStarUsed): 229 isinstance(warning, ImportStarUsed):
230 continue 230 continue
231 fname, lineno, message = \ 231 fname, lineno, message = \
232 warning.getMessageData() 232 warning.getMessageData()
233 if not sourceLines[lineno - 1].strip()\ 233 if not sourceLines[lineno - 1].strip()\
234 .endswith("__IGNORE_WARNING__"): 234 .endswith("__IGNORE_WARNING__"):
235 self.noResults = False 235 self.noResults = False
236 self.__createResultItem( 236 self.__createResultItem(
237 fname, lineno, 0, message, "", 237 fname, lineno, 0, message, "",
238 isWarning = True) 238 isWarning=True)
239 except SyntaxError as err: 239 except SyntaxError as err:
240 if err.text.strip(): 240 if err.text.strip():
241 msg = err.text.strip() 241 msg = err.text.strip()
242 else: 242 else:
243 msg = err.msg 243 msg = err.msg
245 err.filename, err.lineno, 0, msg, "") 245 err.filename, err.lineno, 0, msg, "")
246 else: 246 else:
247 for warning in warnings: 247 for warning in warnings:
248 self.noResults = False 248 self.noResults = False
249 self.__createResultItem( 249 self.__createResultItem(
250 warning[0], int(warning[1]), 0, 250 warning[0], int(warning[1]), 0,
251 warning[2], "", isWarning = True) 251 warning[2], "", isWarning=True)
252 progress += 1 252 progress += 1
253 self.checkProgress.setValue(progress) 253 self.checkProgress.setValue(progress)
254 QApplication.processEvents() 254 QApplication.processEvents()
255 self.__resort() 255 self.__resort()
256 else: 256 else:
300 300
301 filterString = self.excludeFilesEdit.text() 301 filterString = self.excludeFilesEdit.text()
302 if "ExcludeFiles" not in self.__data or \ 302 if "ExcludeFiles" not in self.__data or \
303 filterString != self.__data["ExcludeFiles"]: 303 filterString != self.__data["ExcludeFiles"]:
304 self.__data["ExcludeFiles"] = filterString 304 self.__data["ExcludeFiles"] = filterString
305 self.__project.setData("CHECKERSPARMS", "SyntaxChecker", 305 self.__project.setData("CHECKERSPARMS", "SyntaxChecker",
306 self.__data) 306 self.__data)
307 filterList = [f.strip() for f in filterString.split(",") 307 filterList = [f.strip() for f in filterString.split(",")
308 if f.strip()] 308 if f.strip()]
309 if filterList: 309 if filterList:
310 for filter in filterList: 310 for filter in filterList:
311 fileList = \ 311 fileList = \
312 [f for f in fileList if not fnmatch.fnmatch(f, filter)] 312 [f for f in fileList if not fnmatch.fnmatch(f, filter)]
316 self.cancelled = False 316 self.cancelled = False
317 self.start(fileList) 317 self.start(fileList)
318 318
319 def on_resultList_itemActivated(self, itm, col): 319 def on_resultList_itemActivated(self, itm, col):
320 """ 320 """
321 Private slot to handle the activation of an item. 321 Private slot to handle the activation of an item.
322 322
323 @param itm reference to the activated item (QTreeWidgetItem) 323 @param itm reference to the activated item (QTreeWidgetItem)
324 @param col column the item was activated in (integer) 324 @param col column the item was activated in (integer)
325 """ 325 """
326 if self.noResults: 326 if self.noResults:
351 for index in range(self.resultList.topLevelItemCount()): 351 for index in range(self.resultList.topLevelItemCount()):
352 itm = self.resultList.topLevelItem(index) 352 itm = self.resultList.topLevelItem(index)
353 fn = Utilities.normabspath(itm.data(0, self.filenameRole)) 353 fn = Utilities.normabspath(itm.data(0, self.filenameRole))
354 vm.openSourceFile(fn, 1) 354 vm.openSourceFile(fn, 1)
355 355
356 # go through the list again to clear syntax error and 356 # go through the list again to clear syntax error and
357 # py3flakes warning markers for files, that are ok 357 # py3flakes warning markers for files, that are ok
358 openFiles = vm.getOpenFilenames() 358 openFiles = vm.getOpenFilenames()
359 errorFiles = [] 359 errorFiles = []
360 for index in range(self.resultList.topLevelItemCount()): 360 for index in range(self.resultList.topLevelItemCount()):
361 itm = self.resultList.topLevelItem(index) 361 itm = self.resultList.topLevelItem(index)

eric ide

mercurial