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