src/eric7/Plugins/DocumentationPlugins/Ericapi/EricapiExecDialog.py

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

eric ide

mercurial