Debian Codegraph @

avogadro/qtgui/pythonscript.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTGUI_PYTHONSCRIPT_H
7 #define AVOGADRO_QTGUI_PYTHONSCRIPT_H
8
9 #include "avogadroqtguiexport.h"
10 #include <QtCore/QObject>
11
12 #include <avogadro/core/avogadrocore.h>
13
14 #include <QtCore/QByteArray>
15 #include <QtCore/QProcess>
16 #include <QtCore/QString>
17 #include <QtCore/QStringList>
18
19 namespace Avogadro {
20 namespace QtGui {
21
22 /**
23 * @brief The PythonScript class implements a interface for calling short-lived
24 * python utility scripts.
25 */
26 class AVOGADROQTGUI_EXPORT PythonScript : public QObject
27 {
28 Q_OBJECT
29 public:
30 /**
31 * Constructors
32 * @param scriptFilePath_ Absolute path to python script.
33 * @{
34 */
35 explicit PythonScript(const QString& scriptFilePath_,
36 QObject* parent_ = nullptr);
37 explicit PythonScript(QObject* parent_ = nullptr);
38 /**@}*/
39
40 ~PythonScript() override;
41
42 /**
43 * @return True if debugging of python I/O is enabled.
44 */
45 bool debug() const { return m_debug; }
46
47 /**
48 * @return The path to the generator file.
49 */
50 QString scriptFilePath() const { return m_scriptFilePath; }
51
52 /**
53 * Set the path to the input generator script file. This will reset any
54 * cached data held by this class.
55 */
56 void setScriptFilePath(const QString& scriptFile);
57
58 /**
59 * Enable package mode. In this mode, execute() runs
60 * "pixi run <command> <identifier> [args]" with packageDir as the
61 * working directory, instead of launching a script file via python.
62 */
63 void setPackageInfo(const QString& packageDir, const QString& command,
64 const QString& identifier,
65 const QString& displayName = QString());
66
67 /**
68 * @return True if this script is in package mode.
69 */
70 bool isPackageMode() const { return m_packageMode; }
71
72 /**
73 * @return The package directory (only meaningful in package mode).
74 */
75 QString packageDir() const { return m_packageDir; }
76
77 /**
78 * @return The package command (only meaningful in package mode).
79 */
80 QString packageCommand() const { return m_packageCommand; }
81
82 /**
83 * @return The package identifier (only meaningful in package mode).
84 */
85 QString packageIdentifier() const { return m_packageIdentifier; }
86
87 /**
88 * @return The package display name from metadata, if one was provided.
89 */
90 QString packageDisplayName() const { return m_packageDisplayName; }
91
92 /**
93 * @return True if an error is set.
94 */
95 bool hasErrors() const { return !m_errors.isEmpty(); }
96
97 /**
98 * Reset the error counter.
99 */
100 void clearErrors() { m_errors.clear(); }
101
102 /**
103 * @return A QStringList containing all errors that occurred in the last call
104 * to the input generator script.
105 */
106 QStringList errorList() const { return m_errors; }
107
108 /**
109 * Reset the python interpretor path. The following are checked, in order:
110 * - The AVO_PYTHON_INTERPRETER environment variable
111 * - The "interpreters/python" QSettings value
112 * - The path specified in avogadropython.h.
113 */
114 void setDefaultPythonInterpreter();
115
116 /**
117 * Start a new process to execute:
118 * "<m_pythonInterpreter> <scriptFilePath()> [args ...]",
119 * optionally passing scriptStdin to the processes standard input. Returns
120 * the standard output of the process when finished.
121 */
122 QByteArray execute(const QStringList& args,
123 const QByteArray& scriptStdin = QByteArray());
124
125 /**
126 * Start a new process to execute asynchronously
127 * "<m_pythonInterpreter> <scriptFilePath()> [args ...]",
128 * optionally passing scriptStdin to the processes standard input.
129 * If @p closeWriteChannel is false, stdin remains open for follow-up writes.
130 *
131 * Will send asyncFinished() signal when finished
132 */
133 void asyncExecute(const QStringList& args,
134 const QByteArray& scriptStdin = QByteArray(),
135 bool mergedChannels = true, bool closeWriteChannel = true);
136
137 /**
138 * Write input to the asynchronous process' standard input and return the
139 * standard output when ready. Does not wait for the process to terminate
140 * before returning (e.g. "server mode").
141 *
142 * @param input The input to write to the process' standard input
143 * @return The standard output of the process
144 */
145 QByteArray asyncWriteAndResponse(QByteArray input);
146
147 /**
148 * Write input to the asynchronous process' standard input and return raw
149 * bytes from stdout. Unlike asyncWriteAndResponse(), this does not require
150 * line-oriented output and is suitable for framed binary protocols.
151 */
152 QByteArray asyncWriteAndResponseRaw(const QByteArray& input,
153 int timeoutMs = 5000);
154
155 /**
156 * Terminate the asynchronous process.
157 */
158 void asyncTerminate();
159
160 /**
161 * Returns the standard output of the asynchronous process when finished.
162 */
163 QByteArray asyncResponse();
164
165 signals:
166 /**
167 * The asynchronous execution is finished or timed out
168 */
169 void finished();
170
171 public slots:
172 /**
173 * Enable/disable debugging.
174 */
175 void setDebug(bool d) { m_debug = d; }
176
177 /**
178 * Handle a finished process;
179 */
180 void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
181
182 protected:
183 bool m_debug;
184 bool m_packageMode = false;
185 QString m_pythonInterpreter;
186 QString m_pixi;
187 QString m_scriptFilePath;
188 QString m_packageDir;
189 QString m_packageCommand;
190 QString m_packageIdentifier;
191 QString m_packageDisplayName;
192 QStringList m_errors;
193 QProcess* m_process;
194
195 private:
196 /**
197 * Resolve the executable and finalize the argument list for either
198 * execute() or asyncExecute(). On entry @p realArgs already contains
199 * the caller-supplied args plus --debug/--lang. On return @p realArgs
200 * is fully prepended and @p proc has its working directory set.
201 * @return The program path to pass to QProcess::start(), or an empty
202 * string on error (m_errors will be populated).
203 */
204 QString resolveCommand(QStringList& realArgs, QProcess& proc);
205
206 QString processErrorString(const QProcess& proc) const;
207 };
208
209 } // namespace QtGui
210 } // namespace Avogadro
211
212 #endif // AVOGADRO_QTGUI_PYTHONSCRIPT_H