ProjectFlask/FlaskCommandDialog.py

branch
eric7
changeset 70
22e1d0f69668
parent 66
0d3168d0e310
child 72
4557829a4acf
equal deleted inserted replaced
69:c31a4f756a04 70:22e1d0f69668
17 17
18 class FlaskCommandDialog(QDialog, Ui_FlaskCommandDialog): 18 class FlaskCommandDialog(QDialog, Ui_FlaskCommandDialog):
19 """ 19 """
20 Class implementing a dialog to run a flask command and show its output. 20 Class implementing a dialog to run a flask command and show its output.
21 """ 21 """
22 def __init__(self, project, title="", msgSuccess="", msgError="", 22
23 parent=None): 23 def __init__(self, project, title="", msgSuccess="", msgError="", parent=None):
24 """ 24 """
25 Constructor 25 Constructor
26 26
27 @param project reference to the project object 27 @param project reference to the project object
28 @type Project 28 @type Project
29 @param title window title of the dialog 29 @param title window title of the dialog
30 @type str 30 @type str
31 @param msgSuccess success message to be shown 31 @param msgSuccess success message to be shown
35 @param parent reference to the parent widget 35 @param parent reference to the parent widget
36 @type QWidget 36 @type QWidget
37 """ 37 """
38 super().__init__(parent) 38 super().__init__(parent)
39 self.setupUi(self) 39 self.setupUi(self)
40 40
41 if title: 41 if title:
42 self.setWindowTitle(title) 42 self.setWindowTitle(title)
43 43
44 self.__project = project 44 self.__project = project
45 self.__successMessage = msgSuccess 45 self.__successMessage = msgSuccess
46 self.__errorMessage = msgError 46 self.__errorMessage = msgError
47 47
48 self.__process = None 48 self.__process = None
49 49
50 self.buttonBox.button( 50 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True)
51 QDialogButtonBox.StandardButton.Close).setEnabled(True) 51 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True)
52 self.buttonBox.button( 52 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
53 QDialogButtonBox.StandardButton.Close).setDefault(True) 53
54 self.buttonBox.button(
55 QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
56
57 def startCommand(self, command, args=None): 54 def startCommand(self, command, args=None):
58 """ 55 """
59 Public method to start a flask command and show its output. 56 Public method to start a flask command and show its output.
60 57
61 @param command flask command to be run 58 @param command flask command to be run
62 @type str 59 @type str
63 @param args list of command line arguments for the command 60 @param args list of command line arguments for the command
64 @type list of str 61 @type list of str
65 @return flag indicating a successful start 62 @return flag indicating a successful start
67 """ 64 """
68 self.__normal = False 65 self.__normal = False
69 workdir, env = self.__project.prepareRuntimeEnvironment() 66 workdir, env = self.__project.prepareRuntimeEnvironment()
70 if env is not None: 67 if env is not None:
71 flaskCommand = self.__project.getFlaskCommand() 68 flaskCommand = self.__project.getFlaskCommand()
72 69
73 self.__process = QProcess() 70 self.__process = QProcess()
74 self.__process.setProcessEnvironment(env) 71 self.__process.setProcessEnvironment(env)
75 self.__process.setWorkingDirectory(workdir) 72 self.__process.setWorkingDirectory(workdir)
76 self.__process.setProcessChannelMode( 73 self.__process.setProcessChannelMode(
77 QProcess.ProcessChannelMode.MergedChannels) 74 QProcess.ProcessChannelMode.MergedChannels
78 75 )
76
79 self.__process.readyReadStandardOutput.connect(self.__readStdOut) 77 self.__process.readyReadStandardOutput.connect(self.__readStdOut)
80 self.__process.finished.connect(self.__processFinished) 78 self.__process.finished.connect(self.__processFinished)
81 79
82 self.outputEdit.clear() 80 self.outputEdit.clear()
83 81
84 flaskArgs = [command] 82 flaskArgs = [command]
85 if args: 83 if args:
86 flaskArgs += args 84 flaskArgs += args
87 85
88 self.__process.start(flaskCommand, flaskArgs) 86 self.__process.start(flaskCommand, flaskArgs)
89 ok = self.__process.waitForStarted(10000) 87 ok = self.__process.waitForStarted(10000)
90 if not ok: 88 if not ok:
91 EricMessageBox.critical( 89 EricMessageBox.critical(
92 None, 90 None,
93 self.tr("Execute Flask Command"), 91 self.tr("Execute Flask Command"),
94 self.tr("""The Flask process could not be started.""")) 92 self.tr("""The Flask process could not be started."""),
93 )
95 else: 94 else:
95 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(
96 False
97 )
96 self.buttonBox.button( 98 self.buttonBox.button(
97 QDialogButtonBox.StandardButton.Close).setEnabled(False) 99 QDialogButtonBox.StandardButton.Cancel
100 ).setDefault(True)
98 self.buttonBox.button( 101 self.buttonBox.button(
99 QDialogButtonBox.StandardButton.Cancel).setDefault(True) 102 QDialogButtonBox.StandardButton.Cancel
100 self.buttonBox.button( 103 ).setEnabled(True)
101 QDialogButtonBox.StandardButton.Cancel).setEnabled(True) 104 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setFocus(
102 self.buttonBox.button( 105 Qt.FocusReason.OtherFocusReason
103 QDialogButtonBox.StandardButton.Cancel).setFocus( 106 )
104 Qt.FocusReason.OtherFocusReason)
105 else: 107 else:
106 ok = False 108 ok = False
107 109
108 return ok 110 return ok
109 111
110 def closeEvent(self, evt): 112 def closeEvent(self, evt):
111 """ 113 """
112 Protected method handling the close event of the dialog. 114 Protected method handling the close event of the dialog.
113 115
114 @param evt reference to the close event object 116 @param evt reference to the close event object
115 @type QCloseEvent 117 @type QCloseEvent
116 """ 118 """
117 self.__cancelProcess() 119 self.__cancelProcess()
118 evt.accept() 120 evt.accept()
119 121
120 @pyqtSlot() 122 @pyqtSlot()
121 def __readStdOut(self): 123 def __readStdOut(self):
122 """ 124 """
123 Private slot to add the server process output to the output pane. 125 Private slot to add the server process output to the output pane.
124 """ 126 """
125 if self.__process is not None: 127 if self.__process is not None:
126 out = str(self.__process.readAllStandardOutput(), "utf-8") 128 out = str(self.__process.readAllStandardOutput(), "utf-8")
127 self.outputEdit.insertPlainText(out) 129 self.outputEdit.insertPlainText(out)
128 130
129 def __processFinished(self, exitCode, exitStatus): 131 def __processFinished(self, exitCode, exitStatus):
130 """ 132 """
131 Private slot connected to the finished signal. 133 Private slot connected to the finished signal.
132 134
133 @param exitCode exit code of the process 135 @param exitCode exit code of the process
134 @type int 136 @type int
135 @param exitStatus exit status of the process 137 @param exitStatus exit status of the process
136 @type QProcess.ExitStatus 138 @type QProcess.ExitStatus
137 """ 139 """
138 self.__normal = ( 140 self.__normal = exitStatus == QProcess.ExitStatus.NormalExit and exitCode == 0
139 exitStatus == QProcess.ExitStatus.NormalExit and 141 self.__cancelProcess()
140 exitCode == 0 142
143 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
144 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True)
145 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True)
146 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setFocus(
147 Qt.FocusReason.OtherFocusReason
141 ) 148 )
142 self.__cancelProcess() 149
143
144 self.buttonBox.button(
145 QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
146 self.buttonBox.button(
147 QDialogButtonBox.StandardButton.Close).setEnabled(True)
148 self.buttonBox.button(
149 QDialogButtonBox.StandardButton.Close).setDefault(True)
150 self.buttonBox.button(
151 QDialogButtonBox.StandardButton.Close).setFocus(
152 Qt.FocusReason.OtherFocusReason)
153
154 if self.__normal and self.__successMessage: 150 if self.__normal and self.__successMessage:
155 self.outputEdit.insertPlainText(self.__successMessage) 151 self.outputEdit.insertPlainText(self.__successMessage)
156 elif not self.__normal and self.__errorMessage: 152 elif not self.__normal and self.__errorMessage:
157 self.outputEdit.insertPlainText(self.__errorMessage) 153 self.outputEdit.insertPlainText(self.__errorMessage)
158 154
159 @pyqtSlot() 155 @pyqtSlot()
160 def __cancelProcess(self): 156 def __cancelProcess(self):
161 """ 157 """
162 Private slot to terminate the current process. 158 Private slot to terminate the current process.
163 """ 159 """
164 if ( 160 if (
165 self.__process is not None and 161 self.__process is not None
166 self.__process.state() != QProcess.ProcessState.NotRunning 162 and self.__process.state() != QProcess.ProcessState.NotRunning
167 ): 163 ):
168 self.__process.terminate() 164 self.__process.terminate()
169 QTimer.singleShot(2000, self.__process.kill) 165 QTimer.singleShot(2000, self.__process.kill)
170 self.__process.waitForFinished(3000) 166 self.__process.waitForFinished(3000)
171 167
172 self.__process = None 168 self.__process = None
173 169
174 @pyqtSlot(QAbstractButton) 170 @pyqtSlot(QAbstractButton)
175 def on_buttonBox_clicked(self, button): 171 def on_buttonBox_clicked(self, button):
176 """ 172 """
177 Private slot handling presses of the button box buttons. 173 Private slot handling presses of the button box buttons.
178 174
179 @param button reference to the button been clicked 175 @param button reference to the button been clicked
180 @type QAbstractButton 176 @type QAbstractButton
181 """ 177 """
182 if button is self.buttonBox.button( 178 if button is self.buttonBox.button(QDialogButtonBox.StandardButton.Close):
183 QDialogButtonBox.StandardButton.Close
184 ):
185 self.close() 179 self.close()
186 elif button is self.buttonBox.button( 180 elif button is self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel):
187 QDialogButtonBox.StandardButton.Cancel
188 ):
189 self.__cancelProcess() 181 self.__cancelProcess()
190 182
191 def normalExit(self): 183 def normalExit(self):
192 """ 184 """
193 Public method to test, if the process ended without errors. 185 Public method to test, if the process ended without errors.
194 186
195 @return flag indicating a normal process exit 187 @return flag indicating a normal process exit
196 @rtype bool 188 @rtype bool
197 """ 189 """
198 return self.__normal 190 return self.__normal

eric ide

mercurial