[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 | |
---|
[392] | 27 | struct module_state { |
---|
| 28 | PyObject *error; |
---|
| 29 | }; |
---|
| 30 | |
---|
| 31 | #if PY_MAJOR_VERSION >= 3 |
---|
| 32 | #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) |
---|
[453] | 33 | #define PyInt_FromLong PyLong_FromLong |
---|
| 34 | #define PyInt_AsLong PyLong_AsLong |
---|
| 35 | #define PyString_FromString PyUnicode_FromString |
---|
| 36 | #define PyString_FromStringAndSize PyUnicode_FromStringAndSize |
---|
| 37 | #define PyString_Check PyUnicode_Check |
---|
| 38 | #define PyString_AsString _PyUnicode_AsString |
---|
| 39 | #define PyString_Size PyUnicode_GetSize |
---|
[392] | 40 | #else |
---|
| 41 | #define GETSTATE(m) (&_state) |
---|
| 42 | static struct module_state _state; |
---|
| 43 | #endif |
---|
| 44 | |
---|
[368] | 45 | static PyObject* ZooError; |
---|
| 46 | |
---|
| 47 | PyMethodDef zooMethods[] = { |
---|
[376] | 48 | {"_", PythonTranslate, METH_VARARGS, "Translate a string using the zoo-services textdomain."}, |
---|
[368] | 49 | {"update_status", PythonUpdateStatus, METH_VARARGS, "Update status percentage of a running process."}, |
---|
| 50 | {NULL, NULL, 0, NULL} /* tempt not the blade, all fear the sentinel */ |
---|
| 51 | }; |
---|
| 52 | |
---|
[392] | 53 | #if PY_MAJOR_VERSION >= 3 |
---|
| 54 | |
---|
| 55 | static int myextension_traverse(PyObject *m, visitproc visit, void *arg) { |
---|
| 56 | Py_VISIT(GETSTATE(m)->error); |
---|
| 57 | return 0; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | static int myextension_clear(PyObject *m) { |
---|
| 61 | Py_CLEAR(GETSTATE(m)->error); |
---|
| 62 | return 0; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | static struct PyModuleDef moduledef = { |
---|
| 66 | PyModuleDef_HEAD_INIT, |
---|
| 67 | "zoo", |
---|
| 68 | NULL, |
---|
| 69 | sizeof(struct module_state), |
---|
| 70 | zooMethods, |
---|
| 71 | NULL, |
---|
| 72 | myextension_traverse, |
---|
| 73 | myextension_clear, |
---|
| 74 | NULL |
---|
| 75 | }; |
---|
| 76 | #endif |
---|
| 77 | |
---|
[368] | 78 | PyMODINIT_FUNC init_zoo(){ |
---|
| 79 | PyObject *tmp,*d; |
---|
[392] | 80 | PyObject *module = |
---|
| 81 | #if PY_MAJOR_VERSION >= 3 |
---|
| 82 | PyModule_Create(&moduledef); |
---|
| 83 | #else |
---|
| 84 | Py_InitModule("zoo", zooMethods); |
---|
| 85 | #endif |
---|
| 86 | if (module == NULL){ |
---|
| 87 | #if PY_MAJOR_VERSION >= 3 |
---|
| 88 | return NULL; |
---|
| 89 | #else |
---|
[368] | 90 | return; |
---|
[392] | 91 | #endif |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | struct module_state *st = GETSTATE(module); |
---|
| 95 | |
---|
[368] | 96 | d = PyModule_GetDict(module); |
---|
| 97 | tmp = PyInt_FromLong(3); |
---|
| 98 | PyDict_SetItemString(d, "SERVICE_SUCCEEDED", tmp); |
---|
| 99 | Py_DECREF(tmp); |
---|
| 100 | |
---|
| 101 | tmp = PyInt_FromLong(4); |
---|
| 102 | PyDict_SetItemString(d, "SERVICE_FAILED", tmp); |
---|
| 103 | Py_DECREF(tmp); |
---|
| 104 | |
---|
| 105 | ZooError = PyErr_NewException("zoo.error", NULL, NULL); |
---|
| 106 | Py_INCREF(ZooError); |
---|
| 107 | PyModule_AddObject(module, "error", ZooError); |
---|
[392] | 108 | #if PY_MAJOR_VERSION >= 3 |
---|
| 109 | return module; |
---|
| 110 | #endif |
---|
[368] | 111 | } |
---|
| 112 | |
---|
[1] | 113 | int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
[451] | 114 | char *pythonpath; |
---|
| 115 | char *python_path; |
---|
[1] | 116 | maps* m=*main_conf; |
---|
| 117 | maps* inputs=*real_inputs; |
---|
| 118 | maps* outputs=*real_outputs; |
---|
[392] | 119 | map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd"); |
---|
| 120 | char *ntmp=tmp0->value; |
---|
[1] | 121 | map* tmp=NULL; |
---|
[451] | 122 | int hasToClean=0; |
---|
[1] | 123 | tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH"); |
---|
[63] | 124 | #ifdef DEBUG |
---|
[9] | 125 | fprintf(stderr,"PYTHON SUPPORT \n"); |
---|
[63] | 126 | #endif |
---|
[1] | 127 | if(tmp!=NULL){ |
---|
[63] | 128 | #ifdef DEBUG |
---|
[9] | 129 | fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value)); |
---|
[63] | 130 | #endif |
---|
[9] | 131 | python_path=(char*)malloc((strlen(tmp->value))*sizeof(char)); |
---|
| 132 | sprintf(python_path,"%s",tmp->value); |
---|
[451] | 133 | hasToClean=1; |
---|
[1] | 134 | } |
---|
| 135 | else{ |
---|
[451] | 136 | python_path="."; |
---|
[1] | 137 | } |
---|
| 138 | tmp=NULL; |
---|
| 139 | tmp=getMap(request,"metapath"); |
---|
[392] | 140 | if(tmp!=NULL && strcmp(tmp->value,"")!=0){ |
---|
| 141 | pythonpath=(char*)malloc((4+strlen(python_path)+strlen(ntmp)+strlen(tmp->value))*sizeof(char)); |
---|
[1] | 142 | #ifdef WIN32 |
---|
[451] | 143 | sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); |
---|
[1] | 144 | #else |
---|
[451] | 145 | sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); |
---|
[1] | 146 | #endif |
---|
[392] | 147 | } |
---|
| 148 | else{ |
---|
| 149 | pythonpath=(char*)malloc((2+strlen(python_path)+strlen(ntmp))*sizeof(char)); |
---|
[1] | 150 | #ifdef WIN32 |
---|
| 151 | sprintf(pythonpath,"%s;%s",ntmp,python_path); |
---|
| 152 | #else |
---|
[392] | 153 | sprintf(pythonpath,"%s:%s",ntmp,python_path); |
---|
[1] | 154 | #endif |
---|
[392] | 155 | } |
---|
[67] | 156 | #ifdef DEBUG |
---|
[9] | 157 | fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); |
---|
[67] | 158 | #endif |
---|
[1] | 159 | #ifndef WIN32 |
---|
| 160 | setenv("PYTHONPATH",pythonpath,1); |
---|
| 161 | #else |
---|
| 162 | SetEnvironmentVariable("PYTHONPATH",pythonpath); |
---|
[364] | 163 | char* toto=(char*)malloc((strlen(pythonpath)+12)*sizeof(char)); |
---|
| 164 | sprintf(toto,"PYTHONPATH=%s",pythonpath); |
---|
| 165 | putenv(toto); |
---|
[451] | 166 | free(toto); |
---|
[1] | 167 | #endif |
---|
[451] | 168 | if(hasToClean>0) |
---|
| 169 | free(python_path); |
---|
[1] | 170 | free(pythonpath); |
---|
[9] | 171 | |
---|
[295] | 172 | PyThreadState *mainstate; |
---|
[392] | 173 | #if PY_MAJOR_VERSION >= 3 |
---|
| 174 | PyImport_AppendInittab("zoo", init_zoo); |
---|
| 175 | #else |
---|
[295] | 176 | PyEval_InitThreads(); |
---|
[392] | 177 | #endif |
---|
[1] | 178 | Py_Initialize(); |
---|
[392] | 179 | #if PY_MAJOR_VERSION >= 3 |
---|
| 180 | PyEval_InitThreads(); |
---|
| 181 | PyImport_ImportModule("zoo"); |
---|
| 182 | #else |
---|
[368] | 183 | init_zoo(); |
---|
[392] | 184 | #endif |
---|
[295] | 185 | mainstate = PyThreadState_Swap(NULL); |
---|
| 186 | PyEval_ReleaseLock(); |
---|
| 187 | PyGILState_STATE gstate; |
---|
| 188 | gstate = PyGILState_Ensure(); |
---|
[1] | 189 | PyObject *pName, *pModule, *pFunc; |
---|
| 190 | tmp=getMap(s->content,"serviceProvider"); |
---|
[9] | 191 | if(tmp!=NULL) |
---|
[453] | 192 | pName = PyString_FromString(tmp->value); |
---|
[9] | 193 | else{ |
---|
| 194 | map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file."); |
---|
| 195 | addToMap(err,"code","NoApplicableCode"); |
---|
| 196 | printExceptionReportResponse(m,err); |
---|
| 197 | exit(-1); |
---|
| 198 | } |
---|
[1] | 199 | pModule = PyImport_Import(pName); |
---|
| 200 | int res=SERVICE_FAILED; |
---|
| 201 | if (pModule != NULL) { |
---|
| 202 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
| 203 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
[114] | 204 | PyObject *pValue; |
---|
[1] | 205 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
| 206 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
| 207 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
| 208 | PyObject *pArgs=PyTuple_New(3); |
---|
[114] | 209 | if (!pArgs) |
---|
| 210 | return -1; |
---|
[1] | 211 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
| 212 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
| 213 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
| 214 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 215 | if (pValue != NULL) { |
---|
| 216 | res=PyInt_AsLong(pValue); |
---|
[9] | 217 | freeMaps(real_outputs); |
---|
| 218 | free(*real_outputs); |
---|
[59] | 219 | freeMaps(main_conf); |
---|
| 220 | free(*main_conf); |
---|
[57] | 221 | *main_conf=mapsFromPyDict(arg1); |
---|
[9] | 222 | *real_outputs=mapsFromPyDict(arg3); |
---|
[1] | 223 | #ifdef DEBUG |
---|
| 224 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
| 225 | dumpMaps(inputs); |
---|
[364] | 226 | dumpMaps(*real_outputs); |
---|
[1] | 227 | #endif |
---|
[9] | 228 | }else{ |
---|
[1] | 229 | PyObject *ptype,*pvalue, *ptraceback; |
---|
| 230 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
| 231 | PyObject *trace=PyObject_Str(pvalue); |
---|
| 232 | char pbt[10240]; |
---|
| 233 | if(PyString_Check(trace)) |
---|
| 234 | sprintf(pbt,"TRACE : %s",PyString_AsString(trace)); |
---|
| 235 | else |
---|
| 236 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 237 | trace=NULL; |
---|
| 238 | trace=PyObject_Str(ptype); |
---|
[59] | 239 | if(PyString_Check(trace)){ |
---|
[453] | 240 | char *tpbt=zStrdup(pbt); |
---|
[337] | 241 | sprintf(pbt,"%s\n%s\0",tpbt,PyString_AsString(trace)); |
---|
[59] | 242 | free(tpbt); |
---|
| 243 | } |
---|
[1] | 244 | else |
---|
| 245 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
[332] | 246 | |
---|
[453] | 247 | char *tpbt=zStrdup(pbt); |
---|
[1] | 248 | pName = PyString_FromString("traceback"); |
---|
| 249 | pModule = PyImport_Import(pName); |
---|
| 250 | pArgs = PyTuple_New(1); |
---|
| 251 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
| 252 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
| 253 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 254 | trace=NULL; |
---|
| 255 | trace=PyObject_Str(pValue); |
---|
| 256 | if(PyString_Check(trace)) |
---|
[332] | 257 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,PyString_AsString(trace)); |
---|
[1] | 258 | else |
---|
[332] | 259 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations. %s",tpbt); |
---|
| 260 | free(tpbt); |
---|
[1] | 261 | map* err=createMap("text",pbt); |
---|
| 262 | addToMap(err,"code","NoApplicableCode"); |
---|
| 263 | printExceptionReportResponse(m,err); |
---|
[295] | 264 | res=-1; |
---|
[1] | 265 | } |
---|
| 266 | } |
---|
| 267 | else{ |
---|
| 268 | char tmpS[1024]; |
---|
[295] | 269 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
[1] | 270 | map* tmps=createMap("text",tmpS); |
---|
| 271 | printExceptionReportResponse(m,tmps); |
---|
[295] | 272 | res=-1; |
---|
[1] | 273 | } |
---|
| 274 | } else{ |
---|
| 275 | char tmpS[1024]; |
---|
[9] | 276 | sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); |
---|
[1] | 277 | map* tmps=createMap("text",tmpS); |
---|
| 278 | printExceptionReportResponse(m,tmps); |
---|
| 279 | if (PyErr_Occurred()) |
---|
| 280 | PyErr_Print(); |
---|
[295] | 281 | PyErr_Clear(); |
---|
| 282 | res=-1; |
---|
| 283 | //exit(-1); |
---|
[1] | 284 | } |
---|
[392] | 285 | #if PY_MAJOR_VERSION < 3 |
---|
[295] | 286 | PyGILState_Release(gstate); |
---|
| 287 | PyEval_AcquireLock(); |
---|
[392] | 288 | #endif |
---|
[295] | 289 | PyThreadState_Swap(mainstate); |
---|
[1] | 290 | Py_Finalize(); |
---|
| 291 | return res; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
| 295 | PyObject* res=PyDict_New( ); |
---|
| 296 | maps* tmp=t; |
---|
| 297 | while(tmp!=NULL){ |
---|
[295] | 298 | PyObject* value=(PyObject*)PyDict_FromMap(tmp->content); |
---|
[453] | 299 | PyObject* name=PyString_FromString(tmp->name); |
---|
[295] | 300 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 301 | fprintf(stderr,"Unable to set map value ..."); |
---|
| 302 | return NULL; |
---|
[1] | 303 | } |
---|
[295] | 304 | Py_DECREF(name); |
---|
[1] | 305 | tmp=tmp->next; |
---|
| 306 | } |
---|
| 307 | return (PyDictObject*) res; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | PyDictObject* PyDict_FromMap(map* t){ |
---|
| 311 | PyObject* res=PyDict_New( ); |
---|
| 312 | map* tmp=t; |
---|
[360] | 313 | int hasSize=0; |
---|
| 314 | map* isArray=getMap(tmp,"isArray"); |
---|
[58] | 315 | map* size=getMap(tmp,"size"); |
---|
[360] | 316 | map* tmap=getMapType(tmp); |
---|
[1] | 317 | while(tmp!=NULL){ |
---|
[453] | 318 | PyObject* name=PyString_FromString(tmp->name); |
---|
[360] | 319 | if(strcasecmp(tmp->name,"value")==0) { |
---|
| 320 | if(isArray!=NULL){ |
---|
| 321 | map* len=getMap(tmp,"length"); |
---|
| 322 | int cnt=atoi(len->value); |
---|
| 323 | PyObject* value=PyList_New(cnt); |
---|
| 324 | PyObject* mvalue=PyList_New(cnt); |
---|
| 325 | PyObject* svalue=PyList_New(cnt); |
---|
| 326 | |
---|
| 327 | for(int i=0;i<cnt;i++){ |
---|
| 328 | |
---|
| 329 | map* vMap=getMapArray(tmp,"value",i); |
---|
| 330 | map* sMap=getMapArray(tmp,"size",i); |
---|
| 331 | |
---|
| 332 | if(vMap!=NULL){ |
---|
| 333 | |
---|
| 334 | PyObject* lvalue; |
---|
| 335 | PyObject* lsvalue; |
---|
| 336 | if(sMap==NULL){ |
---|
| 337 | lvalue=PyString_FromString(vMap->value); |
---|
| 338 | lsvalue=Py_None; |
---|
| 339 | } |
---|
[453] | 340 | else{ |
---|
[360] | 341 | lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value)); |
---|
| 342 | lsvalue=PyString_FromString(sMap->value); |
---|
| 343 | hasSize=1; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | if(PyList_SetItem(value,i,lvalue)<0){ |
---|
| 347 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 348 | return NULL; |
---|
| 349 | } |
---|
| 350 | if(PyList_SetItem(svalue,i,lsvalue)<0){ |
---|
| 351 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 352 | return NULL; |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | map* mMap=getMapArray(tmp,tmap->name,i); |
---|
| 357 | PyObject* lmvalue; |
---|
| 358 | if(mMap!=NULL){ |
---|
| 359 | lmvalue=PyString_FromString(mMap->value); |
---|
| 360 | }else |
---|
| 361 | lmvalue=Py_None; |
---|
| 362 | |
---|
| 363 | if(PyList_SetItem(mvalue,i,lmvalue)<0){ |
---|
| 364 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 365 | return NULL; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 371 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 372 | return NULL; |
---|
| 373 | } |
---|
| 374 | if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){ |
---|
| 375 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 376 | return NULL; |
---|
| 377 | } |
---|
| 378 | if(hasSize>0) |
---|
| 379 | if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){ |
---|
| 380 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 381 | return NULL; |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | else if(size!=NULL){ |
---|
[453] | 385 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
[59] | 386 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[295] | 387 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 388 | return NULL; |
---|
[43] | 389 | } |
---|
[58] | 390 | } |
---|
[59] | 391 | else{ |
---|
[453] | 392 | PyObject* value=PyString_FromString(tmp->value); |
---|
[59] | 393 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[295] | 394 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 395 | return NULL; |
---|
[43] | 396 | } |
---|
[59] | 397 | } |
---|
| 398 | } |
---|
| 399 | else{ |
---|
[360] | 400 | if(PyDict_GetItem(res,name)==NULL){ |
---|
[453] | 401 | PyObject* value=PyString_FromString(tmp->value); |
---|
[360] | 402 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 403 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 404 | return NULL; |
---|
| 405 | } |
---|
[43] | 406 | } |
---|
[59] | 407 | } |
---|
| 408 | Py_DECREF(name); |
---|
[1] | 409 | tmp=tmp->next; |
---|
| 410 | } |
---|
| 411 | return (PyDictObject*) res; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 415 | maps* res=NULL; |
---|
| 416 | maps* cursor=res; |
---|
| 417 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 418 | int nb=PyList_Size(list); |
---|
| 419 | int i; |
---|
| 420 | for(i=0;i<nb;i++){ |
---|
| 421 | #ifdef DEBUG |
---|
| 422 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 423 | #endif |
---|
| 424 | PyObject* key=PyList_GetItem(list,i); |
---|
| 425 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 426 | #ifdef DEBUG |
---|
| 427 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 428 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 429 | #endif |
---|
[9] | 430 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[1] | 431 | cursor->name=PyString_AsString(key); |
---|
[295] | 432 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
[1] | 433 | #ifdef DEBUG |
---|
[295] | 434 | dumpMap(cursor->content); |
---|
[1] | 435 | #endif |
---|
| 436 | cursor->next=NULL; |
---|
| 437 | if(res==NULL) |
---|
[59] | 438 | res=dupMaps(&cursor); |
---|
[1] | 439 | else |
---|
| 440 | addMapsToMaps(&res,cursor); |
---|
[59] | 441 | freeMap(&cursor->content); |
---|
| 442 | free(cursor->content); |
---|
| 443 | free(cursor); |
---|
[1] | 444 | #ifdef DEBUG |
---|
| 445 | dumpMaps(res); |
---|
| 446 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 447 | #endif |
---|
| 448 | } |
---|
| 449 | return res; |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 453 | map* res=NULL; |
---|
| 454 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 455 | int nb=PyList_Size(list); |
---|
| 456 | int i; |
---|
| 457 | for(i=0;i<nb;i++){ |
---|
| 458 | PyObject* key=PyList_GetItem(list,i); |
---|
| 459 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 460 | #ifdef DEBUG |
---|
| 461 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 462 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 463 | #endif |
---|
[453] | 464 | |
---|
[100] | 465 | if(strcmp(PyString_AsString(key),"value")==0){ |
---|
| 466 | char *buffer=NULL; |
---|
[67] | 467 | Py_ssize_t size; |
---|
[392] | 468 | #if PY_MAJOR_VERSION >= 3 |
---|
| 469 | buffer=_PyUnicode_AsStringAndSize(value,&size); |
---|
| 470 | #else |
---|
[67] | 471 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
[392] | 472 | #endif |
---|
[67] | 473 | if(res!=NULL){ |
---|
| 474 | addToMap(res,PyString_AsString(key),""); |
---|
| 475 | }else{ |
---|
| 476 | res=createMap(PyString_AsString(key),""); |
---|
| 477 | } |
---|
| 478 | map* tmpR=getMap(res,"value"); |
---|
| 479 | free(tmpR->value); |
---|
[100] | 480 | tmpR->value=(char*)malloc((size+1)*sizeof(char)); |
---|
| 481 | memmove(tmpR->value,buffer,size*sizeof(char)); |
---|
| 482 | tmpR->value[size]=0; |
---|
| 483 | char sin[1024]; |
---|
| 484 | sprintf(sin,"%d",size); |
---|
| 485 | addToMap(res,"size",sin); |
---|
[67] | 486 | }else{ |
---|
[392] | 487 | if(res!=NULL){ |
---|
[411] | 488 | if(PyString_Size(value)>0) |
---|
| 489 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
[392] | 490 | } |
---|
| 491 | else{ |
---|
[411] | 492 | if(PyString_Size(value)>0) |
---|
[453] | 493 | res=createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
[392] | 494 | } |
---|
[67] | 495 | } |
---|
[1] | 496 | } |
---|
| 497 | return res; |
---|
| 498 | } |
---|
[368] | 499 | |
---|
| 500 | PyObject* |
---|
[376] | 501 | PythonTranslate(PyObject* self, PyObject* args) |
---|
| 502 | { |
---|
| 503 | char *str; |
---|
| 504 | if (!PyArg_ParseTuple(args, "s", &str)){ |
---|
| 505 | #ifdef DEBUG |
---|
| 506 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 507 | #endif |
---|
| 508 | return NULL; |
---|
| 509 | } |
---|
| 510 | return PyString_FromString(_ss(str)); |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | PyObject* |
---|
[368] | 514 | PythonUpdateStatus(PyObject* self, PyObject* args) |
---|
| 515 | { |
---|
| 516 | maps* conf; |
---|
| 517 | PyObject* confdict; |
---|
| 518 | int istatus; |
---|
| 519 | char* status; |
---|
| 520 | if (!PyArg_ParseTuple(args, "O!i", &PyDict_Type, &confdict, &istatus)){ |
---|
| 521 | #ifdef DEBUG |
---|
| 522 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 523 | #endif |
---|
| 524 | return NULL; |
---|
| 525 | } |
---|
| 526 | if (istatus < 0 || istatus > 100){ |
---|
| 527 | PyErr_SetString(ZooError, "Status must be a percentage."); |
---|
| 528 | return NULL; |
---|
| 529 | }else{ |
---|
| 530 | char tmpStatus[4]; |
---|
| 531 | snprintf(tmpStatus, 4, "%i", istatus); |
---|
[453] | 532 | status = zStrdup(tmpStatus); |
---|
[368] | 533 | } |
---|
| 534 | /* now update the map */ |
---|
| 535 | { |
---|
| 536 | PyObject* lenv = PyMapping_GetItemString(confdict, "lenv"); |
---|
| 537 | if (lenv && PyMapping_Check(lenv)){ |
---|
[453] | 538 | PyObject* valobj = PyString_FromString(status); |
---|
[368] | 539 | PyMapping_SetItemString(lenv, "status", valobj); |
---|
| 540 | Py_DECREF(valobj); |
---|
| 541 | } |
---|
| 542 | Py_DECREF(lenv); |
---|
| 543 | } |
---|
| 544 | conf = mapsFromPyDict((PyDictObject*)confdict); |
---|
| 545 | if (getMapFromMaps(conf,"lenv","status") != NULL){ |
---|
| 546 | fprintf(stderr,"STATUS RETURNED : %s\n",status); |
---|
| 547 | if(status!=NULL){ |
---|
| 548 | setMapInMaps(conf,"lenv","status",status); |
---|
| 549 | free(status); |
---|
| 550 | } |
---|
| 551 | else |
---|
| 552 | setMapInMaps(conf,"lenv","status","15"); |
---|
| 553 | updateStatus(conf); |
---|
| 554 | } |
---|
| 555 | freeMaps(&conf); |
---|
| 556 | free(conf); |
---|
| 557 | Py_RETURN_NONE; |
---|
| 558 | } |
---|