Computer Assisted Medical Intervention Tool Kit version 6.0
 
Loading...
Searching...
No Matches
PythonManager.h
Go to the documentation of this file.
1/*****************************************************************************
2 * $CAMITK_LICENCE_BEGIN$
3 *
4 * CamiTK - Computer Assisted Medical Intervention ToolKit
5 * (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
6 *
7 * Visit http://camitk.imag.fr for more information
8 *
9 * This file is part of CamiTK.
10 *
11 * CamiTK is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * CamiTK is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22 *
23 * $CAMITK_LICENCE_END$
24 ****************************************************************************/
25
26#ifdef PYTHON_BINDING
27
28#ifndef __PYTHON_MANAGER__
29#define __PYTHON_MANAGER__
30
31#include "CamiTKAPI.h"
32
33#include <QDir>
34#include <QProcess>
35
36#pragma push_macro("slots")
37#undef slots
38#include <pybind11/embed.h>
39#pragma pop_macro("slots")
40
41namespace py = pybind11;
42
43namespace camitk {
44
96class CAMITK_API PythonManager {
97
98public:
115 static bool initPython();
116
122 static QString getPythonStatus();
123
134 static py::module_ lockContext(QString virtualEnvPath, QString scriptPath);
135
147 static QMap<QString, QVariant> runScript(QString virtualEnvPath, const QString& pythonScript, QString& pythonError);
148
150 static void unlock();
151
153 static QString getPythonVersion();
154
157 static QString pythonEnvironmentDebugInfo();
158
160 static void dump(py::dict dict);
161
175 static QVariant fromPython(const py::handle& value);
176
183 static bool checkVirtualEnvPath(QString virtualEnvRootPath, bool silent = true);
184
188 static bool createVirtualEnv(QString virtualEnvRootPath);
189
194 static bool installPackages(QString virtualEnvPath, QStringList packages, int progressMinimum = 0, int progressMaximum = 100);
195
207 static void setPythonPointer(QObject* qObject, py::object pythonPointer);
208
211 static void backupPythonState(QObject* qObject);
212
215 static void restorePythonState(QObject* qObject);
216
217private:
219 static void resolvePythonSharedLibPathAndVersion();
220
222 static bool loadPythonSharedLibrary();
223
228 static QString findPythonModule(const QString& moduleName);
229
233 static bool insertIfNotAlreadyInList(py::list* list, const QString& value);
234
238 static void checkPythonPackageConflicts();
239
242 static QString findPythonExecutable();
243
253 static py::module_ importOrReload(const QString& moduleName, bool clearAll = false);
254
258 static bool setupPython(QString virtualEnvPath, QString scriptPath = QString());
259
261 static QString pythonStatusString;
262
264 static QDir pythonCamitkModulePath;
265
267 static QString pythonSoLib;
268
270 static QString pythonVersion;
271
273 static QString systemPythonExecutable;
274
276 static bool isLocked;
277
279 static QMap<QObject*, QPair<py::object, py::dict>> pythonStateMap;
280
282 static QString currentVirtualEnvPath;
283 static QString currentScriptPath;
284 static py::module_ currentModule;
285
286};
287
288} // namespace camitk
289
290#endif // __PYTHON_MANAGER__
291#endif // PYTHON_BINDING
#define CAMITK_API
Definition CamiTKAPI.h:66
Definition Action.cpp:40