-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtps_globals.cpp
More file actions
159 lines (125 loc) · 3.61 KB
/
gtps_globals.cpp
File metadata and controls
159 lines (125 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* GTlab - Gas Turbine laboratory
* Source File: gtps_globals.cpp
*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR)
*
* Created on: 30.11.2022
* Author: Marvin Noethen (DLR AT-TWK)
*/
#include <QVector>
#include <QString>
#include "gt_globals.h"
#include "gt_moduleinterface.h"
#include "gt_settings.h"
#include "gtps_constants.h"
#include "gtps_globals.h"
#include <gt_application.h>
#include <QCoreApplication>
#include <QDir>
#include <QDirIterator>
namespace
{
QString pythonExe()
{
#ifdef _WINDOWS
return "python.exe";
#else
return "bin/python";
#endif
}
}
void
gtps::settings::setSetting(const QString& settingId, const QVariant& value)
{
gtApp->settings()->setSetting(gtps::settings::path(settingId), value);
}
QVariant
gtps::settings::getSetting(const QString& settingId)
{
return gtApp->settings()->getSetting(gtps::settings::path(settingId));
}
QString
gtps::apiVersionStr(const GtVersionNumber &version)
{
return {QString::number(version.major()) + "." +
QString::number(version.minor())};
}
QString
gtps::python::module::pythonModuleId(const GtVersionNumber& version)
{
return QString{"Python Module (Python %1)"}
.arg(gtps::apiVersionStr(version));
}
QVector<GtVersionNumber>
gtps::python::version::supportedVersions()
{
QVector<GtVersionNumber> available;
QString moduleDir = QCoreApplication::applicationDirPath() + "/modules";
static QRegularExpression pymodRe("(lib)?(?i)(GTlabPython)([0-9])([0-9]+)(-d)?.(dll|so|dylib)$");
QDirIterator it(moduleDir, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString filePath = it.next();
QFileInfo fileInfo(filePath);
QString fileName = fileInfo.fileName();
auto matcher = pymodRe.match(fileName);
if (matcher.hasMatch())
{
available.push_back(GtVersionNumber(matcher.captured(3).toInt(),
matcher.captured(4).toInt()));
}
}
std::sort(available.begin(), available.end());
return available;
}
bool
gtps::python::version::isSupported(const GtVersionNumber& version)
{
auto versions = supportedVersions();
auto iter = std::find_if(versions.begin(), versions.end(),
[&version] (const GtVersionNumber& supported) {
return supported.major() == version.major() &&
supported.minor() == version.minor();});
return iter != versions.end() ? true : false;
}
QString
gtps::settings::path(const QString& settingId)
{
return moduleSettingPath(GT_MODULENAME(), settingId);
}
QString
gtps::embeddedPythonPath()
{
// test, if there is an embedded python distribution
auto pythonDir = QDir(QCoreApplication::applicationDirPath()
+ "/../lib/python");
QString pythonPath = pythonDir.absoluteFilePath(pythonExe());
if (!QFile(pythonPath).exists()) return {};
return pythonPath;
}
bool
gtps::settings::useEmbeddedPython()
{
QVariant useEmbeddedVar = getSetting(gtps::constants::USE_EMBEDDED_S_ID);
bool useEmbedded = true;
if (useEmbeddedVar.isValid()) useEmbedded = useEmbeddedVar.toBool();
return useEmbedded && !gtps::embeddedPythonPath().isEmpty();
}
QString
gtps::pythonPath()
{
if (gtps::settings::useEmbeddedPython())
{
return gtps::embeddedPythonPath();
}
else
{
return gtps::settings::customPythonPath();
}
}
QString
gtps::settings::customPythonPath()
{
return getSetting(gtps::constants::PYEXE_S_ID).toString();
}