src/eric7/Plugins/DocumentationPlugins/Ericdoc/EricdocExecDialog.py

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

eric ide

mercurial