--- a/ProjectFlask/FlaskCommandDialog.py Thu Dec 30 12:13:22 2021 +0100 +++ b/ProjectFlask/FlaskCommandDialog.py Wed Sep 21 16:30:15 2022 +0200 @@ -19,11 +19,11 @@ """ Class implementing a dialog to run a flask command and show its output. """ - def __init__(self, project, title="", msgSuccess="", msgError="", - parent=None): + + def __init__(self, project, title="", msgSuccess="", msgError="", parent=None): """ Constructor - + @param project reference to the project object @type Project @param title window title of the dialog @@ -37,27 +37,24 @@ """ super().__init__(parent) self.setupUi(self) - + if title: self.setWindowTitle(title) - + self.__project = project self.__successMessage = msgSuccess self.__errorMessage = msgError - + self.__process = None - - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setEnabled(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setDefault(True) - 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.Cancel).setEnabled(False) + def startCommand(self, command, args=None): """ Public method to start a flask command and show its output. - + @param command flask command to be run @type str @param args list of command line arguments for the command @@ -69,54 +66,59 @@ workdir, env = self.__project.prepareRuntimeEnvironment() if env is not None: flaskCommand = self.__project.getFlaskCommand() - + self.__process = QProcess() self.__process.setProcessEnvironment(env) self.__process.setWorkingDirectory(workdir) self.__process.setProcessChannelMode( - QProcess.ProcessChannelMode.MergedChannels) - + QProcess.ProcessChannelMode.MergedChannels + ) + self.__process.readyReadStandardOutput.connect(self.__readStdOut) self.__process.finished.connect(self.__processFinished) - + self.outputEdit.clear() - + flaskArgs = [command] if args: flaskArgs += args - + self.__process.start(flaskCommand, flaskArgs) ok = self.__process.waitForStarted(10000) if not ok: EricMessageBox.critical( None, self.tr("Execute Flask Command"), - self.tr("""The Flask process could not be started.""")) + self.tr("""The Flask process could not be started."""), + ) else: - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setEnabled(False) + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled( + False + ) self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setDefault(True) + QDialogButtonBox.StandardButton.Cancel + ).setDefault(True) self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setEnabled(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setFocus( - Qt.FocusReason.OtherFocusReason) + QDialogButtonBox.StandardButton.Cancel + ).setEnabled(True) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setFocus( + Qt.FocusReason.OtherFocusReason + ) else: ok = False - + return ok - + def closeEvent(self, evt): """ Protected method handling the close event of the dialog. - + @param evt reference to the close event object @type QCloseEvent """ self.__cancelProcess() evt.accept() - + @pyqtSlot() def __readStdOut(self): """ @@ -125,73 +127,63 @@ if self.__process is not None: out = str(self.__process.readAllStandardOutput(), "utf-8") self.outputEdit.insertPlainText(out) - + def __processFinished(self, exitCode, exitStatus): """ Private slot connected to the finished signal. - + @param exitCode exit code of the process @type int @param exitStatus exit status of the process @type QProcess.ExitStatus """ - self.__normal = ( - exitStatus == QProcess.ExitStatus.NormalExit and - exitCode == 0 - ) + self.__normal = exitStatus == QProcess.ExitStatus.NormalExit and exitCode == 0 self.__cancelProcess() - - 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 + ) + if self.__normal and self.__successMessage: self.outputEdit.insertPlainText(self.__successMessage) elif not self.__normal and self.__errorMessage: self.outputEdit.insertPlainText(self.__errorMessage) - + @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 - + @pyqtSlot(QAbstractButton) def on_buttonBox_clicked(self, button): """ Private slot handling presses of the button box buttons. - + @param button reference to the button been clicked @type QAbstractButton """ - if button is self.buttonBox.button( - QDialogButtonBox.StandardButton.Close - ): + if button is self.buttonBox.button(QDialogButtonBox.StandardButton.Close): self.close() - elif button is self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel - ): + elif button is self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel): self.__cancelProcess() - + def normalExit(self): """ Public method to test, if the process ended without errors. - + @return flag indicating a normal process exit @rtype bool """