ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PythonConfig.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <QString>
11 #include <QtGlobal>
12 
13 // Qt5/Qt6 Compatibility
14 #include <QtCompat.h>
15 
16 #include <memory>
17 
18 #undef slots
19 #include <pybind11/pybind11.h>
20 
21 struct PyVenvCfg;
22 class QProcess;
23 class PythonConfigPaths;
24 class QWidget;
25 
27 struct Version
28 {
29  constexpr Version() = default;
30 
31  constexpr Version(uint16_t major_, uint16_t minor_, uint16_t patch_)
32  : versionMajor(major_), versionMinor(minor_), versionPatch(patch_)
33  {
34  }
35 
36  explicit Version(const QtCompatStringRef &versionStr);
37 
50 
51  bool isNull() const
52  {
53  return versionMajor == 0 && versionMinor == 0 && versionPatch == 0;
54  };
55 
56  bool operator==(const Version &other) const;
57 
58  uint16_t versionMajor{0};
59  uint16_t versionMinor{0};
60  uint16_t versionPatch{0};
61 };
62 
64 constexpr Version PythonVersion(PY_MAJOR_VERSION, PY_MINOR_VERSION, PY_MICRO_VERSION);
65 
71 class PythonConfig final
72 {
73  public:
74  enum class Type
75  {
76  Venv,
77  Conda,
78  System,
79  Bundled
80  };
81 
82  PythonConfig() = default;
83 
84  Type type() const
85  {
86  return m_type;
87  }
88 
89  template <class ostream> friend ostream &operator<<(ostream &o, Type type)
90  {
91  switch (type)
92  {
93  case Type::Venv:
94  o << "Venv";
95  break;
96  case Type::Conda:
97  o << "Conda";
98  break;
99  case Type::System:
100  o << "System";
101  break;
102  case Type::Bundled:
103  o << "Bundled";
104  break;
105  }
106  return o;
107  }
108 
109  const QString &pythonHome() const
110  {
111  return m_pythonHome;
112  }
113 
116  void preparePythonProcess(QProcess &pythonProcess) const;
117 
121 
128  Version getVersion() const;
129 
137  bool validateAndDisplayErrors(QWidget *parent = nullptr) const;
138 
139  static bool IsInsideEnvironment();
141 
148  void initDefault();
151  void initBundled();
155  void initFromLocation(const QString &prefix);
157  void initCondaEnv(const QString &condaPrefix);
159  void initVenv(const QString &venvPrefix);
160 
161  void initFromPythonExecutable(const QString &pythonExecutable);
162 
163  template <class ostream> friend ostream &operator<<(ostream &o, const PythonConfig &config)
164  {
165  o << "PythonConfig { type: " << config.m_type << ", home: '" << config.m_pythonHome
166  << "', path: '" << config.m_pythonPath << "'}";
167  return o;
168  }
169 
170  private:
171  QString m_pythonHome{};
172  QString m_pythonPath{};
173  Type m_type{Type::Bundled};
174 };
175 
183 class PythonConfigPaths final
184 {
185  friend PythonConfig;
186 
187  public:
189  PythonConfigPaths() = default;
190 
192  bool isSet() const;
193 
195  const wchar_t *pythonHome() const;
196 
198  const wchar_t *pythonPath() const;
199 
200  private:
203  std::unique_ptr<wchar_t[]> m_pythonHome{};
205  std::unique_ptr<wchar_t[]> m_pythonPath{};
206 };
constexpr Version PythonVersion(PY_MAJOR_VERSION, PY_MINOR_VERSION, PY_MICRO_VERSION)
Python Version the plugin was compiled against.
QStringView QtCompatStringRef
Definition: QtCompat.h:227
const wchar_t * pythonPath() const
Returns the pythonPath.
const wchar_t * pythonHome() const
Returns the pythonHome.
PythonConfigPaths()=default
Default ctor, does not initialize pythonHome & pythonPath.
bool isSet() const
returns true if both paths are non empty
PythonConfig()=default
void initBundled()
Version getVersion() const
static PythonConfig fromContainingEnvironment()
bool validateAndDisplayErrors(QWidget *parent=nullptr) const
const QString & pythonHome() const
Definition: PythonConfig.h:109
friend ostream & operator<<(ostream &o, Type type)
Definition: PythonConfig.h:89
Type type() const
Definition: PythonConfig.h:84
PythonConfigPaths pythonCompatiblePaths() const
friend ostream & operator<<(ostream &o, const PythonConfig &config)
Definition: PythonConfig.h:163
void initFromLocation(const QString &prefix)
void preparePythonProcess(QProcess &pythonProcess) const
void initFromPythonExecutable(const QString &pythonExecutable)
static bool IsInsideEnvironment()
void initDefault()
void initCondaEnv(const QString &condaPrefix)
Initialize the paths to use the conda environment stored at condaPrefix.
void initVenv(const QString &venvPrefix)
Initialize the paths to use the python venv stored at venvPrefix.
Simple representation of a SemVer version.
Definition: PythonConfig.h:28
constexpr Version(uint16_t major_, uint16_t minor_, uint16_t patch_)
Definition: PythonConfig.h:31
bool isNull() const
Definition: PythonConfig.h:51
constexpr Version()=default
bool operator==(const Version &other) const
uint16_t versionPatch
Definition: PythonConfig.h:60
bool isCompatibleWithCompiledVersion() const
uint16_t versionMinor
Definition: PythonConfig.h:59
uint16_t versionMajor
Definition: PythonConfig.h:58