|
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 ericdoc 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_EricdocExecDialog import Ui_EricdocExecDialog |
|
24 |
|
25 import Preferences |
|
26 |
|
27 |
|
28 class EricdocExecDialog(QDialog, Ui_EricdocExecDialog): |
|
29 """ |
|
30 Class implementing a dialog to show the output of the ericdoc 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 documentation generator (string) |
|
40 @param parent parent widget of this dialog (QWidget) |
|
41 """ |
|
42 super(EricdocExecDialog, 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 ericdoc command. |
|
55 |
|
56 @param args commandline arguments for ericdoc program (list of strings) |
|
57 @param fn filename or dirname to be processed by ericdoc 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: |
|
121 if self.process.state() != QProcess.NotRunning: |
|
122 self.process.terminate() |
|
123 QTimer.singleShot(2000, self.process.kill) |
|
124 self.process.waitForFinished(3000) |
|
125 if self.process.exitStatus() == QProcess.CrashExit: |
|
126 self.contents.insertPlainText( |
|
127 self.tr('\n{0} crashed.\n').format(self.cmdname)) |
|
128 |
|
129 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) |
|
130 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) |
|
131 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) |
|
132 |
|
133 self.process = None |
|
134 |
|
135 self.contents.insertPlainText( |
|
136 self.tr('\n{0} finished.\n').format(self.cmdname)) |
|
137 self.contents.ensureCursorVisible() |
|
138 |
|
139 def __readStdout(self): |
|
140 """ |
|
141 Private slot to handle the readyReadStandardOutput signal. |
|
142 |
|
143 It reads the output of the process, formats it and inserts it into |
|
144 the contents pane. |
|
145 """ |
|
146 self.process.setReadChannel(QProcess.StandardOutput) |
|
147 |
|
148 while self.process.canReadLine(): |
|
149 s = str(self.process.readLine(), |
|
150 Preferences.getSystem("IOEncoding"), |
|
151 'replace') |
|
152 self.contents.insertPlainText(s) |
|
153 self.contents.ensureCursorVisible() |
|
154 |
|
155 def __readStderr(self): |
|
156 """ |
|
157 Private slot to handle the readyReadStandardError signal. |
|
158 |
|
159 It reads the error output of the process and inserts it into the |
|
160 error pane. |
|
161 """ |
|
162 self.process.setReadChannel(QProcess.StandardError) |
|
163 |
|
164 while self.process.canReadLine(): |
|
165 self.errorGroup.show() |
|
166 s = str(self.process.readLine(), |
|
167 Preferences.getSystem("IOEncoding"), |
|
168 'replace') |
|
169 self.errors.insertPlainText(s) |
|
170 self.errors.ensureCursorVisible() |