Sat, 06 Aug 2022 17:37:06 +0200
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
9214 | 1 | # -*- coding: utf-8 -*- |
2 | ||
3 | # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> | |
4 | # | |
5 | ||
6 | """ | |
7 | Module implementing a dialog showing the code formatting progress and the result. | |
8 | """ | |
9 | ||
10 | import copy | |
11 | import datetime | |
12 | import pathlib | |
13 | ||
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
14 | from dataclasses import dataclass |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
15 | |
9214 | 16 | import black |
17 | ||
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
18 | from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QCoreApplication, QObject |
9214 | 19 | from PyQt6.QtWidgets import ( |
20 | QAbstractButton, | |
21 | QDialog, | |
22 | QDialogButtonBox, | |
23 | QHeaderView, | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
24 | QTreeWidgetItem, |
9214 | 25 | ) |
26 | ||
27 | from EricWidgets import EricMessageBox | |
28 | ||
29 | from .Ui_BlackFormattingDialog import Ui_BlackFormattingDialog | |
30 | ||
31 | from . import BlackUtilities | |
32 | from .BlackDiffWidget import BlackDiffWidget | |
33 | from .BlackFormattingAction import BlackFormattingAction | |
34 | ||
35 | import Utilities | |
36 | ||
37 | ||
38 | class BlackFormattingDialog(QDialog, Ui_BlackFormattingDialog): | |
39 | """ | |
40 | Class implementing a dialog showing the code formatting progress and the result. | |
41 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
42 | |
9214 | 43 | DataTypeRole = Qt.ItemDataRole.UserRole |
44 | DataRole = Qt.ItemDataRole.UserRole + 1 | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
45 | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
46 | StatusColumn = 0 |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
47 | FileNameColumn = 1 |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
48 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
49 | def __init__( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
50 | self, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
51 | configuration, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
52 | filesList, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
53 | project=None, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
54 | action=BlackFormattingAction.Format, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
55 | parent=None, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
56 | ): |
9214 | 57 | """ |
58 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
59 | |
9214 | 60 | @param configuration dictionary containing the configuration parameters |
61 | @type dict | |
62 | @param filesList list of absolute file paths to be processed | |
63 | @type list of str | |
64 | @param project reference to the project object (defaults to None) | |
65 | @type Project (optional) | |
66 | @param action action to be performed (defaults to BlackFormattingAction.Format) | |
67 | @type BlackFormattingAction (optional) | |
68 | @param parent reference to the parent widget (defaults to None) | |
69 | @type QWidget (optional) | |
70 | """ | |
71 | super().__init__(parent) | |
72 | self.setupUi(self) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
73 | |
9214 | 74 | self.progressBar.setMaximum(len(filesList)) |
75 | self.progressBar.setValue(0) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
76 | |
9214 | 77 | self.resultsList.header().setSortIndicator(1, Qt.SortOrder.AscendingOrder) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
78 | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
79 | self.statisticsGroup.setVisible(False) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
80 | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
81 | self.__statistics = BlackStatistics() |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
82 | |
9214 | 83 | self.__report = BlackReport(self) |
84 | self.__report.check = action is BlackFormattingAction.Check | |
85 | self.__report.diff = action is BlackFormattingAction.Diff | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
86 | self.__report.result.connect(self.__handleBlackFormattingResult) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
87 | |
9214 | 88 | self.__config = copy.deepcopy(configuration) |
89 | self.__project = project | |
90 | self.__action = action | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
91 | |
9214 | 92 | self.__cancelled = False |
93 | self.__diffDialog = None | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
94 | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
95 | self.__allFilter = self.tr("<all>") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
96 | |
9214 | 97 | self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(True) |
98 | self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(False) | |
99 | self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault(True) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
100 | |
9214 | 101 | self.show() |
102 | QCoreApplication.processEvents() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
103 | |
9214 | 104 | self.__files = self.__filterFiles(filesList) |
105 | self.__formatFiles() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
106 | |
9214 | 107 | def __filterFiles(self, filesList): |
108 | """ | |
109 | Private method to filter the given list of files according the | |
110 | configuration parameters. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
111 | |
9214 | 112 | @param filesList list of files |
113 | @type list of str | |
114 | @return list of filtered files | |
115 | @rtype list of str | |
116 | """ | |
117 | filterRegExps = [ | |
118 | BlackUtilities.compileRegExp(self.__config[k]) | |
119 | for k in ["force-exclude", "extend-exclude", "exclude"] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
120 | if k in self.__config |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
121 | and bool(self.__config[k]) |
9214 | 122 | and BlackUtilities.validateRegExp(self.__config[k])[0] |
123 | ] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
124 | |
9214 | 125 | files = [] |
126 | for file in filesList: | |
127 | file = Utilities.fromNativeSeparators(file) | |
128 | for filterRegExp in filterRegExps: | |
129 | filterMatch = filterRegExp.search(file) | |
130 | if filterMatch and filterMatch.group(0): | |
131 | self.__report.path_ignored(file) | |
132 | break | |
133 | else: | |
134 | files.append(file) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
135 | |
9214 | 136 | return files |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
137 | |
9214 | 138 | def __resort(self): |
139 | """ | |
140 | Private method to resort the result list. | |
141 | """ | |
142 | self.resultsList.sortItems( | |
143 | self.resultsList.sortColumn(), | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
144 | self.resultsList.header().sortIndicatorOrder(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
145 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
146 | |
9214 | 147 | def __resizeColumns(self): |
148 | """ | |
149 | Private method to resize the columns of the result list. | |
150 | """ | |
151 | self.resultsList.header().resizeSections( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
152 | QHeaderView.ResizeMode.ResizeToContents |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
153 | ) |
9214 | 154 | self.resultsList.header().setStretchLastSection(True) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
155 | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
156 | def __populateStatusFilterCombo(self): |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
157 | """ |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
158 | Private method to populate the status filter combo box with allowed selections. |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
159 | """ |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
160 | allowedSelections = set() |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
161 | for row in range(self.resultsList.topLevelItemCount()): |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
162 | allowedSelections.add( |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
163 | self.resultsList.topLevelItem(row).text( |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
164 | BlackFormattingDialog.StatusColumn |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
165 | ) |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
166 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
167 | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
168 | self.statusFilterComboBox.addItem(self.__allFilter) |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
169 | self.statusFilterComboBox.addItems(sorted(allowedSelections)) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
170 | |
9214 | 171 | def __finish(self): |
172 | """ | |
173 | Private method to perform some actions after the run was performed or canceled. | |
174 | """ | |
175 | self.__resort() | |
176 | self.__resizeColumns() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
177 | |
9214 | 178 | self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(False) |
179 | self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True) | |
180 | self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
181 | |
9214 | 182 | self.progressBar.setVisible(False) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
183 | |
9214 | 184 | self.__updateStatistics() |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
185 | self.__populateStatusFilterCombo() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
186 | |
9214 | 187 | def __updateStatistics(self): |
188 | """ | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
189 | Private method to update the statistics about the recent formatting run and |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
190 | make them visible. |
9214 | 191 | """ |
192 | self.reformattedLabel.setText( | |
193 | self.tr("reformatted") | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
194 | if self.__action is BlackFormattingAction.Format |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
195 | else self.tr("would reformat") |
9214 | 196 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
197 | |
9214 | 198 | total = self.progressBar.maximum() |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
199 | processed = total - self.__statistics.ignoreCount |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
200 | |
9214 | 201 | self.totalCountLabel.setText("{0:n}".format(total)) |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
202 | self.excludedCountLabel.setText("{0:n}".format(self.__statistics.ignoreCount)) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
203 | self.failuresCountLabel.setText("{0:n}".format(self.__statistics.failureCount)) |
9214 | 204 | self.processedCountLabel.setText("{0:n}".format(processed)) |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
205 | self.reformattedCountLabel.setText( |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
206 | "{0:n}".format(self.__statistics.changeCount) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
207 | ) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
208 | self.unchangedCountLabel.setText("{0:n}".format(self.__statistics.sameCount)) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
209 | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
210 | self.statisticsGroup.setVisible(True) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
211 | |
9214 | 212 | @pyqtSlot(QAbstractButton) |
213 | def on_buttonBox_clicked(self, button): | |
214 | """ | |
215 | Private slot to handle button presses of the dialog buttons. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
216 | |
9214 | 217 | @param button reference to the pressed button |
218 | @type QAbstractButton | |
219 | """ | |
220 | if button == self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel): | |
221 | self.__cancelled = True | |
222 | elif button == self.buttonBox.button(QDialogButtonBox.StandardButton.Close): | |
223 | self.accept() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
224 | |
9214 | 225 | @pyqtSlot(QTreeWidgetItem, int) |
226 | def on_resultsList_itemDoubleClicked(self, item, column): | |
227 | """ | |
228 | Private slot handling a double click of a result item. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
229 | |
9214 | 230 | @param item reference to the double clicked item |
231 | @type QTreeWidgetItem | |
232 | @param column column number that was double clicked | |
233 | @type int | |
234 | """ | |
235 | dataType = item.data(0, BlackFormattingDialog.DataTypeRole) | |
236 | if dataType == "error": | |
237 | EricMessageBox.critical( | |
238 | self, | |
239 | self.tr("Formatting Failure"), | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
240 | self.tr("<p>Formatting failed due to this error.</p><p>{0}</p>").format( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
241 | item.data(0, BlackFormattingDialog.DataRole) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
242 | ), |
9214 | 243 | ) |
244 | elif dataType == "diff": | |
245 | if self.__diffDialog is None: | |
246 | self.__diffDialog = BlackDiffWidget() | |
247 | self.__diffDialog.showDiff(item.data(0, BlackFormattingDialog.DataRole)) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
248 | |
9214 | 249 | def __formatFiles(self): |
250 | """ | |
251 | Private method to format the list of files according the configuration. | |
252 | """ | |
253 | writeBack = black.WriteBack.from_configuration( | |
254 | check=self.__action is BlackFormattingAction.Check, | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
255 | diff=self.__action is BlackFormattingAction.Diff, |
9214 | 256 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
257 | |
9214 | 258 | versions = ( |
259 | { | |
260 | black.TargetVersion[target.upper()] | |
261 | for target in self.__config["target-version"] | |
262 | } | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
263 | if self.__config["target-version"] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
264 | else set() |
9214 | 265 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
266 | |
9214 | 267 | mode = black.Mode( |
268 | target_versions=versions, | |
269 | line_length=int(self.__config["line-length"]), | |
270 | string_normalization=not self.__config["skip-string-normalization"], | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
271 | magic_trailing_comma=not self.__config["skip-magic-trailing-comma"], |
9214 | 272 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
273 | |
9214 | 274 | for file in self.__files: |
275 | if self.__action is BlackFormattingAction.Diff: | |
276 | self.__diffFormatFile( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
277 | pathlib.Path(file), fast=False, mode=mode, report=self.__report |
9214 | 278 | ) |
279 | else: | |
280 | black.reformat_one( | |
281 | pathlib.Path(file), | |
282 | fast=False, | |
283 | write_back=writeBack, | |
284 | mode=mode, | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
285 | report=self.__report, |
9214 | 286 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
287 | |
9214 | 288 | if self.__cancelled: |
289 | break | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
290 | |
9214 | 291 | self.__finish() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
292 | |
9214 | 293 | def __diffFormatFile(self, src, fast, mode, report): |
294 | """ | |
295 | Private method to check, if the given files need to be reformatted, and generate | |
296 | a unified diff. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
297 | |
9214 | 298 | @param src path of file to be checked |
299 | @type pathlib.Path | |
300 | @param fast flag indicating fast operation | |
301 | @type bool | |
302 | @param mode code formatting options | |
303 | @type black.Mode | |
304 | @param report reference to the report object | |
305 | @type BlackReport | |
306 | """ | |
307 | then = datetime.datetime.utcfromtimestamp(src.stat().st_mtime) | |
308 | with open(src, "rb") as buf: | |
309 | srcContents, _, _ = black.decode_bytes(buf.read()) | |
310 | try: | |
311 | dstContents = black.format_file_contents(srcContents, fast=fast, mode=mode) | |
312 | except black.NothingChanged: | |
313 | report.done(src, black.Changed.NO) | |
314 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
315 | |
9214 | 316 | fileName = str(src) |
317 | if self.__project: | |
318 | fileName = self.__project.getRelativePath(fileName) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
319 | |
9214 | 320 | now = datetime.datetime.utcnow() |
321 | srcName = f"{fileName}\t{then} +0000" | |
322 | dstName = f"{fileName}\t{now} +0000" | |
323 | diffContents = black.diff(srcContents, dstContents, srcName, dstName) | |
324 | report.done(src, black.Changed.YES, diff=diffContents) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
325 | |
9214 | 326 | def closeEvent(self, evt): |
327 | """ | |
328 | Protected slot implementing a close event handler. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
329 | |
9214 | 330 | @param evt reference to the close event |
331 | @type QCloseEvent | |
332 | """ | |
333 | if self.__diffDialog is not None: | |
334 | self.__diffDialog.close() | |
335 | evt.accept() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
336 | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
337 | @pyqtSlot(str) |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
338 | def on_statusFilterComboBox_currentTextChanged(self, status): |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
339 | """ |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
340 | Private slot handling the selection of a status for items to be shown. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
341 | |
9220
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
342 | @param status selected status |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
343 | @type str |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
344 | """ |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
345 | for row in range(self.resultsList.topLevelItemCount()): |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
346 | itm = self.resultsList.topLevelItem(row) |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
347 | itm.setHidden( |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
348 | status != self.__allFilter |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
349 | and itm.text(BlackFormattingDialog.StatusColumn) != status |
e9e7eca7efee
Black Formatting Dialog
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
350 | ) |
9214 | 351 | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
352 | @pyqtSlot(str, str, str) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
353 | def __handleBlackFormattingResult(self, status, filename, data): |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
354 | """ |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
355 | Private slot to handle the result of a black reformatting action. |
9214 | 356 | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
357 | @param status status of the performed action (one of 'changed', 'unchanged', |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
358 | 'unmodified', 'failed' or 'ignored') |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
359 | @type str |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
360 | @param filename name of the processed file |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
361 | @type str |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
362 | @param data action data (error message or unified diff) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
363 | @type str |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
364 | """ |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
365 | isError = False |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
366 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
367 | if status == "changed": |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
368 | statusMsg = ( |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
369 | self.tr("would reformat") |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
370 | if self.__action |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
371 | in (BlackFormattingAction.Check, BlackFormattingAction.Diff) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
372 | else self.tr("reformatted") |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
373 | ) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
374 | self.__statistics.changeCount += 1 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
375 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
376 | elif status == "unchanged": |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
377 | statusMsg = self.tr("unchanged") |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
378 | self.__statistics.sameCount += 1 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
379 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
380 | elif status == "unmodified": |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
381 | statusMsg = self.tr("unmodified") |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
382 | self.__statistics.sameCount += 1 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
383 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
384 | elif status == "ignored": |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
385 | statusMsg = self.tr("ignored") |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
386 | self.__statistics.ignoreCount += 1 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
387 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
388 | elif status == "failed": |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
389 | statusMsg = self.tr("failed") |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
390 | self.__statistics.failureCount += 1 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
391 | isError = True |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
392 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
393 | else: |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
394 | statusMsg = self.tr("invalid status ({0})").format(status) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
395 | self.__statistics.failureCount += 1 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
396 | isError = True |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
397 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
398 | if self.__project: |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
399 | filename = self.__project.getRelativePath(filename) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
400 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
401 | itm = QTreeWidgetItem(self.resultsList, [statusMsg, filename]) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
402 | if data: |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
403 | itm.setData( |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
404 | 0, BlackFormattingDialog.DataTypeRole, "error" if isError else "diff" |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
405 | ) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
406 | itm.setData(0, BlackFormattingDialog.DataRole, data) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
407 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
408 | self.progressBar.setValue(self.progressBar.value() + 1) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
409 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
410 | QCoreApplication.processEvents() |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
411 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
412 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
413 | @dataclass |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
414 | class BlackStatistics: |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
415 | """ |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
416 | Class containing the reformatting statistic data. |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
417 | """ |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
418 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
419 | ignoreCount: int = 0 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
420 | changeCount: int = 0 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
421 | sameCount: int = 0 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
422 | failureCount: int = 0 |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
423 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
424 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
425 | class BlackReport(QObject, black.Report): |
9214 | 426 | """ |
427 | Class extending the black Report to work with our dialog. | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
428 | |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
429 | @signal result(status, file name, data) emitted to signal the reformatting result |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
430 | as three strings giving the status (one of 'changed', 'unchanged', 'unmodified', |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
431 | 'failed' or 'ignored'), the file name and data related to the result |
9214 | 432 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
433 | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
434 | result = pyqtSignal(str, str, str) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
435 | |
9214 | 436 | def __init__(self, dialog): |
437 | """ | |
438 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
439 | |
9214 | 440 | @param dialog reference to the result dialog |
441 | @type QDialog | |
442 | """ | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
443 | QObject.__init__(self, dialog) |
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
444 | black.Report.__init__(self) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
445 | |
9214 | 446 | self.ignored_count = 0 |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
447 | |
9214 | 448 | self.__dialog = dialog |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
449 | |
9214 | 450 | def done(self, src, changed, diff=""): |
451 | """ | |
452 | Public method to handle the end of a reformat. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
453 | |
9214 | 454 | @param src name of the processed file |
455 | @type pathlib.Path | |
456 | @param changed change status | |
457 | @type black.Changed | |
458 | @param diff unified diff of potential changes (defaults to "") | |
459 | @type str | |
460 | """ | |
461 | if changed is black.Changed.YES: | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
462 | status = "changed" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
463 | |
9214 | 464 | elif changed is black.Changed.NO: |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
465 | status = "unchanged" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
466 | |
9214 | 467 | elif changed is black.Changed.CACHED: |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
468 | status = "unmodified" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
469 | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
470 | self.result.emit(status, str(src), diff) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
471 | |
9214 | 472 | def failed(self, src, message): |
473 | """ | |
474 | Public method to handle a reformat failure. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
475 | |
9214 | 476 | @param src name of the processed file |
477 | @type pathlib.Path | |
478 | @param message error message | |
479 | @type str | |
480 | """ | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
481 | self.result.emit("failed", str(src), message) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
482 | |
9214 | 483 | def path_ignored(self, src, message=""): |
484 | """ | |
485 | Public method handling an ignored path. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9220
diff
changeset
|
486 | |
9214 | 487 | @param src name of the processed file |
488 | @type pathlib.Path or str | |
489 | @param message ignore message (default to "") | |
490 | @type str (optional) | |
491 | """ | |
9281
76caf27cb8a8
Changed the code reformatting dialog to work with signals and slots and do the statistics calculation in the dialog class (prepare for multi processing).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
492 | self.result.emit("ignored", str(src), "") |