[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 | |
---|
[490] | 109 | ZooError = PyErr_NewException((char*)"zoo.error", NULL, NULL); |
---|
[368] | 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{ |
---|
[490] | 140 | python_path=(char*)"."; |
---|
[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"); |
---|
[505] | 195 | map* mp=getMap(request,"metapath"); |
---|
| 196 | if(tmp!=NULL){ |
---|
| 197 | if(mp!=NULL && strlen(mp->value)>0){ |
---|
| 198 | char *mps=zStrdup(mp->value); |
---|
| 199 | int i,len=strlen(mps); |
---|
| 200 | int j=0; |
---|
| 201 | for(i=0;i<len;i++){ |
---|
| 202 | if(mps[i]=='/'){ |
---|
| 203 | mps[i]='.'; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | char *mn=(char*)malloc((strlen(mps)+strlen(tmp->value)+2)*sizeof(char)); |
---|
| 207 | sprintf(mn,"%s.%s",mps,tmp->value); |
---|
| 208 | pName = PyString_FromString(mn); |
---|
| 209 | free(mn); |
---|
| 210 | free(mps); |
---|
| 211 | } |
---|
| 212 | else{ |
---|
| 213 | pName = PyString_FromString(tmp->value); |
---|
| 214 | } |
---|
| 215 | } |
---|
[9] | 216 | else{ |
---|
[576] | 217 | errorException (m, "Unable to parse serviceProvider please check your zcfg file.", "NoApplicableCode", NULL); |
---|
[9] | 218 | exit(-1); |
---|
| 219 | } |
---|
[1] | 220 | pModule = PyImport_Import(pName); |
---|
| 221 | int res=SERVICE_FAILED; |
---|
| 222 | if (pModule != NULL) { |
---|
| 223 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
| 224 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
[114] | 225 | PyObject *pValue; |
---|
[1] | 226 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
| 227 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
| 228 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
| 229 | PyObject *pArgs=PyTuple_New(3); |
---|
[114] | 230 | if (!pArgs) |
---|
| 231 | return -1; |
---|
[1] | 232 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
| 233 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
| 234 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
| 235 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 236 | if (pValue != NULL) { |
---|
| 237 | res=PyInt_AsLong(pValue); |
---|
[9] | 238 | freeMaps(real_outputs); |
---|
| 239 | free(*real_outputs); |
---|
[59] | 240 | freeMaps(main_conf); |
---|
| 241 | free(*main_conf); |
---|
[57] | 242 | *main_conf=mapsFromPyDict(arg1); |
---|
[9] | 243 | *real_outputs=mapsFromPyDict(arg3); |
---|
[1] | 244 | #ifdef DEBUG |
---|
| 245 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
| 246 | dumpMaps(inputs); |
---|
[364] | 247 | dumpMaps(*real_outputs); |
---|
[1] | 248 | #endif |
---|
[9] | 249 | }else{ |
---|
[576] | 250 | PythonZooReport(m,tmp->value,0); |
---|
[295] | 251 | res=-1; |
---|
[1] | 252 | } |
---|
| 253 | } |
---|
| 254 | else{ |
---|
| 255 | char tmpS[1024]; |
---|
[295] | 256 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
[576] | 257 | errorException(m,tmpS,"NoApplicableCode",NULL); |
---|
[295] | 258 | res=-1; |
---|
[1] | 259 | } |
---|
| 260 | } else{ |
---|
[576] | 261 | PythonZooReport(m,tmp->value,1); |
---|
[295] | 262 | res=-1; |
---|
[1] | 263 | } |
---|
[392] | 264 | #if PY_MAJOR_VERSION < 3 |
---|
[295] | 265 | PyGILState_Release(gstate); |
---|
[478] | 266 | PyEval_AcquireLock(); |
---|
[392] | 267 | #endif |
---|
[295] | 268 | PyThreadState_Swap(mainstate); |
---|
[1] | 269 | Py_Finalize(); |
---|
| 270 | return res; |
---|
| 271 | } |
---|
| 272 | |
---|
[576] | 273 | void PythonZooReport(maps* m,const char* module,int load){ |
---|
| 274 | PyObject *pName, *pModule, *pFunc; |
---|
| 275 | PyObject *ptype, *pvalue, *ptraceback,*pValue,*pArgs; |
---|
| 276 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
| 277 | char *pStrErrorMessage = PyString_AsString(pvalue); |
---|
| 278 | char *tmp0=_("Python module %s cannot be loaded. Message: %s\n"); |
---|
| 279 | |
---|
| 280 | PyObject *trace=PyObject_Str(pvalue); |
---|
| 281 | char *pbt=NULL; |
---|
| 282 | if(PyString_Check(trace)){ |
---|
| 283 | pbt=(char*)malloc((7+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 284 | sprintf(pbt,"TRACE: %s",PyString_AsString(trace)); |
---|
| 285 | } |
---|
| 286 | else |
---|
| 287 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 288 | |
---|
| 289 | trace=NULL; |
---|
| 290 | |
---|
| 291 | trace=PyObject_Str(ptype); |
---|
| 292 | if(PyString_Check(trace)){ |
---|
| 293 | char *tpbt=zStrdup(pbt); |
---|
| 294 | if(pbt!=NULL) |
---|
| 295 | free(pbt); |
---|
| 296 | pbt=(char*)malloc((1+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 297 | sprintf(pbt,"%s\n%s",tpbt,PyString_AsString(trace)); |
---|
| 298 | free(tpbt); |
---|
| 299 | } |
---|
| 300 | else |
---|
| 301 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 302 | |
---|
| 303 | if(ptraceback!=NULL){ |
---|
| 304 | char *tpbt=zStrdup(pbt); |
---|
| 305 | pName = PyString_FromString("traceback"); |
---|
| 306 | pModule = PyImport_Import(pName); |
---|
| 307 | pArgs = PyTuple_New(1); |
---|
| 308 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
| 309 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
| 310 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 311 | trace=NULL; |
---|
| 312 | trace=PyObject_Str(pValue); |
---|
| 313 | if(PyString_Check(trace)){ |
---|
| 314 | if(pbt!=NULL) |
---|
| 315 | free(pbt); |
---|
| 316 | pbt=(char*)malloc((90+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 317 | sprintf(pbt,_("%s\nUnable to run your python process properly. Please check the following messages : %s"),tpbt,PyString_AsString(trace)); |
---|
| 318 | } |
---|
| 319 | else{ |
---|
| 320 | if(pbt!=NULL) |
---|
| 321 | free(pbt); |
---|
| 322 | pbt=(char*)malloc((90+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 323 | sprintf(pbt,_("%s \n Unable to run your python process properly. Unable to provide any futher informations."),tpbt); |
---|
| 324 | } |
---|
| 325 | free(tpbt); |
---|
| 326 | } |
---|
| 327 | if(load>0){ |
---|
| 328 | char *tmpS=(char*)malloc((strlen(tmp0)+strlen(module)+strlen(pbt)+1)*sizeof(char)); |
---|
| 329 | sprintf(tmpS,tmp0,module,pbt); |
---|
| 330 | errorException(m,tmpS,"NoApplicableCode",NULL); |
---|
| 331 | free(tmpS); |
---|
| 332 | }else |
---|
| 333 | errorException(m,pbt,"NoApplicableCode",NULL); |
---|
| 334 | free(pbt); |
---|
| 335 | } |
---|
| 336 | |
---|
[1] | 337 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
| 338 | PyObject* res=PyDict_New( ); |
---|
| 339 | maps* tmp=t; |
---|
| 340 | while(tmp!=NULL){ |
---|
[295] | 341 | PyObject* value=(PyObject*)PyDict_FromMap(tmp->content); |
---|
[453] | 342 | PyObject* name=PyString_FromString(tmp->name); |
---|
[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){ |
---|
[453] | 361 | PyObject* name=PyString_FromString(tmp->name); |
---|
[360] | 362 | if(strcasecmp(tmp->name,"value")==0) { |
---|
| 363 | if(isArray!=NULL){ |
---|
| 364 | map* len=getMap(tmp,"length"); |
---|
| 365 | int cnt=atoi(len->value); |
---|
| 366 | PyObject* value=PyList_New(cnt); |
---|
| 367 | PyObject* mvalue=PyList_New(cnt); |
---|
| 368 | PyObject* svalue=PyList_New(cnt); |
---|
| 369 | |
---|
| 370 | for(int i=0;i<cnt;i++){ |
---|
| 371 | |
---|
| 372 | map* vMap=getMapArray(tmp,"value",i); |
---|
| 373 | map* sMap=getMapArray(tmp,"size",i); |
---|
| 374 | |
---|
| 375 | if(vMap!=NULL){ |
---|
| 376 | |
---|
| 377 | PyObject* lvalue; |
---|
| 378 | PyObject* lsvalue; |
---|
| 379 | if(sMap==NULL){ |
---|
| 380 | lvalue=PyString_FromString(vMap->value); |
---|
| 381 | lsvalue=Py_None; |
---|
| 382 | } |
---|
[453] | 383 | else{ |
---|
[360] | 384 | lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value)); |
---|
| 385 | lsvalue=PyString_FromString(sMap->value); |
---|
| 386 | hasSize=1; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | if(PyList_SetItem(value,i,lvalue)<0){ |
---|
| 390 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 391 | return NULL; |
---|
| 392 | } |
---|
| 393 | if(PyList_SetItem(svalue,i,lsvalue)<0){ |
---|
| 394 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 395 | return NULL; |
---|
| 396 | } |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | map* mMap=getMapArray(tmp,tmap->name,i); |
---|
| 400 | PyObject* lmvalue; |
---|
| 401 | if(mMap!=NULL){ |
---|
| 402 | lmvalue=PyString_FromString(mMap->value); |
---|
| 403 | }else |
---|
| 404 | lmvalue=Py_None; |
---|
| 405 | |
---|
| 406 | if(PyList_SetItem(mvalue,i,lmvalue)<0){ |
---|
| 407 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 408 | return NULL; |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 414 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 415 | return NULL; |
---|
| 416 | } |
---|
| 417 | if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){ |
---|
| 418 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 419 | return NULL; |
---|
| 420 | } |
---|
| 421 | if(hasSize>0) |
---|
| 422 | if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){ |
---|
| 423 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 424 | return NULL; |
---|
| 425 | } |
---|
| 426 | } |
---|
| 427 | else if(size!=NULL){ |
---|
[453] | 428 | PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
[59] | 429 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 430 | Py_DECREF(value); |
---|
[295] | 431 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 432 | return NULL; |
---|
[43] | 433 | } |
---|
[471] | 434 | Py_DECREF(value); |
---|
[58] | 435 | } |
---|
[59] | 436 | else{ |
---|
[453] | 437 | PyObject* value=PyString_FromString(tmp->value); |
---|
[59] | 438 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 439 | Py_DECREF(value); |
---|
[295] | 440 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 441 | return NULL; |
---|
[43] | 442 | } |
---|
[471] | 443 | Py_DECREF(value); |
---|
[59] | 444 | } |
---|
| 445 | } |
---|
| 446 | else{ |
---|
[360] | 447 | if(PyDict_GetItem(res,name)==NULL){ |
---|
[453] | 448 | PyObject* value=PyString_FromString(tmp->value); |
---|
[360] | 449 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 450 | Py_DECREF(value); |
---|
[360] | 451 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 452 | return NULL; |
---|
| 453 | } |
---|
[471] | 454 | Py_DECREF(value); |
---|
[43] | 455 | } |
---|
[59] | 456 | } |
---|
| 457 | Py_DECREF(name); |
---|
[1] | 458 | tmp=tmp->next; |
---|
| 459 | } |
---|
| 460 | return (PyDictObject*) res; |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 464 | maps* res=NULL; |
---|
| 465 | maps* cursor=res; |
---|
| 466 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 467 | int nb=PyList_Size(list); |
---|
| 468 | int i; |
---|
| 469 | for(i=0;i<nb;i++){ |
---|
| 470 | #ifdef DEBUG |
---|
| 471 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 472 | #endif |
---|
| 473 | PyObject* key=PyList_GetItem(list,i); |
---|
| 474 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 475 | #ifdef DEBUG |
---|
| 476 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 477 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 478 | #endif |
---|
[9] | 479 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
[1] | 480 | cursor->name=PyString_AsString(key); |
---|
[295] | 481 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
[1] | 482 | #ifdef DEBUG |
---|
[295] | 483 | dumpMap(cursor->content); |
---|
[1] | 484 | #endif |
---|
| 485 | cursor->next=NULL; |
---|
| 486 | if(res==NULL) |
---|
[59] | 487 | res=dupMaps(&cursor); |
---|
[1] | 488 | else |
---|
| 489 | addMapsToMaps(&res,cursor); |
---|
[59] | 490 | freeMap(&cursor->content); |
---|
| 491 | free(cursor->content); |
---|
| 492 | free(cursor); |
---|
[471] | 493 | Py_DECREF(key); |
---|
[1] | 494 | #ifdef DEBUG |
---|
| 495 | dumpMaps(res); |
---|
| 496 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 497 | #endif |
---|
| 498 | } |
---|
[505] | 499 | Py_DECREF(list); |
---|
[1] | 500 | return res; |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 504 | map* res=NULL; |
---|
| 505 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 506 | int nb=PyList_Size(list); |
---|
| 507 | int i; |
---|
| 508 | for(i=0;i<nb;i++){ |
---|
| 509 | PyObject* key=PyList_GetItem(list,i); |
---|
| 510 | PyObject* value=PyDict_GetItem((PyObject*)t,key); |
---|
| 511 | #ifdef DEBUG |
---|
| 512 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 513 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 514 | #endif |
---|
[453] | 515 | |
---|
[100] | 516 | if(strcmp(PyString_AsString(key),"value")==0){ |
---|
| 517 | char *buffer=NULL; |
---|
[67] | 518 | Py_ssize_t size; |
---|
[392] | 519 | #if PY_MAJOR_VERSION >= 3 |
---|
| 520 | buffer=_PyUnicode_AsStringAndSize(value,&size); |
---|
| 521 | #else |
---|
[67] | 522 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
[392] | 523 | #endif |
---|
[508] | 524 | addToMapWithSize(res,"value",buffer,size); |
---|
[67] | 525 | }else{ |
---|
[471] | 526 | char* lkey=PyString_AsString(key); |
---|
| 527 | char* lvalue=PyString_AsString(value); |
---|
[392] | 528 | if(res!=NULL){ |
---|
[411] | 529 | if(PyString_Size(value)>0) |
---|
[471] | 530 | addToMap(res,lkey,lvalue); |
---|
[392] | 531 | } |
---|
| 532 | else{ |
---|
[411] | 533 | if(PyString_Size(value)>0) |
---|
[471] | 534 | res=createMap(lkey,lvalue); |
---|
[392] | 535 | } |
---|
[67] | 536 | } |
---|
[1] | 537 | } |
---|
[505] | 538 | Py_DECREF(list); |
---|
[1] | 539 | return res; |
---|
| 540 | } |
---|
[368] | 541 | |
---|
| 542 | PyObject* |
---|
[376] | 543 | PythonTranslate(PyObject* self, PyObject* args) |
---|
| 544 | { |
---|
| 545 | char *str; |
---|
| 546 | if (!PyArg_ParseTuple(args, "s", &str)){ |
---|
| 547 | #ifdef DEBUG |
---|
| 548 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 549 | #endif |
---|
| 550 | return NULL; |
---|
| 551 | } |
---|
| 552 | return PyString_FromString(_ss(str)); |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | PyObject* |
---|
[368] | 556 | PythonUpdateStatus(PyObject* self, PyObject* args) |
---|
| 557 | { |
---|
| 558 | maps* conf; |
---|
| 559 | PyObject* confdict; |
---|
| 560 | int istatus; |
---|
| 561 | char* status; |
---|
| 562 | if (!PyArg_ParseTuple(args, "O!i", &PyDict_Type, &confdict, &istatus)){ |
---|
| 563 | #ifdef DEBUG |
---|
| 564 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 565 | #endif |
---|
| 566 | return NULL; |
---|
| 567 | } |
---|
| 568 | if (istatus < 0 || istatus > 100){ |
---|
| 569 | PyErr_SetString(ZooError, "Status must be a percentage."); |
---|
| 570 | return NULL; |
---|
| 571 | }else{ |
---|
| 572 | char tmpStatus[4]; |
---|
| 573 | snprintf(tmpStatus, 4, "%i", istatus); |
---|
[453] | 574 | status = zStrdup(tmpStatus); |
---|
[368] | 575 | } |
---|
| 576 | /* now update the map */ |
---|
| 577 | { |
---|
[490] | 578 | PyObject* lenv = PyMapping_GetItemString(confdict, (char *)"lenv"); |
---|
[368] | 579 | if (lenv && PyMapping_Check(lenv)){ |
---|
[453] | 580 | PyObject* valobj = PyString_FromString(status); |
---|
[490] | 581 | PyMapping_SetItemString(lenv, (char *)"status", valobj); |
---|
[368] | 582 | Py_DECREF(valobj); |
---|
| 583 | } |
---|
| 584 | Py_DECREF(lenv); |
---|
| 585 | } |
---|
| 586 | conf = mapsFromPyDict((PyDictObject*)confdict); |
---|
| 587 | if (getMapFromMaps(conf,"lenv","status") != NULL){ |
---|
| 588 | if(status!=NULL){ |
---|
| 589 | setMapInMaps(conf,"lenv","status",status); |
---|
| 590 | free(status); |
---|
| 591 | } |
---|
| 592 | else |
---|
| 593 | setMapInMaps(conf,"lenv","status","15"); |
---|
[471] | 594 | _updateStatus(conf); |
---|
[368] | 595 | } |
---|
| 596 | freeMaps(&conf); |
---|
| 597 | free(conf); |
---|
| 598 | Py_RETURN_NONE; |
---|
| 599 | } |
---|