| 53 | |
| 54 | |
| 55 | PythonConfig 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 |
no test coverage detected