--- a/ProjectFlask/FlaskMigrateExtension/MigrateSummaryDialog.py Thu Dec 30 12:13:22 2021 +0100 +++ b/ProjectFlask/FlaskMigrateExtension/MigrateSummaryDialog.py Wed Sep 21 16:30:15 2022 +0200 @@ -10,8 +10,11 @@ from PyQt6.QtCore import pyqtSlot, Qt, QProcess, QEventLoop, QTimer from PyQt6.QtGui import QGuiApplication from PyQt6.QtWidgets import ( - QDialog, QDialogButtonBox, QAbstractButton, QTreeWidgetItem, - QAbstractItemView + QDialog, + QDialogButtonBox, + QAbstractButton, + QTreeWidgetItem, + QAbstractItemView, ) from EricGui.EricOverrideCursor import EricOverrideCursor, EricOverridenCursor @@ -24,10 +27,11 @@ """ Class implementing a dialog showing a summary of all created.migrations. """ + def __init__(self, project, migrateProject, migrations="", parent=None): """ Constructor - + @param project reference to the project object @type Project @param migrateProject reference to the migrate project extension @@ -39,19 +43,20 @@ """ super().__init__(parent) self.setupUi(self) - + self.__refreshButton = self.buttonBox.addButton( - self.tr("Refresh"), QDialogButtonBox.ButtonRole.ActionRole) + self.tr("Refresh"), QDialogButtonBox.ButtonRole.ActionRole + ) self.__refreshButton.clicked.connect(self.showSummary) - + self.__project = project self.__migrateProject = migrateProject self.__migrations = migrations - + self.__process = None self.__currentItemIndex = 1000000 self.__currentRevision = "" - + def showSummary(self): """ Public method to show the migrations summary. @@ -60,37 +65,40 @@ if env is not None: self.show() self.raise_() - - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setEnabled(False) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setDefault(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setEnabled(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setFocus( - Qt.FocusReason.OtherFocusReason) + + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled( + False + ) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault( + True + ) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled( + True + ) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setFocus( + Qt.FocusReason.OtherFocusReason + ) QGuiApplication.processEvents( - QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) - + QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents + ) + command = self.__project.getFlaskCommand() - + self.__process = QProcess() self.__process.setProcessEnvironment(env) self.__process.setWorkingDirectory(workdir) - + args = ["db", "history", "--indicate-current"] if self.__migrations: args += ["--directory", self.__migrations] - + with EricOverrideCursor(): self.__process.start(command, args) ok = self.__process.waitForStarted(10000) if ok: ok = self.__process.waitForFinished(10000) if ok: - out = str(self.__process.readAllStandardOutput(), - "utf-8") + out = str(self.__process.readAllStandardOutput(), "utf-8") self.__processOutput(out) self.__selectItem(self.__currentRevision) else: @@ -98,33 +106,41 @@ EricMessageBox.critical( None, self.tr("Migrations Summary"), - self.tr("""The Flask process did not finish""" - """ within 10 seconds.""")) + self.tr( + """The Flask process did not finish""" + """ within 10 seconds.""" + ), + ) else: with EricOverridenCursor(): EricMessageBox.critical( None, self.tr("Migrations Summary"), - self.tr("""The Flask process could not be""" - """ started.""")) + self.tr( + """The Flask process could not be""" """ started.""" + ), + ) for column in range(self.summaryWidget.columnCount()): self.summaryWidget.resizeColumnToContents(column) - - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setEnabled(False) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setEnabled(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setDefault(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setFocus( - Qt.FocusReason.OtherFocusReason) - + + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled( + False + ) + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled( + True + ) + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault( + True + ) + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setFocus( + Qt.FocusReason.OtherFocusReason + ) + def __processOutput(self, output): """ Private method to process the flask output and populate the summary list. - + @param output output of the flask process @type str """ @@ -132,7 +148,7 @@ self.upDownButton.setEnabled(False) self.__currentItemIndex = 1000000 self.__currentRevision = "" - + lines = output.splitlines() for line in lines: isCurrent = False @@ -141,32 +157,32 @@ newRev, *labels = rest.split() if labels: labelList = [ - label.replace("(", "").replace(")", "") - for label in labels + label.replace("(", "").replace(")", "") for label in labels ] labelsStr = ", ".join(labelList) if "current" in labelList: isCurrent = True else: labelsStr = "" - - itm = QTreeWidgetItem(self.summaryWidget, [ - oldRev.strip(), - newRev.strip(), - message.strip(), - labelsStr, - ]) + + itm = QTreeWidgetItem( + self.summaryWidget, + [ + oldRev.strip(), + newRev.strip(), + message.strip(), + labelsStr, + ], + ) if isCurrent: font = itm.font(0) font.setBold(True) for column in range(self.summaryWidget.columnCount()): itm.setFont(column, font) - - self.__currentItemIndex = ( - self.summaryWidget.indexOfTopLevelItem(itm) - ) + + self.__currentItemIndex = self.summaryWidget.indexOfTopLevelItem(itm) self.__currentRevision = newRev.strip() - + @pyqtSlot() def on_summaryWidget_itemSelectionChanged(self): """ @@ -182,7 +198,7 @@ self.upDownButton.setEnabled(index != self.__currentItemIndex) else: self.upDownButton.setEnabled(False) - + @pyqtSlot() def on_upDownButton_clicked(self): """ @@ -195,47 +211,45 @@ else: self.__migrateProject.downgradeDatabase(revision=rev) self.showSummary() - + @pyqtSlot(QAbstractButton) def on_buttonBox_clicked(self, button): """ Private slot handling a button press of the button box. - + @param button reference to the pressed button @type QAbstractButton """ - if button is self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel - ): + if button is self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel): self.__cancelProcess() - + @pyqtSlot() def __cancelProcess(self): """ Private slot to terminate the current process. """ if ( - self.__process is not None and - self.__process.state() != QProcess.ProcessState.NotRunning + self.__process is not None + and self.__process.state() != QProcess.ProcessState.NotRunning ): self.__process.terminate() QTimer.singleShot(2000, self.__process.kill) self.__process.waitForFinished(3000) - + self.__process = None - + def __selectItem(self, revision): """ Private method to select an item given its revision. - + @param revision revision of the item to select @type str """ if revision: - items = self.summaryWidget.findItems( - revision, Qt.MatchFlag.MatchExactly, 1) + items = self.summaryWidget.findItems(revision, Qt.MatchFlag.MatchExactly, 1) if items: # select the first item items[0].setSelected(True) self.summaryWidget.scrollToItem( - items[0], QAbstractItemView.ScrollHint.PositionAtCenter) + items[0], QAbstractItemView.ScrollHint.PositionAtCenter + )