|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 """ |
|
4 Module implementing a dialog to run a flask command and show its output. |
|
5 """ |
|
6 |
|
7 from PyQt5.QtCore import pyqtSlot, Qt, QProcess, QTimer |
|
8 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton |
|
9 |
|
10 from E5Gui import E5MessageBox |
|
11 |
|
12 from .Ui_FlaskCommandDialog import Ui_FlaskCommandDialog |
|
13 |
|
14 |
|
15 class FlaskCommandDialog(QDialog, Ui_FlaskCommandDialog): |
|
16 """ |
|
17 Class implementing a dialog to run a flask command and show its output. |
|
18 """ |
|
19 def __init__(self, project, parent=None): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param project reference to the project object |
|
24 @type Project |
|
25 @param parent reference to the parent widget |
|
26 @type QWidget |
|
27 """ |
|
28 super(FlaskCommandDialog, self).__init__(parent) |
|
29 self.setupUi(self) |
|
30 |
|
31 self.__project = project |
|
32 |
|
33 self.__process = None |
|
34 |
|
35 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) |
|
36 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) |
|
37 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) |
|
38 |
|
39 def startCommand(self, command, args=None): |
|
40 """ |
|
41 Public method to start a flask command and show its output. |
|
42 |
|
43 @param command flask command to be run |
|
44 @type str |
|
45 @param args list of command line arguments for the command |
|
46 @type list of str |
|
47 @return flag indicating a successful start |
|
48 @rtype bool |
|
49 """ |
|
50 workdir, env = self.__project.prepareRuntimeEnvironment() |
|
51 if env is not None: |
|
52 flaskCommand = self.__project.getFlaskCommand() |
|
53 |
|
54 self.__process = QProcess() |
|
55 self.__process.setProcessEnvironment(env) |
|
56 self.__process.setWorkingDirectory(workdir) |
|
57 self.__process.setProcessChannelMode(QProcess.MergedChannels) |
|
58 |
|
59 self.__process.readyReadStandardOutput.connect(self.__readStdOut) |
|
60 self.__process.finished.connect(self.__processFinished) |
|
61 |
|
62 self.outputEdit.clear() |
|
63 |
|
64 flaskArgs = [command] |
|
65 if args: |
|
66 flaskArgs += args |
|
67 |
|
68 self.__process.start(flaskCommand, flaskArgs) |
|
69 ok = self.__process.waitForStarted(10000) |
|
70 if not ok: |
|
71 E5MessageBox.critical( |
|
72 None, |
|
73 self.tr("Execute Flask Command"), |
|
74 self.tr("""The Flask process could not be started.""")) |
|
75 else: |
|
76 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) |
|
77 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) |
|
78 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True) |
|
79 self.buttonBox.button(QDialogButtonBox.Cancel).setFocus( |
|
80 Qt.OtherFocusReason) |
|
81 else: |
|
82 ok = False |
|
83 |
|
84 return ok |
|
85 |
|
86 def closeEvent(self, evt): |
|
87 """ |
|
88 Protected method handling the close event of the dialog. |
|
89 |
|
90 @param evt reference to the close event object |
|
91 @type QCloseEvent |
|
92 """ |
|
93 self.__cancelProcess() |
|
94 evt.accept() |
|
95 |
|
96 @pyqtSlot() |
|
97 def __readStdOut(self): |
|
98 """ |
|
99 Private slot to add the server process output to the output pane. |
|
100 """ |
|
101 if self.__process is not None: |
|
102 out = str(self.__process.readAllStandardOutput(), "utf-8") |
|
103 self.outputEdit.insertPlainText(out) |
|
104 |
|
105 @pyqtSlot() |
|
106 def __processFinished(self): |
|
107 """ |
|
108 Private slot handling the finishing of the server process. |
|
109 """ |
|
110 self.__cancelProcess() |
|
111 |
|
112 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) |
|
113 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) |
|
114 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) |
|
115 self.buttonBox.button(QDialogButtonBox.Close).setFocus( |
|
116 Qt.OtherFocusReason) |
|
117 |
|
118 @pyqtSlot() |
|
119 def __cancelProcess(self): |
|
120 """ |
|
121 Private slot to terminate the current process. |
|
122 """ |
|
123 if ( |
|
124 self.__process is not None and |
|
125 self.__process.state() != QProcess.NotRunning |
|
126 ): |
|
127 self.__process.terminate() |
|
128 QTimer.singleShot(2000, self.__process.kill) |
|
129 self.__process.waitForFinished(3000) |
|
130 |
|
131 self.__process = None |
|
132 |
|
133 @pyqtSlot(QAbstractButton) |
|
134 def on_buttonBox_clicked(self, button): |
|
135 """ |
|
136 Slot handling presses of the button box buttons. |
|
137 |
|
138 @param button reference to the button been clicked |
|
139 @type QAbstractButton |
|
140 """ |
|
141 if button is self.buttonBox.button(QDialogButtonBox.Close): |
|
142 self.close() |
|
143 elif button is self.buttonBox.button(QDialogButtonBox.Cancel): |
|
144 self.__cancelProcess() |