MCPcopy Create free account
hub / github.com/NatLabRockies/SAM / ReadPythonConfig

Function ReadPythonConfig

src/pythonhandler.cpp:55–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

53
54
55PythonConfig ReadPythonConfig(const std::string& configPath) {
56 // load python configuration
57 rapidjson::Document python_config_root;
58 std::ifstream python_config_doc(configPath);
59 if (python_config_doc.fail())
60 throw std::runtime_error("Could not open " + configPath );
61
62
63#ifdef __WXMSW__
64 // check for byte-order mark indicating UTF-8 and skip if it exists since it's not JSON-compatible
65 char a, b, c;
66 a = (char)python_config_doc.get();
67 b = (char)python_config_doc.get();
68 c = (char)python_config_doc.get();
69 if (a != (char)0xEF || b != (char)0xBB || c != (char)0xBF) {
70 python_config_doc.seekg(0);
71 }
72#endif
73
74 std::ostringstream tmp;
75 tmp << python_config_doc.rdbuf();
76 python_config_root.Parse(tmp.str().c_str());
77 if (python_config_root.HasParseError()) {
78 std::string s = rapidjson::GetParseError_En(python_config_root.GetParseError());
79 throw std::runtime_error(s);
80 }
81
82 if (!python_config_root.HasMember("miniconda_version"))
83 throw std::runtime_error("Missing key 'miniconda_version' in " + configPath);
84 if (!python_config_root.HasMember("python_version"))
85 throw std::runtime_error("Missing key 'python_version' in " + configPath);
86 if (!python_config_root.HasMember("exec_path"))
87 throw std::runtime_error("Missing key 'exec_path' in " + configPath);
88 if (!python_config_root.HasMember("pip_path"))
89 throw std::runtime_error("Missing key 'pip_path' in " + configPath);
90 if (!python_config_root.HasMember("packages"))
91 throw std::runtime_error("Missing key 'packages' in " + configPath);
92
93 std::vector<std::string> packages;
94 for (auto &i : python_config_root["packages"].GetArray())
95 packages.push_back(i.GetString());
96
97 std::unordered_map<std::string, std::string> options;
98
99 if (python_config_root.HasMember("options")){
100 rapidjson::Value json_val = python_config_root["options"].GetObject();
101 for (rapidjson::Value::ConstMemberIterator itr = json_val.MemberBegin(); itr != json_val.MemberEnd(); ++itr) {
102 options.insert({ itr->name.GetString(),itr->value.GetString() });
103 }
104 }
105
106 PythonConfig config = {python_config_root["python_version"].GetString(),
107 python_config_root["miniconda_version"].GetString(),
108 python_config_root["exec_path"].GetString(),
109 python_config_root["pip_path"].GetString(),
110 packages,
111 options};
112

Callers 1

LoadPythonConfigMethod · 0.85

Calls 4

ParseMethod · 0.45
GetArrayMethod · 0.45
GetStringMethod · 0.45
GetObjectMethod · 0.45

Tested by

no test coverage detected