|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to show the output of the ericapi process. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 try: |
|
12 str = unicode |
|
13 except NameError: |
|
14 pass |
|
15 |
|
16 import os.path |
|
17 |
|
18 from PyQt5.QtCore import QProcess, QTimer |
|
19 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
20 |
|
21 from E5Gui import E5MessageBox |
|
22 |
|
23 from .Ui_EricapiExecDialog import Ui_EricapiExecDialog |
|
24 |
|
25 import Preferences |
|
26 |
|
27 |
|
28 class EricapiExecDialog(QDialog, Ui_EricapiExecDialog): |
|
29 """ |
|
30 Class implementing a dialog to show the output of the ericapi process. |
|
31 |
|
32 This class starts a QProcess and displays a dialog that |
|
33 shows the output of the documentation command process. |
|
34 """ |
|
35 def __init__(self, cmdname, parent=None): |
|
36 """ |
|
37 Constructor |
|
38 |
|
39 @param cmdname name of the ericapi generator (string) |
|
40 @param parent parent widget of this dialog (QWidget) |
|
41 """ |
|
42 super(EricapiExecDialog, self).__init__(parent) |
|
43 self.setModal(True) |
|
44 self.setupUi(self) |
|
45 |
|
46 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) |
|
47 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) |
|
48 |
|
49 self.process = None |
|
50 self.cmdname = cmdname |
|
51 |
|
52 def start(self, args, fn): |
|
53 """ |
|
54 Public slot to start the ericapi command. |
|
55 |
|
56 @param args commandline arguments for ericapi program (list of strings) |
|
57 @param fn filename or dirname to be processed by ericapi program |
|
58 (string) |
|
59 @return flag indicating the successful start of the process (boolean) |
|
60 """ |
|
61 self.errorGroup.hide() |
|
62 |
|
63 self.filename = fn |
|
64 if os.path.isdir(self.filename): |
|
65 dname = os.path.abspath(self.filename) |
|
66 fname = "." |
|
67 if os.path.exists(os.path.join(dname, "__init__.py")): |
|
68 fname = os.path.basename(dname) |
|
69 dname = os.path.dirname(dname) |
|
70 else: |
|
71 dname = os.path.dirname(self.filename) |
|
72 fname = os.path.basename(self.filename) |
|
73 |
|
74 self.contents.clear() |
|
75 self.errors.clear() |
|
76 |
|
77 program = args[0] |
|
78 del args[0] |
|
79 args.append(fname) |
|
80 |
|
81 self.process = QProcess() |
|
82 self.process.setWorkingDirectory(dname) |
|
83 |
|
84 self.process.readyReadStandardOutput.connect(self.__readStdout) |
|
85 self.process.readyReadStandardError.connect(self.__readStderr) |
|
86 self.process.finished.connect(self.__finish) |
|
87 |
|
88 self.setWindowTitle( |
|
89 self.tr('{0} - {1}').format(self.cmdname, self.filename)) |
|
90 self.process.start(program, args) |
|
91 procStarted = self.process.waitForStarted(5000) |
|
92 if not procStarted: |
|
93 E5MessageBox.critical( |
|
94 self, |
|
95 self.tr('Process Generation Error'), |
|
96 self.tr( |
|
97 'The process {0} could not be started. ' |
|
98 'Ensure, that it is in the search path.' |
|
99 ).format(program)) |
|
100 return procStarted |
|
101 |
|
102 def on_buttonBox_clicked(self, button): |
|
103 """ |
|
104 Private slot called by a button of the button box clicked. |
|
105 |
|
106 @param button button that was clicked (QAbstractButton) |
|
107 """ |
|
108 if button == self.buttonBox.button(QDialogButtonBox.Close): |
|
109 self.accept() |
|
110 elif button == self.buttonBox.button(QDialogButtonBox.Cancel): |
|
111 self.__finish() |
|
112 |
|
113 def __finish(self): |
|
114 """ |
|
115 Private slot called when the process finished. |
|
116 |
|
117 It is called when the process finished or |
|
118 the user pressed the button. |
|
119 """ |
|
120 if self.process is not None and \ |
|
121 self.process.state() != QProcess.NotRunning: |
|
122 self.process.terminate() |
|
123 QTimer.singleShot(2000, self.process.kill) |
|
124 self.process.waitForFinished(3000) |
|
125 |
|
126 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) |
|
127 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) |
|
128 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) |
|
129 |
|
130 self.process = None |
|
131 |
|
132 self.contents.insertPlainText( |
|
133 self.tr('\n{0} finished.\n').format(self.cmdname)) |
|
134 self.contents.ensureCursorVisible() |
|
135 |
|
136 def __readStdout(self): |
|
137 """ |
|
138 Private slot to handle the readyReadStandardOutput signal. |
|
139 |
|
140 It reads the output of the process, formats it and inserts it into |
|
141 the contents pane. |
|
142 """ |
|
143 self.process.setReadChannel(QProcess.StandardOutput) |
|
144 |
|
145 while self.process.canReadLine(): |
|
146 s = str(self.process.readLine(), |
|
147 Preferences.getSystem("IOEncoding"), |
|
148 'replace') |
|
149 self.contents.insertPlainText(s) |
|
150 self.contents.ensureCursorVisible() |
|
151 |
|
152 def __readStderr(self): |
|
153 """ |
|
154 Private slot to handle the readyReadStandardError signal. |
|
155 |
|
156 It reads the error output of the process and inserts it into the |
|
157 error pane. |
|
158 """ |
|
159 self.process.setReadChannel(QProcess.StandardError) |
|
160 |
|
161 while self.process.canReadLine(): |
|
162 self.errorGroup.show() |
|
163 s = str(self.process.readLine(), |
|
164 Preferences.getSystem("IOEncoding"), |
|
165 'replace') |
|
166 self.errors.insertPlainText(s) |
|
167 self.errors.ensureCursorVisible() |