[579] | 1 | /* |
---|
[1] | 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
[453] | 4 | * Copyright (c) 2009-2014 GeoLabs SARL |
---|
[1] | 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include "service_internal_python.h" |
---|
| 26 | |
---|
[579] | 27 | /** |
---|
| 28 | * The state for the zoo Python module |
---|
| 29 | */ |
---|
[392] | 30 | struct module_state { |
---|
[579] | 31 | PyObject *error; |
---|
[392] | 32 | }; |
---|
| 33 | |
---|
| 34 | #if PY_MAJOR_VERSION >= 3 |
---|
| 35 | #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) |
---|
[453] | 36 | #define PyInt_FromLong PyLong_FromLong |
---|
| 37 | #define PyInt_AsLong PyLong_AsLong |
---|
| 38 | #define PyString_FromString PyUnicode_FromString |
---|
| 39 | #define PyString_FromStringAndSize PyUnicode_FromStringAndSize |
---|
| 40 | #define PyString_Check PyUnicode_Check |
---|
| 41 | #define PyString_AsString _PyUnicode_AsString |
---|
| 42 | #define PyString_Size PyUnicode_GetSize |
---|
[392] | 43 | #else |
---|
| 44 | #define GETSTATE(m) (&_state) |
---|
| 45 | static struct module_state _state; |
---|
| 46 | #endif |
---|
| 47 | |
---|
[579] | 48 | /** |
---|
| 49 | * The exception for the zoo Python module |
---|
| 50 | */ |
---|
[368] | 51 | static PyObject* ZooError; |
---|
| 52 | |
---|
[579] | 53 | /** |
---|
| 54 | * Function definitions for the zoo Python Module |
---|
| 55 | * |
---|
| 56 | * Define the following functions available from a service loaded and running |
---|
| 57 | * from the ZOO-Kernel Python environment: |
---|
| 58 | * - "_" corresponding to the PythonTranslate function |
---|
| 59 | * - "updte_status" corresponding to the PythonUpdateStatus function |
---|
| 60 | * @see PythonTranslate, PythonUpdateStatus |
---|
| 61 | */ |
---|
[368] | 62 | PyMethodDef zooMethods[] = { |
---|
[376] | 63 | {"_", PythonTranslate, METH_VARARGS, "Translate a string using the zoo-services textdomain."}, |
---|
[368] | 64 | {"update_status", PythonUpdateStatus, METH_VARARGS, "Update status percentage of a running process."}, |
---|
| 65 | {NULL, NULL, 0, NULL} /* tempt not the blade, all fear the sentinel */ |
---|
| 66 | }; |
---|
| 67 | |
---|
[392] | 68 | #if PY_MAJOR_VERSION >= 3 |
---|
| 69 | |
---|
| 70 | static int myextension_traverse(PyObject *m, visitproc visit, void *arg) { |
---|
| 71 | Py_VISIT(GETSTATE(m)->error); |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | static int myextension_clear(PyObject *m) { |
---|
| 76 | Py_CLEAR(GETSTATE(m)->error); |
---|
| 77 | return 0; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | static struct PyModuleDef moduledef = { |
---|
| 81 | PyModuleDef_HEAD_INIT, |
---|
| 82 | "zoo", |
---|
| 83 | NULL, |
---|
| 84 | sizeof(struct module_state), |
---|
| 85 | zooMethods, |
---|
| 86 | NULL, |
---|
| 87 | myextension_traverse, |
---|
| 88 | myextension_clear, |
---|
| 89 | NULL |
---|
| 90 | }; |
---|
| 91 | #endif |
---|
| 92 | |
---|
[579] | 93 | /** |
---|
| 94 | * Function to create and initialize the zoo Python module |
---|
| 95 | * |
---|
| 96 | * @return the Python module (for Python versions < 3, nothing for version >=3) |
---|
| 97 | */ |
---|
[368] | 98 | PyMODINIT_FUNC init_zoo(){ |
---|
| 99 | PyObject *tmp,*d; |
---|
[392] | 100 | PyObject *module = |
---|
| 101 | #if PY_MAJOR_VERSION >= 3 |
---|
| 102 | PyModule_Create(&moduledef); |
---|
| 103 | #else |
---|
| 104 | Py_InitModule("zoo", zooMethods); |
---|
| 105 | #endif |
---|
| 106 | if (module == NULL){ |
---|
| 107 | #if PY_MAJOR_VERSION >= 3 |
---|
| 108 | return NULL; |
---|
| 109 | #else |
---|
[368] | 110 | return; |
---|
[392] | 111 | #endif |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | struct module_state *st = GETSTATE(module); |
---|
| 115 | |
---|
[368] | 116 | d = PyModule_GetDict(module); |
---|
| 117 | tmp = PyInt_FromLong(3); |
---|
| 118 | PyDict_SetItemString(d, "SERVICE_SUCCEEDED", tmp); |
---|
| 119 | Py_DECREF(tmp); |
---|
| 120 | |
---|
| 121 | tmp = PyInt_FromLong(4); |
---|
| 122 | PyDict_SetItemString(d, "SERVICE_FAILED", tmp); |
---|
| 123 | Py_DECREF(tmp); |
---|
| 124 | |
---|
[465] | 125 | tmp = PyString_FromString(ZOO_VERSION); |
---|
| 126 | PyDict_SetItemString(d, "VERSION", tmp); |
---|
| 127 | Py_DECREF(tmp); |
---|
| 128 | |
---|
[490] | 129 | ZooError = PyErr_NewException((char*)"zoo.error", NULL, NULL); |
---|
[368] | 130 | Py_INCREF(ZooError); |
---|
| 131 | PyModule_AddObject(module, "error", ZooError); |
---|
[392] | 132 | #if PY_MAJOR_VERSION >= 3 |
---|
| 133 | return module; |
---|
| 134 | #endif |
---|
[368] | 135 | } |
---|
| 136 | |
---|
[579] | 137 | /** |
---|
[580] | 138 | * Load a Python module then run the function corresponding to the service |
---|
[579] | 139 | * by passing the conf, inputs and outputs parameters by reference. |
---|
| 140 | * |
---|
| 141 | * @param main_conf the conf maps containing the main.cfg settings |
---|
| 142 | * @param request the map containing the HTTP request |
---|
| 143 | * @param s the service structure |
---|
| 144 | * @param real_inputs the maps containing the inputs |
---|
| 145 | * @param real_outputs the maps containing the outputs |
---|
| 146 | */ |
---|
[1] | 147 | int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
[451] | 148 | char *pythonpath; |
---|
| 149 | char *python_path; |
---|
[1] | 150 | maps* m=*main_conf; |
---|
| 151 | maps* inputs=*real_inputs; |
---|
| 152 | maps* outputs=*real_outputs; |
---|
[392] | 153 | map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd"); |
---|
| 154 | char *ntmp=tmp0->value; |
---|
[1] | 155 | map* tmp=NULL; |
---|
[451] | 156 | int hasToClean=0; |
---|
[1] | 157 | tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH"); |
---|
[63] | 158 | #ifdef DEBUG |
---|
[9] | 159 | fprintf(stderr,"PYTHON SUPPORT \n"); |
---|
[63] | 160 | #endif |
---|
[1] | 161 | if(tmp!=NULL){ |
---|
[63] | 162 | #ifdef DEBUG |
---|
[9] | 163 | fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value)); |
---|
[63] | 164 | #endif |
---|
[9] | 165 | python_path=(char*)malloc((strlen(tmp->value))*sizeof(char)); |
---|
| 166 | sprintf(python_path,"%s",tmp->value); |
---|
[451] | 167 | hasToClean=1; |
---|
[1] | 168 | } |
---|
| 169 | else{ |
---|
[490] | 170 | python_path=(char*)"."; |
---|
[1] | 171 | } |
---|
| 172 | tmp=NULL; |
---|
| 173 | tmp=getMap(request,"metapath"); |
---|
[392] | 174 | if(tmp!=NULL && strcmp(tmp->value,"")!=0){ |
---|
| 175 | pythonpath=(char*)malloc((4+strlen(python_path)+strlen(ntmp)+strlen(tmp->value))*sizeof(char)); |
---|
[1] | 176 | #ifdef WIN32 |
---|
[451] | 177 | sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); |
---|
[1] | 178 | #else |
---|
[451] | 179 | sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); |
---|
[1] | 180 | #endif |
---|
[392] | 181 | } |
---|
| 182 | else{ |
---|
| 183 | pythonpath=(char*)malloc((2+strlen(python_path)+strlen(ntmp))*sizeof(char)); |
---|
[1] | 184 | #ifdef WIN32 |
---|
| 185 | sprintf(pythonpath,"%s;%s",ntmp,python_path); |
---|
| 186 | #else |
---|
[392] | 187 | sprintf(pythonpath,"%s:%s",ntmp,python_path); |
---|
[1] | 188 | #endif |
---|
[392] | 189 | } |
---|
[67] | 190 | #ifdef DEBUG |
---|
[9] | 191 | fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); |
---|
[67] | 192 | #endif |
---|
[1] | 193 | #ifndef WIN32 |
---|
| 194 | setenv("PYTHONPATH",pythonpath,1); |
---|
| 195 | #else |
---|
| 196 | SetEnvironmentVariable("PYTHONPATH",pythonpath); |
---|
[364] | 197 | char* toto=(char*)malloc((strlen(pythonpath)+12)*sizeof(char)); |
---|
| 198 | sprintf(toto,"PYTHONPATH=%s",pythonpath); |
---|
| 199 | putenv(toto); |
---|
[451] | 200 | free(toto); |
---|
[1] | 201 | #endif |
---|
[451] | 202 | if(hasToClean>0) |
---|
| 203 | free(python_path); |
---|
[1] | 204 | free(pythonpath); |
---|
[9] | 205 | |
---|
[295] | 206 | PyThreadState *mainstate; |
---|
[392] | 207 | #if PY_MAJOR_VERSION >= 3 |
---|
| 208 | PyImport_AppendInittab("zoo", init_zoo); |
---|
| 209 | #else |
---|
[295] | 210 | PyEval_InitThreads(); |
---|
[392] | 211 | #endif |
---|
[1] | 212 | Py_Initialize(); |
---|
[392] | 213 | #if PY_MAJOR_VERSION >= 3 |
---|
| 214 | PyEval_InitThreads(); |
---|
| 215 | PyImport_ImportModule("zoo"); |
---|
| 216 | #else |
---|
[368] | 217 | init_zoo(); |
---|
[392] | 218 | #endif |
---|
[295] | 219 | mainstate = PyThreadState_Swap(NULL); |
---|
| 220 | PyEval_ReleaseLock(); |
---|
| 221 | PyGILState_STATE gstate; |
---|
| 222 | gstate = PyGILState_Ensure(); |
---|
[1] | 223 | PyObject *pName, *pModule, *pFunc; |
---|
| 224 | tmp=getMap(s->content,"serviceProvider"); |
---|
[505] | 225 | map* mp=getMap(request,"metapath"); |
---|
| 226 | if(tmp!=NULL){ |
---|
| 227 | if(mp!=NULL && strlen(mp->value)>0){ |
---|
| 228 | char *mps=zStrdup(mp->value); |
---|
| 229 | int i,len=strlen(mps); |
---|
| 230 | int j=0; |
---|
| 231 | for(i=0;i<len;i++){ |
---|
| 232 | if(mps[i]=='/'){ |
---|
| 233 | mps[i]='.'; |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | char *mn=(char*)malloc((strlen(mps)+strlen(tmp->value)+2)*sizeof(char)); |
---|
| 237 | sprintf(mn,"%s.%s",mps,tmp->value); |
---|
| 238 | pName = PyString_FromString(mn); |
---|
| 239 | free(mn); |
---|
| 240 | free(mps); |
---|
| 241 | } |
---|
| 242 | else{ |
---|
| 243 | pName = PyString_FromString(tmp->value); |
---|
| 244 | } |
---|
| 245 | } |
---|
[9] | 246 | else{ |
---|
[576] | 247 | errorException (m, "Unable to parse serviceProvider please check your zcfg file.", "NoApplicableCode", NULL); |
---|
[9] | 248 | exit(-1); |
---|
| 249 | } |
---|
[1] | 250 | pModule = PyImport_Import(pName); |
---|
| 251 | int res=SERVICE_FAILED; |
---|
| 252 | if (pModule != NULL) { |
---|
| 253 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
| 254 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
[114] | 255 | PyObject *pValue; |
---|
[1] | 256 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
| 257 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
| 258 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
| 259 | PyObject *pArgs=PyTuple_New(3); |
---|
[114] | 260 | if (!pArgs) |
---|
| 261 | return -1; |
---|
[1] | 262 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
| 263 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
| 264 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
| 265 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 266 | if (pValue != NULL) { |
---|
| 267 | res=PyInt_AsLong(pValue); |
---|
[9] | 268 | freeMaps(real_outputs); |
---|
| 269 | free(*real_outputs); |
---|
[59] | 270 | freeMaps(main_conf); |
---|
| 271 | free(*main_conf); |
---|
[57] | 272 | *main_conf=mapsFromPyDict(arg1); |
---|
[9] | 273 | *real_outputs=mapsFromPyDict(arg3); |
---|
[1] | 274 | #ifdef DEBUG |
---|
| 275 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
| 276 | dumpMaps(inputs); |
---|
[364] | 277 | dumpMaps(*real_outputs); |
---|
[1] | 278 | #endif |
---|
[9] | 279 | }else{ |
---|
[576] | 280 | PythonZooReport(m,tmp->value,0); |
---|
[295] | 281 | res=-1; |
---|
[1] | 282 | } |
---|
| 283 | } |
---|
| 284 | else{ |
---|
| 285 | char tmpS[1024]; |
---|
[295] | 286 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
[576] | 287 | errorException(m,tmpS,"NoApplicableCode",NULL); |
---|
[295] | 288 | res=-1; |
---|
[1] | 289 | } |
---|
| 290 | } else{ |
---|
[576] | 291 | PythonZooReport(m,tmp->value,1); |
---|
[295] | 292 | res=-1; |
---|
[1] | 293 | } |
---|
[392] | 294 | #if PY_MAJOR_VERSION < 3 |
---|
[295] | 295 | PyGILState_Release(gstate); |
---|
[478] | 296 | PyEval_AcquireLock(); |
---|
[392] | 297 | #endif |
---|
[295] | 298 | PyThreadState_Swap(mainstate); |
---|
[1] | 299 | Py_Finalize(); |
---|
| 300 | return res; |
---|
| 301 | } |
---|
| 302 | |
---|
[579] | 303 | /** |
---|
[580] | 304 | * Report Python error which may occur on loading the Python module or at |
---|
[579] | 305 | * runtime. |
---|
| 306 | * |
---|
| 307 | * @param m the conf maps containing the main.cfg settings |
---|
| 308 | * @param module the service name |
---|
| 309 | * @param load 1 if the Python module was not loaded yet |
---|
| 310 | */ |
---|
[576] | 311 | void PythonZooReport(maps* m,const char* module,int load){ |
---|
| 312 | PyObject *pName, *pModule, *pFunc; |
---|
| 313 | PyObject *ptype, *pvalue, *ptraceback,*pValue,*pArgs; |
---|
| 314 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
| 315 | char *pStrErrorMessage = PyString_AsString(pvalue); |
---|
| 316 | char *tmp0=_("Python module %s cannot be loaded. Message: %s\n"); |
---|
| 317 | |
---|
| 318 | PyObject *trace=PyObject_Str(pvalue); |
---|
| 319 | char *pbt=NULL; |
---|
| 320 | if(PyString_Check(trace)){ |
---|
| 321 | pbt=(char*)malloc((7+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 322 | sprintf(pbt,"TRACE: %s",PyString_AsString(trace)); |
---|
| 323 | } |
---|
| 324 | else |
---|
| 325 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 326 | |
---|
| 327 | trace=NULL; |
---|
| 328 | |
---|
| 329 | trace=PyObject_Str(ptype); |
---|
| 330 | if(PyString_Check(trace)){ |
---|
| 331 | char *tpbt=zStrdup(pbt); |
---|
| 332 | if(pbt!=NULL) |
---|
| 333 | free(pbt); |
---|
| 334 | pbt=(char*)malloc((1+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 335 | sprintf(pbt,"%s\n%s",tpbt,PyString_AsString(trace)); |
---|
| 336 | free(tpbt); |
---|
| 337 | } |
---|
| 338 | else |
---|
| 339 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 340 | |
---|
| 341 | if(ptraceback!=NULL){ |
---|
| 342 | char *tpbt=zStrdup(pbt); |
---|
| 343 | pName = PyString_FromString("traceback"); |
---|
| 344 | pModule = PyImport_Import(pName); |
---|
| 345 | pArgs = PyTuple_New(1); |
---|
| 346 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
| 347 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
| 348 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 349 | trace=NULL; |
---|
| 350 | trace=PyObject_Str(pValue); |
---|
| 351 | if(PyString_Check(trace)){ |
---|
| 352 | if(pbt!=NULL) |
---|
| 353 | free(pbt); |
---|
| 354 | pbt=(char*)malloc((90+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 355 | sprintf(pbt,_("%s\nUnable to run your python process properly. Please check the following messages : %s"),tpbt,PyString_AsString(trace)); |
---|
| 356 | } |
---|
| 357 | else{ |
---|
| 358 | if(pbt!=NULL) |
---|
| 359 | free(pbt); |
---|
| 360 | pbt=(char*)malloc((90+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 361 | sprintf(pbt,_("%s \n Unable to run your python process properly. Unable to provide any futher informations."),tpbt); |
---|
| 362 | } |
---|
| 363 | free(tpbt); |
---|
| 364 | } |
---|
| 365 | if(load>0){ |
---|
| 366 | char *tmpS=(char*)malloc((strlen(tmp0)+strlen(module)+strlen(pbt)+1)*sizeof(char)); |
---|
| 367 | sprintf(tmpS,tmp0,module,pbt); |
---|
| 368 | errorException(m,tmpS,"NoApplicableCode",NULL); |
---|
| 369 | free(tmpS); |
---|
| 370 | }else |
---|
| 371 | errorException(m,pbt,"NoApplicableCode",NULL); |
---|
| 372 | free(pbt); |
---|
| 373 | } |
---|
| 374 | |
---|
[579] | 375 | /** |
---|
| 376 | * Convert a maps to a Python dictionary |
---|
| 377 | * |
---|
| 378 | * @param t the maps to convert |
---|
| 379 | * @return a new PyDictObject containing the converted maps |
---|
| 380 | * @see PyDict_FromMap |
---|
| 381 | * @warning make sure to free ressources returned by this function |
---|
| 382 | */ |
---|
[1] | 383 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
| 384 | PyObject* res=PyDict_New( ); |
---|
| 385 | maps* tmp=t; |
---|
| 386 | while(tmp!=NULL){ |
---|
[295] | 387 | PyObject* value=(PyObject*)PyDict_FromMap(tmp->content); |
---|
[453] | 388 | PyObject* name=PyString_FromString(tmp->name); |
---|
[295] | 389 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 390 | fprintf(stderr,"Unable to set map value ..."); |
---|
| 391 | return NULL; |
---|
[1] | 392 | } |
---|
[295] | 393 | Py_DECREF(name); |
---|
[1] | 394 | tmp=tmp->next; |
---|
| 395 | } |
---|
| 396 | return (PyDictObject*) res; |
---|
| 397 | } |
---|
| 398 | |
---|
[579] | 399 | /** |
---|
| 400 | * Convert a map to a Python dictionary |
---|
| 401 | * |
---|
| 402 | * @param t the map to convert |
---|
| 403 | * @return a new PyDictObject containing the converted maps |
---|
| 404 | * @warning make sure to free ressources returned by this function |
---|
| 405 | */ |
---|
[1] | 406 | PyDictObject* PyDict_FromMap(map* t){ |
---|
| 407 | PyObject* res=PyDict_New( ); |
---|
| 408 | map* tmp=t; |
---|
[360] | 409 | int hasSize=0; |
---|
| 410 | map* isArray=getMap(tmp,"isArray"); |
---|
[58] | 411 | map* size=getMap(tmp,"size"); |
---|
[360] | 412 | map* tmap=getMapType(tmp); |
---|
[1] | 413 | while(tmp!=NULL){ |
---|
[453] | 414 | PyObject* name=PyString_FromString(tmp->name); |
---|
[360] | 415 | if(strcasecmp(tmp->name,"value")==0) { |
---|
| 416 | if(isArray!=NULL){ |
---|
| 417 | map* len=getMap(tmp,"length"); |
---|
| 418 | int cnt=atoi(len->value); |
---|
| 419 | PyObject* value=PyList_New(cnt); |
---|
| 420 | PyObject* mvalue=PyList_New(cnt); |
---|
| 421 | PyObject* svalue=PyList_New(cnt); |
---|
| 422 | |
---|
| 423 | for(int i=0;i<cnt;i++){ |
---|
| 424 | |
---|
| 425 | map* vMap=getMapArray(tmp,"value",i); |
---|
| 426 | map* sMap=getMapArray(tmp,"size",i); |
---|
| 427 | |
---|
| 428 | if(vMap!=NULL){ |
---|
| 429 | |
---|
| 430 | PyObject* lvalue; |
---|
| 431 | PyObject* lsvalue; |
---|
| 432 | if(sMap==NULL){ |
---|
| 433 | lvalue=PyString_FromString(vMap->value); |
---|
| 434 | lsvalue=Py_None; |
---|
| 435 | } |
---|
[453] | 436 | else{ |
---|
[360] | 437 | lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value)); |
---|
| 438 | lsvalue=PyString_FromString(sMap->value); |
---|
| 439 | hasSize=1; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | if(PyList_SetItem(value,i,lvalue)<0){ |
---|
| 443 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 444 | return NULL; |
---|
| 445 | } |
---|
| 446 | if(PyList_SetItem(svalue,i,lsvalue)<0){ |
---|
| 447 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 448 | return NULL; |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | map* mMap=getMapArray(tmp,tmap->name,i); |
---|
| 453 | PyObject* lmvalue; |
---|
| 454 | if(mMap!=NULL){ |
---|
| 455 | lmvalue=PyString_FromString(mMap->value); |
---|
| 456 | }else |
---|
| 457 | lmvalue=Py_None; |
---|
| 458 | |
---|
| 459 | if(PyList_SetItem(mvalue,i,lmvalue)<0){ |
---|
| 460 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 461 | return NULL; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 467 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 468 | return NULL; |
---|
| 469 | } |
---|
| 470 | if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){ |
---|
| 471 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 472 | return NULL; |
---|
| 473 | } |
---|
| 474 | if(hasSize>0) |
---|
| 475 | if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){ |
---|
| 476 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 477 | return NULL; |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | else if(size!=NULL){ |
---|
[453] | 481 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
[59] | 482 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 483 | Py_DECREF(value); |
---|
[295] | 484 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 485 | return NULL; |
---|
[43] | 486 | } |
---|
[471] | 487 | Py_DECREF(value); |
---|
[58] | 488 | } |
---|
[59] | 489 | else{ |
---|
[453] | 490 | PyObject* value=PyString_FromString(tmp->value); |
---|
[59] | 491 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 492 | Py_DECREF(value); |
---|
[295] | 493 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 494 | return NULL; |
---|
[43] | 495 | } |
---|
[471] | 496 | Py_DECREF(value); |
---|
[59] | 497 | } |
---|
| 498 | } |
---|
| 499 | else{ |
---|
[360] | 500 | if(PyDict_GetItem(res,name)==NULL){ |
---|
[453] | 501 | PyObject* value=PyString_FromString(tmp->value); |
---|
[360] | 502 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 503 | Py_DECREF(value); |
---|
[360] | 504 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 505 | return NULL; |
---|
| 506 | } |
---|
[471] | 507 | Py_DECREF(value); |
---|
[43] | 508 | } |
---|
[59] | 509 | } |
---|
| 510 | Py_DECREF(name); |
---|
[1] | 511 | tmp=tmp->next; |
---|
| 512 | } |
---|
| 513 | return (PyDictObject*) res; |
---|
| 514 | } |
---|
| 515 | |
---|
[579] | 516 | /** |
---|
| 517 | * Convert a Python dictionary to a maps |
---|
| 518 | * |
---|
| 519 | * @param t the PyDictObject to convert |
---|
| 520 | * @return a new maps containing the converted PyDictObject |
---|
| 521 | * @warning make sure to free ressources returned by this function |
---|
| 522 | */ |
---|
[1] | 523 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 524 | maps* res=NULL; |
---|
[682] | 525 | maps* cursor=NULL; |
---|
[1] | 526 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 527 | int nb=PyList_Size(list); |
---|
| 528 | int i; |
---|
[682] | 529 | PyObject* key; |
---|
| 530 | PyObject* value; |
---|
[1] | 531 | for(i=0;i<nb;i++){ |
---|
| 532 | #ifdef DEBUG |
---|
| 533 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 534 | #endif |
---|
[682] | 535 | key=PyList_GetItem(list,i); |
---|
| 536 | value=PyDict_GetItem((PyObject*)t,key); |
---|
[1] | 537 | #ifdef DEBUG |
---|
| 538 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 539 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 540 | #endif |
---|
[9] | 541 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[682] | 542 | cursor->name=zStrdup(PyString_AsString(key)); |
---|
[295] | 543 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
[1] | 544 | #ifdef DEBUG |
---|
[295] | 545 | dumpMap(cursor->content); |
---|
[1] | 546 | #endif |
---|
| 547 | cursor->next=NULL; |
---|
| 548 | if(res==NULL) |
---|
[59] | 549 | res=dupMaps(&cursor); |
---|
[1] | 550 | else |
---|
| 551 | addMapsToMaps(&res,cursor); |
---|
[59] | 552 | freeMap(&cursor->content); |
---|
| 553 | free(cursor->content); |
---|
| 554 | free(cursor); |
---|
[1] | 555 | #ifdef DEBUG |
---|
| 556 | dumpMaps(res); |
---|
| 557 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 558 | #endif |
---|
| 559 | } |
---|
[505] | 560 | Py_DECREF(list); |
---|
[1] | 561 | return res; |
---|
| 562 | } |
---|
| 563 | |
---|
[579] | 564 | /** |
---|
[738] | 565 | * Convert a Python dictionary to a maps |
---|
| 566 | * |
---|
| 567 | * @param t the PyDictObject to convert |
---|
| 568 | * @return a new maps containing the converted PyDictObject |
---|
| 569 | * @warning make sure to free resources returned by this function |
---|
| 570 | */ |
---|
| 571 | maps* _mapsFromPyDict(PyDictObject* t){ |
---|
| 572 | |
---|
| 573 | PyObject* list = PyDict_Keys((PyObject*)t); // new ref |
---|
| 574 | int nb = PyList_Size(list); |
---|
| 575 | |
---|
| 576 | if (nb < 1) { |
---|
| 577 | Py_DECREF(list); |
---|
| 578 | return NULL; |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | maps* ptr = (maps*) malloc(MAPS_SIZE); |
---|
| 582 | maps* res = ptr; |
---|
| 583 | |
---|
| 584 | PyObject* key; |
---|
| 585 | PyObject* value; |
---|
| 586 | |
---|
| 587 | for(int i = 0; i < nb; i++) { |
---|
| 588 | |
---|
| 589 | key = PyList_GetItem(list,i); // borrowed ref |
---|
| 590 | value = PyDict_GetItem((PyObject*) t, key); // borrowed ref |
---|
| 591 | |
---|
| 592 | ptr->name = zStrdup(PyString_AsString(key)); |
---|
| 593 | ptr->content = mapFromPyDict((PyDictObject*) value); |
---|
| 594 | |
---|
| 595 | ptr->next = i < nb - 1 ? (maps*) malloc(MAPS_SIZE) : NULL; |
---|
| 596 | ptr = ptr->next; |
---|
| 597 | } |
---|
| 598 | Py_DECREF(list); |
---|
| 599 | |
---|
| 600 | return res; |
---|
| 601 | } // mapsFromPyDict |
---|
| 602 | |
---|
| 603 | /** |
---|
[579] | 604 | * Convert a Python dictionary to a map |
---|
| 605 | * |
---|
| 606 | * @param t the PyDictObject to convert |
---|
| 607 | * @return a new map containing the converted PyDictObject |
---|
| 608 | * @warning make sure to free ressources returned by this function |
---|
| 609 | */ |
---|
[1] | 610 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 611 | map* res=NULL; |
---|
| 612 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 613 | int nb=PyList_Size(list); |
---|
| 614 | int i; |
---|
[682] | 615 | PyObject* key; |
---|
| 616 | PyObject* value; |
---|
[1] | 617 | for(i=0;i<nb;i++){ |
---|
[682] | 618 | key=PyList_GetItem(list,i); |
---|
| 619 | value=PyDict_GetItem((PyObject*)t,key); |
---|
[1] | 620 | #ifdef DEBUG |
---|
| 621 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 622 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 623 | #endif |
---|
[453] | 624 | |
---|
[623] | 625 | if(strncmp(PyString_AsString(key),"value",5)==0){ |
---|
[100] | 626 | char *buffer=NULL; |
---|
[67] | 627 | Py_ssize_t size; |
---|
[392] | 628 | #if PY_MAJOR_VERSION >= 3 |
---|
[682] | 629 | if(PyBytes_Check(value)){ |
---|
| 630 | size=PyBytes_Size(value); |
---|
| 631 | buffer=PyBytes_AsString(value); |
---|
| 632 | } |
---|
| 633 | else |
---|
| 634 | if(PyUnicode_Check(value) && PyUnicode_READY(value) == 0){ |
---|
| 635 | buffer=PyUnicode_AsUTF8AndSize(value,&size); |
---|
| 636 | } |
---|
| 637 | else{ |
---|
[738] | 638 | #ifdef DEBUG |
---|
[682] | 639 | fprintf(stderr,"Unsupported return value."); |
---|
[738] | 640 | #endif |
---|
[682] | 641 | return NULL; |
---|
| 642 | } |
---|
[392] | 643 | #else |
---|
[67] | 644 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
[738] | 645 | #endif |
---|
| 646 | res = addToMapWithSize(res,PyString_AsString(key),buffer,size); |
---|
[67] | 647 | }else{ |
---|
[471] | 648 | char* lkey=PyString_AsString(key); |
---|
| 649 | char* lvalue=PyString_AsString(value); |
---|
[392] | 650 | if(res!=NULL){ |
---|
[411] | 651 | if(PyString_Size(value)>0) |
---|
[471] | 652 | addToMap(res,lkey,lvalue); |
---|
[392] | 653 | } |
---|
| 654 | else{ |
---|
[411] | 655 | if(PyString_Size(value)>0) |
---|
[471] | 656 | res=createMap(lkey,lvalue); |
---|
[392] | 657 | } |
---|
[67] | 658 | } |
---|
[1] | 659 | } |
---|
[505] | 660 | Py_DECREF(list); |
---|
[1] | 661 | return res; |
---|
| 662 | } |
---|
[368] | 663 | |
---|
[579] | 664 | /** |
---|
[738] | 665 | * Convert a Python dictionary to a map |
---|
| 666 | * |
---|
| 667 | * @param t the PyDictObject to convert |
---|
| 668 | * @return a new map containing the converted PyDictObject |
---|
| 669 | * @warning make sure to free resources returned by this function |
---|
| 670 | */ |
---|
| 671 | map* _mapFromPyDict(PyDictObject* t) { |
---|
| 672 | |
---|
| 673 | PyObject* list = PyDict_Keys((PyObject*) t); // new ref |
---|
| 674 | int nb = PyList_Size(list); |
---|
| 675 | |
---|
| 676 | if (nb < 1) { |
---|
| 677 | Py_DECREF(list); |
---|
| 678 | return NULL; |
---|
| 679 | } |
---|
| 680 | |
---|
| 681 | map* ptr = (map*) malloc(MAP_SIZE); |
---|
| 682 | map* res = ptr; |
---|
| 683 | |
---|
| 684 | PyObject* key; |
---|
| 685 | PyObject* value; |
---|
| 686 | char *buffer = NULL; |
---|
| 687 | Py_ssize_t size; |
---|
| 688 | for(int i = 0; i < nb; i++) { |
---|
| 689 | |
---|
| 690 | key = PyList_GetItem(list, i); // borrowed ref |
---|
| 691 | value = PyDict_GetItem((PyObject*) t, key); // borrowed ref |
---|
| 692 | |
---|
| 693 | ptr->name = zStrdup(PyString_AsString(key)); |
---|
| 694 | map* msize = NULL; |
---|
| 695 | |
---|
| 696 | #if PY_MAJOR_VERSION >= 3 |
---|
| 697 | if (PyBytes_Check(value)) { |
---|
| 698 | // value is byte array |
---|
| 699 | size = PyBytes_Size(value); |
---|
| 700 | buffer = PyBytes_AsString(value); // pointer to internal buffer |
---|
| 701 | char sz[32]; |
---|
| 702 | sprintf(sz, "%d", (int) size); |
---|
| 703 | msize = createMap("size", sz); |
---|
| 704 | } |
---|
| 705 | else if (PyUnicode_Check(value) && PyUnicode_READY(value) == 0) { |
---|
| 706 | // value is string object |
---|
| 707 | buffer = PyUnicode_AsUTF8AndSize(value, &size); |
---|
| 708 | size++; |
---|
| 709 | } |
---|
| 710 | else { |
---|
| 711 | printf("Type not recognized\n"); |
---|
| 712 | // error handling |
---|
| 713 | // ... |
---|
| 714 | } |
---|
| 715 | #else |
---|
| 716 | PyString_AsStringAndSize(value, &buffer, &size); |
---|
| 717 | size++; |
---|
| 718 | // to do: handle byte arrays |
---|
| 719 | #endif |
---|
| 720 | |
---|
| 721 | ptr->value = (char*) malloc(size); // check for NULL pointer |
---|
| 722 | memmove(ptr->value, buffer, size); |
---|
| 723 | |
---|
| 724 | if (msize != NULL) { |
---|
| 725 | ptr->next = msize; |
---|
| 726 | ptr = ptr->next; |
---|
| 727 | } |
---|
| 728 | |
---|
| 729 | ptr->next = i < nb - 1 ? (map*) malloc(MAP_SIZE) : NULL; |
---|
| 730 | ptr = ptr->next; |
---|
| 731 | } |
---|
| 732 | Py_DECREF(list); |
---|
| 733 | |
---|
| 734 | return res; |
---|
| 735 | } // mapFromPyDict |
---|
| 736 | |
---|
| 737 | /** |
---|
[580] | 738 | * Use the ZOO-Services messages translation function from the Python |
---|
[579] | 739 | * environment |
---|
| 740 | * |
---|
| 741 | * @param self the Python object on which we can run the method |
---|
| 742 | * @param args the Python arguments given from the Python environment |
---|
[581] | 743 | * @return a new Python string containing the translated value |
---|
[579] | 744 | * @see _ss |
---|
| 745 | */ |
---|
[368] | 746 | PyObject* |
---|
[376] | 747 | PythonTranslate(PyObject* self, PyObject* args) |
---|
| 748 | { |
---|
| 749 | char *str; |
---|
| 750 | if (!PyArg_ParseTuple(args, "s", &str)){ |
---|
| 751 | #ifdef DEBUG |
---|
| 752 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 753 | #endif |
---|
| 754 | return NULL; |
---|
| 755 | } |
---|
| 756 | return PyString_FromString(_ss(str)); |
---|
| 757 | } |
---|
| 758 | |
---|
[579] | 759 | /** |
---|
| 760 | * Update the ongoing status of a running service from the Python environment |
---|
| 761 | * |
---|
| 762 | * @param self the Python object on which we can run the method |
---|
| 763 | * @param args the Python arguments given from the Python environment |
---|
| 764 | * @return None to the Python environment |
---|
| 765 | * @see _updateStatus |
---|
| 766 | */ |
---|
[376] | 767 | PyObject* |
---|
[368] | 768 | PythonUpdateStatus(PyObject* self, PyObject* args) |
---|
| 769 | { |
---|
| 770 | maps* conf; |
---|
| 771 | PyObject* confdict; |
---|
| 772 | int istatus; |
---|
[624] | 773 | char* status=NULL; |
---|
[368] | 774 | if (!PyArg_ParseTuple(args, "O!i", &PyDict_Type, &confdict, &istatus)){ |
---|
| 775 | #ifdef DEBUG |
---|
| 776 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 777 | #endif |
---|
[624] | 778 | Py_RETURN_NONE; |
---|
[368] | 779 | } |
---|
| 780 | if (istatus < 0 || istatus > 100){ |
---|
| 781 | PyErr_SetString(ZooError, "Status must be a percentage."); |
---|
[624] | 782 | Py_RETURN_NONE; |
---|
[368] | 783 | }else{ |
---|
| 784 | char tmpStatus[4]; |
---|
| 785 | snprintf(tmpStatus, 4, "%i", istatus); |
---|
[453] | 786 | status = zStrdup(tmpStatus); |
---|
[368] | 787 | } |
---|
[624] | 788 | // create a local copy and update the lenv map |
---|
[368] | 789 | conf = mapsFromPyDict((PyDictObject*)confdict); |
---|
[624] | 790 | if(status!=NULL){ |
---|
| 791 | setMapInMaps(conf,"lenv","status",status); |
---|
| 792 | free(status); |
---|
[368] | 793 | } |
---|
[624] | 794 | else |
---|
| 795 | setMapInMaps(conf,"lenv","status","15"); |
---|
| 796 | _updateStatus(conf); |
---|
[368] | 797 | freeMaps(&conf); |
---|
| 798 | free(conf); |
---|
| 799 | Py_RETURN_NONE; |
---|
| 800 | } |
---|