-
Notifications
You must be signed in to change notification settings - Fork 5
/
PythonInfo.cpp
61 lines (47 loc) · 1.91 KB
/
PythonInfo.cpp
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
// Copyright (c) 2020 Nicholas Corgan
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Plugin.hpp>
#include <Pothos/Proxy.hpp>
#include <Poco/String.h>
#include <json.hpp>
#include <sstream>
static std::string __getPythonInfoJSON()
{
auto env = Pothos::ProxyEnvironment::make("python");
auto sys = env->findProxy("sys");
nlohmann::json topObj;
auto& pythonInfo = topObj["Python Info"];
pythonInfo["Exec Prefix"] = sys.get<std::string>("exec_prefix");
#if PY_MAJOR_VERSION >= 3
pythonInfo["Implementation"] = sys.get("implementation").get<std::string>("name");
pythonInfo["Cache Tag"] = sys.get("implementation").get<std::string>("cache_tag");
#endif
auto& versionInfo = pythonInfo["Version Info"];
auto sysVersionInfo = sys.get("version_info");
versionInfo["Major"] = sysVersionInfo.call<int, int>("__getitem__", 0);
versionInfo["Minor"] = sysVersionInfo.call<int, int>("__getitem__", 1);
versionInfo["Patch"] = sysVersionInfo.call<int, int>("__getitem__", 2);
versionInfo["Release Level"] = sysVersionInfo.call<std::string>("__getitem__", 3);
versionInfo["Serial"] = sysVersionInfo.call<int, int>("__getitem__", 4);
auto fullVersionString = sys.get<std::string>("version");
versionInfo["Version String"] = fullVersionString.substr(0, fullVersionString.find(" "));
std::string hexVersion;
std::stringstream hexVersionStream;
hexVersionStream << "0x";
hexVersionStream << std::hex << sys.get<size_t>("hexversion");
hexVersionStream >> hexVersion;
versionInfo["Version Hex"] = hexVersion;
return topObj.dump();
}
static std::string getPythonInfoJSON()
{
// Only do this once.
static const std::string pythonInfoJSON = __getPythonInfoJSON();
return pythonInfoJSON;
}
pothos_static_block(registerPythonInfo)
{
Pothos::PluginRegistry::addCall(
"/devices/python/info",
Pothos::Callable(&getPythonInfoJSON));
}