Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing). eric7

Sat, 06 Aug 2022 17:37:06 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 06 Aug 2022 17:37:06 +0200
branch
eric7
changeset 9281
76caf27cb8a8
parent 9280
94f4e2751790
child 9282
c44b7aa4cf71

Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).

src/eric7/CodeFormatting/BlackFormattingDialog.py file | annotate | diff | comparison | revisions
--- a/src/eric7/CodeFormatting/BlackFormattingDialog.py	Fri Aug 05 15:07:54 2022 +0200
+++ b/src/eric7/CodeFormatting/BlackFormattingDialog.py	Sat Aug 06 17:37:06 2022 +0200
@@ -11,9 +11,11 @@
 import datetime
 import pathlib
 
+from dataclasses import dataclass
+
 import black
 
-from PyQt6.QtCore import pyqtSlot, Qt, QCoreApplication
+from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QCoreApplication, QObject
 from PyQt6.QtWidgets import (
     QAbstractButton,
     QDialog,
@@ -76,9 +78,12 @@
 
         self.statisticsGroup.setVisible(False)
 
+        self.__statistics = BlackStatistics()
+
         self.__report = BlackReport(self)
         self.__report.check = action is BlackFormattingAction.Check
         self.__report.diff = action is BlackFormattingAction.Diff
+        self.__report.result.connect(self.__handleBlackFormattingResult)
 
         self.__config = copy.deepcopy(configuration)
         self.__project = project
@@ -191,14 +196,16 @@
         )
 
         total = self.progressBar.maximum()
-        processed = total - self.__report.ignored_count
+        processed = total - self.__statistics.ignoreCount
 
         self.totalCountLabel.setText("{0:n}".format(total))
-        self.excludedCountLabel.setText("{0:n}".format(self.__report.ignored_count))
-        self.failuresCountLabel.setText("{0:n}".format(self.__report.failure_count))
+        self.excludedCountLabel.setText("{0:n}".format(self.__statistics.ignoreCount))
+        self.failuresCountLabel.setText("{0:n}".format(self.__statistics.failureCount))
         self.processedCountLabel.setText("{0:n}".format(processed))
-        self.reformattedCountLabel.setText("{0:n}".format(self.__report.change_count))
-        self.unchangedCountLabel.setText("{0:n}".format(self.__report.same_count))
+        self.reformattedCountLabel.setText(
+            "{0:n}".format(self.__statistics.changeCount)
+        )
+        self.unchangedCountLabel.setText("{0:n}".format(self.__statistics.sameCount))
 
         self.statisticsGroup.setVisible(True)
 
@@ -239,34 +246,6 @@
                 self.__diffDialog = BlackDiffWidget()
             self.__diffDialog.showDiff(item.data(0, BlackFormattingDialog.DataRole))
 
-    def addResultEntry(self, status, fileName, isError=False, data=None):
-        """
-        Public method to add an entry to the result list.
-
-        @param status status of the operation
-        @type str
-        @param fileName name of the processed file
-        @type str
-        @param isError flag indicating that data contains an error message (defaults to
-            False)
-        @type bool (optional)
-        @param data associated data (diff or error message) (defaults to None)
-        @type str (optional)
-        """
-        if self.__project:
-            fileName = self.__project.getRelativePath(fileName)
-
-        itm = QTreeWidgetItem(self.resultsList, [status, fileName])
-        if data:
-            itm.setData(
-                0, BlackFormattingDialog.DataTypeRole, "error" if isError else "diff"
-            )
-            itm.setData(0, BlackFormattingDialog.DataRole, data)
-
-        self.progressBar.setValue(self.progressBar.value() + 1)
-
-        QCoreApplication.processEvents()
-
     def __formatFiles(self):
         """
         Private method to format the list of files according the configuration.
@@ -370,12 +349,90 @@
                 and itm.text(BlackFormattingDialog.StatusColumn) != status
             )
 
+    @pyqtSlot(str, str, str)
+    def __handleBlackFormattingResult(self, status, filename, data):
+        """
+        Private slot to handle the result of a black reformatting action.
 
-class BlackReport(black.Report):
+        @param status status of the performed action (one of 'changed', 'unchanged',
+            'unmodified', 'failed' or 'ignored')
+        @type str
+        @param filename name of the processed file
+        @type str
+        @param data action data (error message or unified diff)
+        @type str
+        """
+        isError = False
+
+        if status == "changed":
+            statusMsg = (
+                self.tr("would reformat")
+                if self.__action
+                in (BlackFormattingAction.Check, BlackFormattingAction.Diff)
+                else self.tr("reformatted")
+            )
+            self.__statistics.changeCount += 1
+
+        elif status == "unchanged":
+            statusMsg = self.tr("unchanged")
+            self.__statistics.sameCount += 1
+
+        elif status == "unmodified":
+            statusMsg = self.tr("unmodified")
+            self.__statistics.sameCount += 1
+
+        elif status == "ignored":
+            statusMsg = self.tr("ignored")
+            self.__statistics.ignoreCount += 1
+
+        elif status == "failed":
+            statusMsg = self.tr("failed")
+            self.__statistics.failureCount += 1
+            isError = True
+
+        else:
+            statusMsg = self.tr("invalid status ({0})").format(status)
+            self.__statistics.failureCount += 1
+            isError = True
+
+        if self.__project:
+            filename = self.__project.getRelativePath(filename)
+
+        itm = QTreeWidgetItem(self.resultsList, [statusMsg, filename])
+        if data:
+            itm.setData(
+                0, BlackFormattingDialog.DataTypeRole, "error" if isError else "diff"
+            )
+            itm.setData(0, BlackFormattingDialog.DataRole, data)
+
+        self.progressBar.setValue(self.progressBar.value() + 1)
+
+        QCoreApplication.processEvents()
+
+
+@dataclass
+class BlackStatistics:
+    """
+    Class containing the reformatting statistic data.
+    """
+
+    ignoreCount: int = 0
+    changeCount: int = 0
+    sameCount: int = 0
+    failureCount: int = 0
+
+
+class BlackReport(QObject, black.Report):
     """
     Class extending the black Report to work with our dialog.
+
+    @signal result(status, file name, data) emitted to signal the reformatting result
+        as three strings giving the status (one of 'changed', 'unchanged', 'unmodified',
+        'failed' or 'ignored'), the file name and data related to the result
     """
 
+    result = pyqtSignal(str, str, str)
+
     def __init__(self, dialog):
         """
         Constructor
@@ -383,7 +440,8 @@
         @param dialog reference to the result dialog
         @type QDialog
         """
-        super().__init__()
+        QObject.__init__(self, dialog)
+        black.Report.__init__(self)
 
         self.ignored_count = 0
 
@@ -401,25 +459,15 @@
         @type str
         """
         if changed is black.Changed.YES:
-            status = (
-                QCoreApplication.translate("BlackFormattingDialog", "would reformat")
-                if self.check or self.diff
-                else QCoreApplication.translate("BlackFormattingDialog", "reformatted")
-            )
-            self.change_count += 1
+            status = "changed"
 
         elif changed is black.Changed.NO:
-            status = QCoreApplication.translate("BlackFormattingDialog", "unchanged")
-            self.same_count += 1
+            status = "unchanged"
 
         elif changed is black.Changed.CACHED:
-            status = QCoreApplication.translate("BlackFormattingDialog", "unmodified")
-            self.same_count += 1
+            status = "unmodified"
 
-        if self.diff:
-            self.__dialog.addResultEntry(status, str(src), data=diff)
-        else:
-            self.__dialog.addResultEntry(status, str(src))
+        self.result.emit(status, str(src), diff)
 
     def failed(self, src, message):
         """
@@ -430,10 +478,7 @@
         @param message error message
         @type str
         """
-        status = QCoreApplication.translate("BlackFormattingDialog", "failed")
-        self.failure_count += 1
-
-        self.__dialog.addResultEntry(status, str(src), isError=True, data=message)
+        self.result.emit("failed", str(src), message)
 
     def path_ignored(self, src, message=""):
         """
@@ -444,7 +489,4 @@
         @param message ignore message (default to "")
         @type str (optional)
         """
-        status = QCoreApplication.translate("BlackFormattingDialog", "ignored")
-        self.ignored_count += 1
-
-        self.__dialog.addResultEntry(status, str(src))
+        self.result.emit("ignored", str(src), "")

eric ide

mercurial