ProjectFlask/FlaskBabelExtension/PyBabelCommandDialog.py

branch
eric7
changeset 70
22e1d0f69668
parent 66
0d3168d0e310
child 72
4557829a4acf
equal deleted inserted replaced
69:c31a4f756a04 70:22e1d0f69668
17 17
18 class PyBabelCommandDialog(QDialog, Ui_FlaskCommandDialog): 18 class PyBabelCommandDialog(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 self.__argsLists = [] 49 self.__argsLists = []
50 self.__workdir = "" 50 self.__workdir = ""
51 51
52 self.buttonBox.button( 52 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True)
53 QDialogButtonBox.StandardButton.Close).setEnabled(True) 53 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True)
54 self.buttonBox.button( 54 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
55 QDialogButtonBox.StandardButton.Close).setDefault(True) 55
56 self.buttonBox.button(
57 QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
58
59 def startCommand(self, command, args, workdir, clearOutput=True): 56 def startCommand(self, command, args, workdir, clearOutput=True):
60 """ 57 """
61 Public method to start a pybabel command and show its output. 58 Public method to start a pybabel command and show its output.
62 59
63 @param command pybabel command to be run 60 @param command pybabel command to be run
64 @type str 61 @type str
65 @param args list of command line arguments for the command 62 @param args list of command line arguments for the command
66 @type list of str 63 @type list of str
67 @param workdir working directory for the command 64 @param workdir working directory for the command
70 @type bool 67 @type bool
71 @return flag indicating a successful start 68 @return flag indicating a successful start
72 @rtype bool 69 @rtype bool
73 """ 70 """
74 babelCommand = self.__project.getBabelCommand() 71 babelCommand = self.__project.getBabelCommand()
75 72
76 self.__process = QProcess() 73 self.__process = QProcess()
77 self.__process.setWorkingDirectory(workdir) 74 self.__process.setWorkingDirectory(workdir)
78 self.__process.setProcessChannelMode( 75 self.__process.setProcessChannelMode(QProcess.ProcessChannelMode.MergedChannels)
79 QProcess.ProcessChannelMode.MergedChannels) 76
80
81 self.__process.readyReadStandardOutput.connect(self.__readStdOut) 77 self.__process.readyReadStandardOutput.connect(self.__readStdOut)
82 self.__process.finished.connect(self.__processFinished) 78 self.__process.finished.connect(self.__processFinished)
83 79
84 if clearOutput: 80 if clearOutput:
85 self.outputEdit.clear() 81 self.outputEdit.clear()
86 82
87 babelArgs = [command] 83 babelArgs = [command]
88 if args: 84 if args:
89 babelArgs += args 85 babelArgs += args
90 86
91 self.__process.start(babelCommand, babelArgs) 87 self.__process.start(babelCommand, babelArgs)
92 ok = self.__process.waitForStarted(10000) 88 ok = self.__process.waitForStarted(10000)
93 if not ok: 89 if not ok:
94 EricMessageBox.critical( 90 EricMessageBox.critical(
95 None, 91 None,
96 self.tr("Execute PyBabel Command"), 92 self.tr("Execute PyBabel Command"),
97 self.tr("""The pybabel process could not be started.""")) 93 self.tr("""The pybabel process could not be started."""),
94 )
98 else: 95 else:
99 self.buttonBox.button( 96 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(
100 QDialogButtonBox.StandardButton.Close).setEnabled(False) 97 False
101 self.buttonBox.button( 98 )
102 QDialogButtonBox.StandardButton.Cancel).setDefault(True) 99 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault(
103 self.buttonBox.button( 100 True
104 QDialogButtonBox.StandardButton.Cancel).setEnabled(True) 101 )
105 self.buttonBox.button( 102 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(
106 QDialogButtonBox.StandardButton.Cancel).setFocus( 103 True
107 Qt.FocusReason.OtherFocusReason) 104 )
108 105 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setFocus(
106 Qt.FocusReason.OtherFocusReason
107 )
108
109 return ok 109 return ok
110 110
111 def startBatchCommand(self, argsLists, workdir): 111 def startBatchCommand(self, argsLists, workdir):
112 """ 112 """
113 Public method to start a pybabel command repeatedly with a list of 113 Public method to start a pybabel command repeatedly with a list of
114 arguments and show the output. 114 arguments and show the output.
115 115
116 @param argsLists list of command line arguments for the batch commands 116 @param argsLists list of command line arguments for the batch commands
117 @type list of lists of str 117 @type list of lists of str
118 @param workdir working directory for the command 118 @param workdir working directory for the command
119 @type str 119 @type str
120 @return flag indicating a successful start of the first process 120 @return flag indicating a successful start of the first process
121 @rtype bool 121 @rtype bool
122 """ 122 """
123 self.__argsLists = argsLists[:] 123 self.__argsLists = argsLists[:]
124 self.__workdir = workdir 124 self.__workdir = workdir
125 125
126 # start the first process 126 # start the first process
127 args = self.__argsLists.pop(0) 127 args = self.__argsLists.pop(0)
128 res = self.startCommand(args[0], args[1:], workdir) 128 res = self.startCommand(args[0], args[1:], workdir)
129 if not res: 129 if not res:
130 self.__argsLists = [] 130 self.__argsLists = []
131 131
132 return res 132 return res
133 133
134 def closeEvent(self, evt): 134 def closeEvent(self, evt):
135 """ 135 """
136 Protected method handling the close event of the dialog. 136 Protected method handling the close event of the dialog.
137 137
138 @param evt reference to the close event object 138 @param evt reference to the close event object
139 @type QCloseEvent 139 @type QCloseEvent
140 """ 140 """
141 self.__argsLists = [] 141 self.__argsLists = []
142 self.__cancelProcess() 142 self.__cancelProcess()
143 evt.accept() 143 evt.accept()
144 144
145 @pyqtSlot() 145 @pyqtSlot()
146 def __readStdOut(self): 146 def __readStdOut(self):
147 """ 147 """
148 Private slot to add the server process output to the output pane. 148 Private slot to add the server process output to the output pane.
149 """ 149 """
150 if self.__process is not None: 150 if self.__process is not None:
151 out = str(self.__process.readAllStandardOutput(), "utf-8") 151 out = str(self.__process.readAllStandardOutput(), "utf-8")
152 self.outputEdit.insertPlainText(out) 152 self.outputEdit.insertPlainText(out)
153 153
154 def __processFinished(self, exitCode, exitStatus): 154 def __processFinished(self, exitCode, exitStatus):
155 """ 155 """
156 Private slot connected to the finished signal. 156 Private slot connected to the finished signal.
157 157
158 @param exitCode exit code of the process 158 @param exitCode exit code of the process
159 @type int 159 @type int
160 @param exitStatus exit status of the process 160 @param exitStatus exit status of the process
161 @type QProcess.ExitStatus 161 @type QProcess.ExitStatus
162 """ 162 """
163 normal = ( 163 normal = exitStatus == QProcess.ExitStatus.NormalExit and exitCode == 0
164 exitStatus == QProcess.ExitStatus.NormalExit and
165 exitCode == 0
166 )
167 self.__cancelProcess() 164 self.__cancelProcess()
168 165
169 if self.__argsLists: 166 if self.__argsLists:
170 args = self.__argsLists.pop(0) 167 args = self.__argsLists.pop(0)
171 self.startCommand(args[0], args[1:], self.__workdir, 168 self.startCommand(args[0], args[1:], self.__workdir, clearOutput=False)
172 clearOutput=False)
173 return 169 return
174 170
175 self.buttonBox.button( 171 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
176 QDialogButtonBox.StandardButton.Cancel).setEnabled(False) 172 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True)
177 self.buttonBox.button( 173 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True)
178 QDialogButtonBox.StandardButton.Close).setEnabled(True)
179 self.buttonBox.button(
180 QDialogButtonBox.StandardButton.Close).setDefault(True)
181 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setFocus( 174 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setFocus(
182 Qt.FocusReason.OtherFocusReason) 175 Qt.FocusReason.OtherFocusReason
183 176 )
177
184 if normal and self.__successMessage: 178 if normal and self.__successMessage:
185 self.outputEdit.insertPlainText(self.__successMessage) 179 self.outputEdit.insertPlainText(self.__successMessage)
186 elif not normal and self.__errorMessage: 180 elif not normal and self.__errorMessage:
187 self.outputEdit.insertPlainText(self.__errorMessage) 181 self.outputEdit.insertPlainText(self.__errorMessage)
188 182
189 @pyqtSlot() 183 @pyqtSlot()
190 def __cancelProcess(self): 184 def __cancelProcess(self):
191 """ 185 """
192 Private slot to terminate the current process. 186 Private slot to terminate the current process.
193 """ 187 """
194 if ( 188 if (
195 self.__process is not None and 189 self.__process is not None
196 self.__process.state() != QProcess.ProcessState.NotRunning 190 and self.__process.state() != QProcess.ProcessState.NotRunning
197 ): 191 ):
198 self.__process.terminate() 192 self.__process.terminate()
199 QTimer.singleShot(2000, self.__process.kill) 193 QTimer.singleShot(2000, self.__process.kill)
200 self.__process.waitForFinished(3000) 194 self.__process.waitForFinished(3000)
201 195
202 self.__process = None 196 self.__process = None
203 197
204 @pyqtSlot(QAbstractButton) 198 @pyqtSlot(QAbstractButton)
205 def on_buttonBox_clicked(self, button): 199 def on_buttonBox_clicked(self, button):
206 """ 200 """
207 Private slot handling presses of the button box buttons. 201 Private slot handling presses of the button box buttons.
208 202
209 @param button reference to the button been clicked 203 @param button reference to the button been clicked
210 @type QAbstractButton 204 @type QAbstractButton
211 """ 205 """
212 if button is self.buttonBox.button( 206 if button is self.buttonBox.button(QDialogButtonBox.StandardButton.Close):
213 QDialogButtonBox.StandardButton.Close
214 ):
215 self.close() 207 self.close()
216 elif button is self.buttonBox.button( 208 elif button is self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel):
217 QDialogButtonBox.StandardButton.Cancel
218 ):
219 self.__argsLists = [] 209 self.__argsLists = []
220 self.__cancelProcess() 210 self.__cancelProcess()

eric ide

mercurial