42 std::string path(
".");
43 std::string cmd = python_script;
46 if (python_script.rfind(
"/") != std::string::npos) {
47 path = python_script.substr(0, python_script.rfind(
"/"));
48 cmd = python_script.substr(python_script.rfind(
"/") + 1);
50 cmd = cmd.substr(0, cmd.find(
".py"));
58#if PY_MAJOR_VERSION >= 3
59 wchar_t** targs =
new wchar_t*[nargs + 1];
61#if PY_MINOR_VERSION >=5
62 targs[0] = Py_DecodeLocale(python_script.c_str(),NULL);
63 for (
int i = 0; i < nargs; i++)
64 targs[i + 1] = Py_DecodeLocale(args[i],NULL);
66 PyObject *tmpstr = PyUnicode_FromString(python_script.c_str());
67 targs[0] = PyUnicode_AsWideCharString(tmpstr,NULL);
69 for (
int i = 0; i < nargs; i++){
70 tmpstr = PyUnicode_FromString(args[i]);
71 targs[i + 1] = PyUnicode_AsWideCharString(tmpstr,NULL);
77 PySys_SetArgvEx(nargs+1, targs,1);
78 for(
int i=0;i<nargs+1; i++) PyMem_RawFree(targs[i]);
81 char** targs =
new char*[nargs + 1];
82 targs[0] = (
char*) python_script.c_str();
83 for (
int i = 0; i < nargs; i++)
84 targs[i + 1] = args[i];
86 PySys_SetArgvEx(nargs+1, targs, 1);
90 PyObject* script =
nullptr;
91 PyObject* process =
nullptr;
92 PyObject* p_main =
nullptr;
93 PyObject* py_list =
nullptr;
94 PyObject* p_process =
nullptr;
98 script = PyImport_ImportModule(cmd.c_str());
104 throw std::runtime_error(
"[ ConfigurePython ]: Problem loading python script.");
110 PyObject* pCMod = PyObject_GetAttrString(script,
"HpstrConf");
113 throw std::runtime_error(
"[ ConfigurePython ]: Problem loading configuration");
116 PyObject* p_process_class = PyObject_GetAttrString(pCMod,
"Process");
118 if (p_process_class == 0) {
120 throw std::runtime_error(
"[ ConfigurePython ]: Problem loading Process class");
123 p_process = PyObject_GetAttrString(p_process_class,
"lastProcess");
124 Py_DECREF(p_process_class);
125 if (p_process == 0) {
127 throw std::runtime_error(
"[ ConfigurePython ]: Problem loading Process class");
134 PyObject* p_sequence = PyObject_GetAttrString(p_process,
"sequence");
135 if (!PyList_Check(p_sequence)) {
136 throw std::runtime_error(
"[ ConfigurePython ]: Sequence is not a python list as expected.");
139 for (Py_ssize_t i = 0; i < PyList_Size(p_sequence); i++) {
140 PyObject* processor = PyList_GetItem(p_sequence, i);
146 PyObject* params = PyObject_GetAttrString(processor,
"parameters");
147 if (params != 0 && PyDict_Check(params)) {
148 PyObject *key(0), *value(0);
151 while (PyDict_Next(params, &pos, &key, &value)) {
152#if PY_MAJOR_VERSION >= 3
153 PyObject* pyStr = PyUnicode_AsEncodedString(key,
"utf-8",
"Error ~");
154 std::string skey = PyBytes_AS_STRING(pyStr);
156 if (PyLong_Check(value)) {
159 }
else if (PyFloat_Check(value)) {
162 }
else if (PyUnicode_Check(value)) {
163 PyObject* pyStr = PyUnicode_AsEncodedString(value,
"utf-8",
"Error ~");
167 }
else if (PyList_Check(value)) {
168 if (PyList_Size(value) > 0) {
169 PyObject* vec0 = PyList_GetItem(value, 0);
170 if (PyLong_Check(vec0)) {
171 std::vector<int> vals;
172 for (Py_ssize_t j = 0; j < PyList_Size(value); j++)
173 vals.push_back(PyLong_AsLong(PyList_GetItem(value, j)));
176 }
else if (PyFloat_Check(vec0)) {
177 std::vector<double> vals;
178 for (Py_ssize_t j = 0; j < PyList_Size(value); j++)
179 vals.push_back(PyFloat_AsDouble(PyList_GetItem(value, j)));
182 }
else if (PyUnicode_Check(vec0)) {
183 std::vector<std::string> vals;
184 for (Py_ssize_t j = 0; j < PyList_Size(value); j++){
185 PyObject* pyStr = PyUnicode_AsEncodedString(PyList_GetItem(value, j),
"utf-8",
"Error ~");
186 vals.push_back( PyBytes_AS_STRING(pyStr));
196 std::string skey = PyString_AsString(key);
197 if (PyInt_Check(value)) {
200 }
else if (PyFloat_Check(value)) {
203 }
else if (PyString_Check(value)) {
206 }
else if (PyList_Check(value)) {
207 if (PyList_Size(value) > 0) {
208 PyObject* vec0 = PyList_GetItem(value, 0);
209 if (PyInt_Check(vec0)) {
210 std::vector<int> vals;
211 for (Py_ssize_t j = 0; j < PyList_Size(value); j++)
212 vals.push_back(PyInt_AsLong(PyList_GetItem(value, j)));
215 }
else if (PyFloat_Check(vec0)) {
216 std::vector<double> vals;
217 for (Py_ssize_t j = 0; j < PyList_Size(value); j++)
218 vals.push_back(PyFloat_AsDouble(PyList_GetItem(value, j)));
221 }
else if (PyString_Check(vec0)) {
222 std::vector<std::string> vals;
223 for (Py_ssize_t j = 0; j < PyList_Size(value); j++)
224 vals.push_back(PyString_AsString(PyList_GetItem(value, j)));
236 Py_DECREF(p_sequence);
238 py_list = PyObject_GetAttrString(p_process,
"input_files");
239 if (!PyList_Check(py_list)) {
240 throw std::runtime_error(
"[ ConfigurePython ]: Input files is not a python list as expected.");
243 for (Py_ssize_t i = 0; i < PyList_Size(py_list); i++) {
244 PyObject* elem = PyList_GetItem(py_list, i);
245#if PY_MAJOR_VERSION >= 3
246 PyObject* pyStr = PyUnicode_AsEncodedString(elem,
"utf-8",
"Error ~");
256 py_list = PyObject_GetAttrString(p_process,
"output_files");
257 if (!PyList_Check(py_list)) {
258 throw std::runtime_error(
"[ ConfigurePython ]: Output files is not a python list as expected.");
261 for (Py_ssize_t i = 0; i < PyList_Size(py_list); i++) {
262 PyObject* elem = PyList_GetItem(py_list, i);
263#if PY_MAJOR_VERSION >= 3
264 PyObject* pyStr = PyUnicode_AsEncodedString(elem,
"utf-8",
"Error ~");
273 py_list = PyObject_GetAttrString(p_process,
"libraries");
274 if (!PyList_Check(py_list)) {
275 throw std::runtime_error(
"[ ConfigurePython ]: libraries is not a python list as expected.");
278 for (Py_ssize_t i = 0; i < PyList_Size(py_list); i++) {
279 PyObject* elem = PyList_GetItem(py_list, i);
280#if PY_MAJOR_VERSION >= 3
281 PyObject* pyStr = PyUnicode_AsEncodedString(elem,
"utf-8",
"Error ~");
282 libraries_.push_back(PyBytes_AS_STRING(pyStr));
285 libraries_.push_back(PyString_AsString(elem));
290 }
catch (std::exception& e) {
291 std::cout << e.what() << std::endl;