Plugins/CheckerPlugins/Pep8/Pep8Dialog.py

changeset 2862
a1448560d7dc
parent 2799
ec8c717e80f5
child 2863
62171fa4a6a4
equal deleted inserted replaced
2861:cdcbca0cea82 2862:a1448560d7dc
23 import UI.PixmapCache 23 import UI.PixmapCache
24 import Preferences 24 import Preferences
25 import Utilities 25 import Utilities
26 26
27 27
28 class Pep8Report(pep8.BaseReport):
29 """
30 Class implementing a special report to be used with our dialog.
31 """
32 def __init__(self, options):
33 """
34 Constructor
35
36 @param options options for the report (optparse.Values)
37 """
38 super().__init__(options)
39
40 self.__repeat = options.repeat
41 self.errors = []
42
43 def error_args(self, line_number, offset, code, check, *args):
44 """
45 Public method to collect the error messages.
46
47 @param line_number line number of the issue (integer)
48 @param offset position within line of the issue (integer)
49 @param code message code (string)
50 @param check reference to the checker function (function)
51 @param args arguments for the message (list)
52 """
53 code = super().error_args(line_number, offset, code, check, *args)
54 if code and (self.counters[code] == 1 or self.__repeat):
55 text = pep8.getMessage(code, *args)
56 self.errors.append(
57 (self.filename, line_number, offset, text)
58 )
59 return code
60
28 class Pep8Dialog(QDialog, Ui_Pep8Dialog): 61 class Pep8Dialog(QDialog, Ui_Pep8Dialog):
29 """ 62 """
30 Class implementing a dialog to show the results of the PEP 8 check. 63 Class implementing a dialog to show the results of the PEP 8 check.
31 """ 64 """
32 filenameRole = Qt.UserRole + 1 65 filenameRole = Qt.UserRole + 1
305 from .Pep8Checker import Pep8Py2Checker 338 from .Pep8Checker import Pep8Py2Checker
306 checker = Pep8Py2Checker(file, [], 339 checker = Pep8Py2Checker(file, [],
307 repeat=repeatMessages, 340 repeat=repeatMessages,
308 select=includeMessages, 341 select=includeMessages,
309 ignore=excludeMessages) 342 ignore=excludeMessages)
343 checker.messages.sort(key=lambda a: a[1])
344 messages = checker.messages
310 else: 345 else:
311 from .Pep8Checker import Pep8Checker 346 checker = None # TODO: remove when Py2 is done
312 checker = Pep8Checker(file, source, 347 if includeMessages:
348 select = [s.strip() for s in includeMessages.split(',')
349 if s.strip()]
350 else:
351 select = []
352 if excludeMessages:
353 ignore = [i.strip() for i in excludeMessages.split(',')
354 if i.strip()]
355 else:
356 ignore = []
357 styleGuide = pep8.StyleGuide(
358 reporter=Pep8Report,
313 repeat=repeatMessages, 359 repeat=repeatMessages,
314 select=includeMessages, 360 select=select,
315 ignore=excludeMessages) 361 ignore=ignore,
316 checker.check_all() 362 max_line_length=79, # TODO: make configurable
317 checker.messages.sort(key=lambda a: a[1]) 363 )
318 for message in checker.messages: 364 report = styleGuide.check_files([file])
365 report.errors.sort(key=lambda a: a[1])
366 messages = report.errors
367 for message in messages:
319 fname, lineno, position, text = message 368 fname, lineno, position, text = message
320 if lineno > len(source): 369 if lineno > len(source):
321 lineno = len(source) 370 lineno = len(source)
322 if "__IGNORE_WARNING__" not in Utilities.extractLineFlags( 371 if "__IGNORE_WARNING__" not in Utilities.extractLineFlags(
323 source[lineno - 1].strip()): 372 source[lineno - 1].strip()):
329 text += "\n" + \ 378 text += "\n" + \
330 self.trUtf8("Fix: {0}").format(msg) 379 self.trUtf8("Fix: {0}").format(msg)
331 self.__createResultItem( 380 self.__createResultItem(
332 fname, lineno, position, text, fixed) 381 fname, lineno, position, text, fixed)
333 fixer and fixer.saveFile(encoding) 382 fixer and fixer.saveFile(encoding)
334 self.__updateStatistics(checker.statistics, fixer) 383 if checker:
384 # TODO: remove when Py2 is done
385 self.__updateStatistics(checker.statistics, fixer)
386 else:
387 self.__updateStatistics(report.counters, fixer)
335 progress += 1 388 progress += 1
336 finally: 389 finally:
337 # reenable updates of the list 390 # reenable updates of the list
338 self.resultList.setSortingEnabled(True) 391 self.resultList.setSortingEnabled(True)
339 self.resultList.setUpdatesEnabled(True) 392 self.resultList.setUpdatesEnabled(True)

eric ide

mercurial