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