|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to run the Flask server. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot, QProcess |
|
11 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton |
|
12 |
|
13 from E5Gui import E5MessageBox |
|
14 |
|
15 from .Ui_RunServerDialog import Ui_RunServerDialog |
|
16 |
|
17 |
|
18 class RunServerDialog(QDialog, Ui_RunServerDialog): |
|
19 """ |
|
20 Class implementing a dialog to run the Flask server. |
|
21 """ |
|
22 def __init__(self, parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param parent reference to the parent widget |
|
27 @type QWidget |
|
28 """ |
|
29 super(RunServerDialog, self).__init__(parent) |
|
30 self.setupUi(self) |
|
31 |
|
32 self.__process = None |
|
33 |
|
34 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) |
|
35 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) |
|
36 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) |
|
37 |
|
38 def startServer(self, command, workdir, env): |
|
39 """ |
|
40 Public method to start the Flask server process. |
|
41 |
|
42 @param command path of the flask command |
|
43 @type str |
|
44 @param workdir working directory for the Flask server |
|
45 @type str |
|
46 @param env environment for the Flask server process |
|
47 @type QProcessEnvironment |
|
48 @return flag indicating a successful start |
|
49 @rtype bool |
|
50 """ |
|
51 self.errorsEdit.hide() |
|
52 |
|
53 self.__process = QProcess() |
|
54 self.__process.readyReadStandardOutput.connect(self.__readStdOut) |
|
55 self.__process.readyReadStandardError.connect(self.__readStdErr) |
|
56 self.__process.finished.connect(self.__processFinished) |
|
57 self.__process.setProcessEnvironment(env) |
|
58 self.__process.setWorkingDirectory(workdir) |
|
59 |
|
60 self.__process.start(command, ["run"]) |
|
61 ok = self.__process.waitForStarted(10000) |
|
62 if not ok: |
|
63 E5MessageBox.critical( |
|
64 None, |
|
65 self.tr("Run Flask Server"), |
|
66 self.tr("""The Flask server process could not be started.""")) |
|
67 else: |
|
68 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) |
|
69 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True) |
|
70 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) |
|
71 |
|
72 return ok |
|
73 |
|
74 def closeEvent(self, evt): |
|
75 """ |
|
76 Private method handling a close event. |
|
77 |
|
78 @param evt reference to the close event |
|
79 @type QCloseEvent |
|
80 """ |
|
81 # TODO: not implemented yet |
|
82 |
|
83 @pyqtSlot(QAbstractButton) |
|
84 def on_buttonBox_clicked(self, button): |
|
85 """ |
|
86 Private slot handling button presses. |
|
87 |
|
88 @param button button that was pressed |
|
89 @type QAbstractButton |
|
90 """ |
|
91 if button is self.buttonBox.button(QDialogButtonBox.Cancel): |
|
92 self.__cancel() |
|
93 elif button is self.buttonBox.button(QDialogButtonBox.Close): |
|
94 self.close() |
|
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.appendPlainText(out) |
|
104 |
|
105 @pyqtSlot() |
|
106 def __readStdErr(self): |
|
107 """ |
|
108 Private slot to add the server process errors to the errors pane. |
|
109 """ |
|
110 if self.__process is not None: |
|
111 err = str(self.__process.readAllStandardError(), "utf-8") |
|
112 self.errorsEdit.appendPlainText(err) |
|
113 |
|
114 self.errorsEdit.show() |
|
115 |
|
116 @pyqtSlot() |
|
117 def __processFinished(self): |
|
118 # TODO: implement it |
|
119 pass |
|
120 |
|
121 @pyqtSlot() |
|
122 def __cancel(self): |
|
123 """ |
|
124 Private slot to cancel the running server. |
|
125 """ |
|
126 # TODO: not implemented yet |