[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; |
---|
| 525 | maps* cursor=res; |
---|
| 526 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 527 | int nb=PyList_Size(list); |
---|
| 528 | int i; |
---|
| 529 | for(i=0;i<nb;i++){ |
---|
| 530 | #ifdef DEBUG |
---|
| 531 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 532 | #endif |
---|
| 533 | PyObject* key=PyList_GetItem(list,i); |
---|
| 534 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 535 | #ifdef DEBUG |
---|
| 536 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 537 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 538 | #endif |
---|
[9] | 539 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[1] | 540 | cursor->name=PyString_AsString(key); |
---|
[295] | 541 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
[1] | 542 | #ifdef DEBUG |
---|
[295] | 543 | dumpMap(cursor->content); |
---|
[1] | 544 | #endif |
---|
| 545 | cursor->next=NULL; |
---|
| 546 | if(res==NULL) |
---|
[59] | 547 | res=dupMaps(&cursor); |
---|
[1] | 548 | else |
---|
| 549 | addMapsToMaps(&res,cursor); |
---|
[59] | 550 | freeMap(&cursor->content); |
---|
| 551 | free(cursor->content); |
---|
| 552 | free(cursor); |
---|
[1] | 553 | #ifdef DEBUG |
---|
| 554 | dumpMaps(res); |
---|
| 555 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 556 | #endif |
---|
| 557 | } |
---|
[505] | 558 | Py_DECREF(list); |
---|
[1] | 559 | return res; |
---|
| 560 | } |
---|
| 561 | |
---|
[579] | 562 | /** |
---|
| 563 | * Convert a Python dictionary to a map |
---|
| 564 | * |
---|
| 565 | * @param t the PyDictObject to convert |
---|
| 566 | * @return a new map containing the converted PyDictObject |
---|
| 567 | * @warning make sure to free ressources returned by this function |
---|
| 568 | */ |
---|
[1] | 569 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 570 | map* res=NULL; |
---|
| 571 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 572 | int nb=PyList_Size(list); |
---|
| 573 | int i; |
---|
| 574 | for(i=0;i<nb;i++){ |
---|
| 575 | PyObject* key=PyList_GetItem(list,i); |
---|
| 576 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 577 | #ifdef DEBUG |
---|
| 578 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 579 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 580 | #endif |
---|
[453] | 581 | |
---|
[623] | 582 | if(strncmp(PyString_AsString(key),"value",5)==0){ |
---|
[100] | 583 | char *buffer=NULL; |
---|
[67] | 584 | Py_ssize_t size; |
---|
[392] | 585 | #if PY_MAJOR_VERSION >= 3 |
---|
| 586 | buffer=_PyUnicode_AsStringAndSize(value,&size); |
---|
| 587 | #else |
---|
[67] | 588 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
[392] | 589 | #endif |
---|
[623] | 590 | addToMapWithSize(res,PyString_AsString(key),buffer,size); |
---|
[67] | 591 | }else{ |
---|
[471] | 592 | char* lkey=PyString_AsString(key); |
---|
| 593 | char* lvalue=PyString_AsString(value); |
---|
[392] | 594 | if(res!=NULL){ |
---|
[411] | 595 | if(PyString_Size(value)>0) |
---|
[471] | 596 | addToMap(res,lkey,lvalue); |
---|
[392] | 597 | } |
---|
| 598 | else{ |
---|
[411] | 599 | if(PyString_Size(value)>0) |
---|
[471] | 600 | res=createMap(lkey,lvalue); |
---|
[392] | 601 | } |
---|
[67] | 602 | } |
---|
[1] | 603 | } |
---|
[505] | 604 | Py_DECREF(list); |
---|
[1] | 605 | return res; |
---|
| 606 | } |
---|
[368] | 607 | |
---|
[579] | 608 | /** |
---|
[580] | 609 | * Use the ZOO-Services messages translation function from the Python |
---|
[579] | 610 | * environment |
---|
| 611 | * |
---|
| 612 | * @param self the Python object on which we can run the method |
---|
| 613 | * @param args the Python arguments given from the Python environment |
---|
[581] | 614 | * @return a new Python string containing the translated value |
---|
[579] | 615 | * @see _ss |
---|
| 616 | */ |
---|
[368] | 617 | PyObject* |
---|
[376] | 618 | PythonTranslate(PyObject* self, PyObject* args) |
---|
| 619 | { |
---|
| 620 | char *str; |
---|
| 621 | if (!PyArg_ParseTuple(args, "s", &str)){ |
---|
| 622 | #ifdef DEBUG |
---|
| 623 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 624 | #endif |
---|
| 625 | return NULL; |
---|
| 626 | } |
---|
| 627 | return PyString_FromString(_ss(str)); |
---|
| 628 | } |
---|
| 629 | |
---|
[579] | 630 | /** |
---|
| 631 | * Update the ongoing status of a running service from the Python environment |
---|
| 632 | * |
---|
| 633 | * @param self the Python object on which we can run the method |
---|
| 634 | * @param args the Python arguments given from the Python environment |
---|
| 635 | * @return None to the Python environment |
---|
| 636 | * @see _updateStatus |
---|
| 637 | */ |
---|
[376] | 638 | PyObject* |
---|
[368] | 639 | PythonUpdateStatus(PyObject* self, PyObject* args) |
---|
| 640 | { |
---|
| 641 | maps* conf; |
---|
| 642 | PyObject* confdict; |
---|
| 643 | int istatus; |
---|
[624] | 644 | char* status=NULL; |
---|
[368] | 645 | if (!PyArg_ParseTuple(args, "O!i", &PyDict_Type, &confdict, &istatus)){ |
---|
| 646 | #ifdef DEBUG |
---|
| 647 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 648 | #endif |
---|
[624] | 649 | Py_RETURN_NONE; |
---|
[368] | 650 | } |
---|
| 651 | if (istatus < 0 || istatus > 100){ |
---|
| 652 | PyErr_SetString(ZooError, "Status must be a percentage."); |
---|
[624] | 653 | Py_RETURN_NONE; |
---|
[368] | 654 | }else{ |
---|
| 655 | char tmpStatus[4]; |
---|
| 656 | snprintf(tmpStatus, 4, "%i", istatus); |
---|
[453] | 657 | status = zStrdup(tmpStatus); |
---|
[368] | 658 | } |
---|
[624] | 659 | // create a local copy and update the lenv map |
---|
[368] | 660 | conf = mapsFromPyDict((PyDictObject*)confdict); |
---|
[624] | 661 | if(status!=NULL){ |
---|
| 662 | setMapInMaps(conf,"lenv","status",status); |
---|
| 663 | free(status); |
---|
[368] | 664 | } |
---|
[624] | 665 | else |
---|
| 666 | setMapInMaps(conf,"lenv","status","15"); |
---|
| 667 | _updateStatus(conf); |
---|
[368] | 668 | freeMaps(&conf); |
---|
| 669 | free(conf); |
---|
| 670 | Py_RETURN_NONE; |
---|
| 671 | } |
---|