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