Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py

changeset 4218
f542ad1f76c5
parent 4217
38e8903f9c2f
child 4221
c9fdc07753a7
equal deleted inserted replaced
4217:38e8903f9c2f 4218:f542ad1f76c5
26 import Utilities 26 import Utilities
27 27
28 from . import pep8 28 from . import pep8
29 29
30 30
31 # TODO: implement CANCEL for batch jobs (if possible)
31 class CodeStyleCheckerDialog(QDialog, Ui_CodeStyleCheckerDialog): 32 class CodeStyleCheckerDialog(QDialog, Ui_CodeStyleCheckerDialog):
32 """ 33 """
33 Class implementing a dialog to show the results of the code style check. 34 Class implementing a dialog to show the results of the code style check.
34 """ 35 """
35 filenameRole = Qt.UserRole + 1 36 filenameRole = Qt.UserRole + 1
83 self.checkProgressLabel.setVisible(False) 84 self.checkProgressLabel.setVisible(False)
84 self.checkProgressLabel.setMaximumWidth(600) 85 self.checkProgressLabel.setMaximumWidth(600)
85 86
86 self.styleCheckService = styleCheckService 87 self.styleCheckService = styleCheckService
87 self.styleCheckService.styleChecked.connect(self.__processResult) 88 self.styleCheckService.styleChecked.connect(self.__processResult)
89 self.styleCheckService.batchFinished.connect(self.__batchFinished)
88 self.filename = None 90 self.filename = None
89 91
90 self.noResults = True 92 self.noResults = True
91 self.cancelled = False 93 self.cancelled = False
92 self.__lastFileItem = None 94 self.__lastFileItem = None
95 self.__finished = True
93 96
94 self.__fileOrFileList = "" 97 self.__fileOrFileList = ""
95 self.__project = None 98 self.__project = None
96 self.__forProject = False 99 self.__forProject = False
97 self.__data = {} 100 self.__data = {}
363 hangClosing, docType] 366 hangClosing, docType]
364 367
365 # now go through all the files 368 # now go through all the files
366 self.progress = 0 369 self.progress = 0
367 self.files.sort() 370 self.files.sort()
368 self.check() 371 if len(self.files) == 1:
372 self.__batch = False
373 self.check()
374 else:
375 self.__batch = True
376 self.checkBatch()
369 377
370 def check(self, codestring=''): 378 def check(self, codestring=''):
371 """ 379 """
372 Public method to start a style check for one file. 380 Public method to start a style check for one file.
373 381
423 431
424 eol = self.__getEol(self.filename) 432 eol = self.__getEol(self.filename)
425 args = self.__options + [ 433 args = self.__options + [
426 errors, eol, encoding, Preferences.getEditor("CreateBackupFile") 434 errors, eol, encoding, Preferences.getEditor("CreateBackupFile")
427 ] 435 ]
436 self.__finished = False
428 self.styleCheckService.styleCheck( 437 self.styleCheckService.styleCheck(
429 None, self.filename, source, args) 438 None, self.filename, source, args)
430 439
440 def checkBatch(self):
441 """
442 Public method to start a style check batch job.
443
444 The results are reported to the __processResult slot.
445 """
446 self.__lastFileItem = None
447
448 # Cancel doesn't work for batch jobs
449 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False)
450
451 self.checkProgressLabel.setPath(self.tr("Preparing files..."))
452 progress = 0
453
454 argumentsList = []
455 for filename in self.files:
456 progress += 1
457 self.checkProgress.setValue(progress)
458 QApplication.processEvents()
459
460 try:
461 source, encoding = Utilities.readEncodedFile(
462 filename)
463 source = source.splitlines(True)
464 except (UnicodeError, IOError) as msg:
465 self.noResults = False
466 self.__createResultItem(
467 filename, 1, 1,
468 self.tr("Error: {0}").format(str(msg))
469 .rstrip(), False, False, False)
470 continue
471
472 if encoding.endswith(
473 ('-selected', '-default', '-guessed', '-ignore')):
474 encoding = encoding.rsplit('-', 1)[0]
475
476 errors = []
477 self.__itms = []
478 for error, itm in self.__onlyFixes.pop(filename, []):
479 errors.append(error)
480 self.__itms.append(itm)
481
482 eol = self.__getEol(filename)
483 args = self.__options + [
484 errors, eol, encoding,
485 Preferences.getEditor("CreateBackupFile")
486 ]
487 argumentsList.append((filename, source, args))
488
489 # reset the progress bar to the checked files
490 self.checkProgress.setValue(self.progress)
491 QApplication.processEvents()
492
493 self.__finished = False
494 self.styleCheckService.styleBatchCheck(argumentsList)
495
496 def __batchFinished(self):
497 """
498 Private slot handling the completion of a batch job.
499 """
500 self.checkProgressLabel.setPath("")
501 self.checkProgress.setMaximum(1)
502 self.checkProgress.setValue(1)
503 self.__finish()
504
431 def __processResult(self, fn, codeStyleCheckerStats, fixes, results): 505 def __processResult(self, fn, codeStyleCheckerStats, fixes, results):
432 """ 506 """
433 Private slot called after perfoming a style check on one file. 507 Private slot called after perfoming a style check on one file.
434 508
435 @param fn filename of the just checked file (str) 509 @param fn filename of the just checked file (str)
437 @param fixes number of applied fixes (int) 511 @param fixes number of applied fixes (int)
438 @param results tuple for each found violation of style (tuple of 512 @param results tuple for each found violation of style (tuple of
439 lineno (int), position (int), text (str), ignored (bool), 513 lineno (int), position (int), text (str), ignored (bool),
440 fixed (bool), autofixing (bool)) 514 fixed (bool), autofixing (bool))
441 """ 515 """
442 # Check if it's the requested file, otherwise ignore signal 516 if self.__finished:
443 if fn != self.filename: 517 return
518
519 # Check if it's the requested file, otherwise ignore signal if not
520 # in batch mode
521 if not self.__batch and fn != self.filename:
444 return 522 return
445 523
446 # disable updates of the list for speed 524 # disable updates of the list for speed
447 self.resultList.setUpdatesEnabled(False) 525 self.resultList.setUpdatesEnabled(False)
448 self.resultList.setSortingEnabled(False) 526 self.resultList.setSortingEnabled(False)
453 for itm, (lineno, position, text, ignored, fixed, autofixing) in \ 531 for itm, (lineno, position, text, ignored, fixed, autofixing) in \
454 zip(self.__itms, results): 532 zip(self.__itms, results):
455 self.__modifyFixedResultItem(itm, text, fixed) 533 self.__modifyFixedResultItem(itm, text, fixed)
456 self.__updateFixerStatistics(fixes) 534 self.__updateFixerStatistics(fixes)
457 else: 535 else:
536 self.__lastFileItem = None
537
458 for lineno, position, text, ignored, fixed, autofixing in results: 538 for lineno, position, text, ignored, fixed, autofixing in results:
459 if ignored: 539 if ignored:
460 ignoredErrors += 1 540 ignoredErrors += 1
461 if self.showIgnored: 541 if self.showIgnored:
462 text = self.tr("{0} (ignored)").format(text) 542 text = self.tr("{0} (ignored)").format(text)
481 # reenable updates of the list 561 # reenable updates of the list
482 self.resultList.setSortingEnabled(True) 562 self.resultList.setSortingEnabled(True)
483 self.resultList.setUpdatesEnabled(True) 563 self.resultList.setUpdatesEnabled(True)
484 564
485 self.checkProgress.setValue(self.progress) 565 self.checkProgress.setValue(self.progress)
566 self.checkProgressLabel.setPath(fn)
486 QApplication.processEvents() 567 QApplication.processEvents()
487 568
488 self.check() 569 if not self.__batch:
570 self.check()
489 571
490 def __finish(self): 572 def __finish(self):
491 """ 573 """
492 Private slot called when the code style check finished or the user 574 Private slot called when the code style check finished or the user
493 pressed the cancel button. 575 pressed the cancel button.
494 """ 576 """
577 self.__finished = True
578
495 self.cancelled = True 579 self.cancelled = True
496 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) 580 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True)
497 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) 581 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False)
498 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) 582 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
499 self.statisticsButton.setEnabled(True) 583 self.statisticsButton.setEnabled(True)

eric ide

mercurial