1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2013 GeoLabs SARL |
---|
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 | |
---|
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 | |
---|
38 | static PyObject* ZooError; |
---|
39 | |
---|
40 | PyMethodDef zooMethods[] = { |
---|
41 | {"_", PythonTranslate, METH_VARARGS, "Translate a string using the zoo-services textdomain."}, |
---|
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 | |
---|
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 | |
---|
71 | PyMODINIT_FUNC init_zoo(){ |
---|
72 | PyObject *tmp,*d; |
---|
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 |
---|
83 | return; |
---|
84 | #endif |
---|
85 | } |
---|
86 | |
---|
87 | struct module_state *st = GETSTATE(module); |
---|
88 | |
---|
89 | d = PyModule_GetDict(module); |
---|
90 | #if PY_MAJOR_VERSION >= 3 |
---|
91 | tmp = PyLong_FromLong(3); |
---|
92 | #else |
---|
93 | tmp = PyInt_FromLong(3); |
---|
94 | #endif |
---|
95 | PyDict_SetItemString(d, "SERVICE_SUCCEEDED", tmp); |
---|
96 | Py_DECREF(tmp); |
---|
97 | |
---|
98 | #if PY_MAJOR_VERSION >= 3 |
---|
99 | tmp = PyLong_FromLong(4); |
---|
100 | #else |
---|
101 | tmp = PyInt_FromLong(4); |
---|
102 | #endif |
---|
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); |
---|
109 | #if PY_MAJOR_VERSION >= 3 |
---|
110 | return module; |
---|
111 | #endif |
---|
112 | } |
---|
113 | |
---|
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; |
---|
118 | map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd"); |
---|
119 | char *ntmp=tmp0->value; |
---|
120 | map* tmp=NULL; |
---|
121 | tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH"); |
---|
122 | char *python_path; |
---|
123 | #ifdef DEBUG |
---|
124 | fprintf(stderr,"PYTHON SUPPORT \n"); |
---|
125 | #endif |
---|
126 | fflush(stderr); |
---|
127 | if(tmp!=NULL){ |
---|
128 | #ifdef DEBUG |
---|
129 | fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value)); |
---|
130 | #endif |
---|
131 | python_path=(char*)malloc((strlen(tmp->value))*sizeof(char)); |
---|
132 | sprintf(python_path,"%s",tmp->value); |
---|
133 | } |
---|
134 | else{ |
---|
135 | python_path=strdup("."); |
---|
136 | } |
---|
137 | tmp=NULL; |
---|
138 | tmp=getMap(request,"metapath"); |
---|
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)); |
---|
142 | #ifdef WIN32 |
---|
143 | sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); |
---|
144 | #else |
---|
145 | sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); |
---|
146 | #endif |
---|
147 | } |
---|
148 | else{ |
---|
149 | pythonpath=(char*)malloc((2+strlen(python_path)+strlen(ntmp))*sizeof(char)); |
---|
150 | #ifdef WIN32 |
---|
151 | sprintf(pythonpath,"%s;%s",ntmp,python_path); |
---|
152 | #else |
---|
153 | sprintf(pythonpath,"%s:%s",ntmp,python_path); |
---|
154 | #endif |
---|
155 | } |
---|
156 | #ifdef DEBUG |
---|
157 | fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); |
---|
158 | #endif |
---|
159 | #ifndef WIN32 |
---|
160 | setenv("PYTHONPATH",pythonpath,1); |
---|
161 | #else |
---|
162 | SetEnvironmentVariable("PYTHONPATH",pythonpath); |
---|
163 | char* toto=(char*)malloc((strlen(pythonpath)+12)*sizeof(char)); |
---|
164 | sprintf(toto,"PYTHONPATH=%s",pythonpath); |
---|
165 | putenv(toto); |
---|
166 | #endif |
---|
167 | free(python_path); |
---|
168 | free(pythonpath); |
---|
169 | |
---|
170 | PyThreadState *mainstate; |
---|
171 | #if PY_MAJOR_VERSION >= 3 |
---|
172 | PyImport_AppendInittab("zoo", init_zoo); |
---|
173 | #else |
---|
174 | PyEval_InitThreads(); |
---|
175 | #endif |
---|
176 | Py_Initialize(); |
---|
177 | #if PY_MAJOR_VERSION >= 3 |
---|
178 | PyEval_InitThreads(); |
---|
179 | PyImport_ImportModule("zoo"); |
---|
180 | #else |
---|
181 | init_zoo(); |
---|
182 | #endif |
---|
183 | mainstate = PyThreadState_Swap(NULL); |
---|
184 | PyEval_ReleaseLock(); |
---|
185 | PyGILState_STATE gstate; |
---|
186 | gstate = PyGILState_Ensure(); |
---|
187 | PyObject *pName, *pModule, *pFunc; |
---|
188 | tmp=getMap(s->content,"serviceProvider"); |
---|
189 | if(tmp!=NULL) |
---|
190 | pName = |
---|
191 | #if PY_MAJOR_VERSION >= 3 |
---|
192 | PyUnicode_FromString(tmp->value); |
---|
193 | #else |
---|
194 | PyString_FromString(tmp->value); |
---|
195 | #endif |
---|
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 | } |
---|
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)){ |
---|
207 | PyObject *pValue; |
---|
208 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
209 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
210 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
211 | PyObject *pArgs=PyTuple_New(3); |
---|
212 | if (!pArgs) |
---|
213 | return -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"); |
---|
220 | fflush(stderr); |
---|
221 | #endif |
---|
222 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
223 | if (pValue != NULL) { |
---|
224 | #if PY_MAJOR_VERSION >= 3 |
---|
225 | res=PyLong_AsLong(pValue); |
---|
226 | #else |
---|
227 | res=PyInt_AsLong(pValue); |
---|
228 | #endif |
---|
229 | freeMaps(real_outputs); |
---|
230 | free(*real_outputs); |
---|
231 | freeMaps(main_conf); |
---|
232 | free(*main_conf); |
---|
233 | *main_conf=mapsFromPyDict(arg1); |
---|
234 | *real_outputs=mapsFromPyDict(arg3); |
---|
235 | #ifdef DEBUG |
---|
236 | #if PY_MAJOR_VERSION >= 3 |
---|
237 | fprintf(stderr,"Result of call: %i\n", PyLong_AsLong(pValue)); |
---|
238 | #else |
---|
239 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
240 | #endif |
---|
241 | dumpMaps(inputs); |
---|
242 | dumpMaps(*real_outputs); |
---|
243 | #endif |
---|
244 | }else{ |
---|
245 | PyObject *ptype,*pvalue, *ptraceback; |
---|
246 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
247 | PyObject *trace=PyObject_Str(pvalue); |
---|
248 | char pbt[10240]; |
---|
249 | #if PY_MAJOR_VERSION >= 3 |
---|
250 | if(PyUnicode_Check(trace)) |
---|
251 | sprintf(pbt,"TRACE : %s",_PyUnicode_AsString(trace)); |
---|
252 | #else |
---|
253 | if(PyString_Check(trace)) |
---|
254 | sprintf(pbt,"TRACE : %s",PyString_AsString(trace)); |
---|
255 | #endif |
---|
256 | else |
---|
257 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
258 | trace=NULL; |
---|
259 | trace=PyObject_Str(ptype); |
---|
260 | #if PY_MAJOR_VERSION >= 3 |
---|
261 | if(PyUnicode_Check(trace)){ |
---|
262 | #else |
---|
263 | if(PyString_Check(trace)){ |
---|
264 | #endif |
---|
265 | char *tpbt=strdup(pbt); |
---|
266 | #if PY_MAJOR_VERSION >= 3 |
---|
267 | sprintf(pbt,"%s\n%s\0",tpbt,_PyUnicode_AsString(trace)); |
---|
268 | #else |
---|
269 | sprintf(pbt,"%s\n%s\0",tpbt,PyString_AsString(trace)); |
---|
270 | #endif |
---|
271 | free(tpbt); |
---|
272 | } |
---|
273 | else |
---|
274 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
275 | |
---|
276 | char *tpbt=strdup(pbt); |
---|
277 | #if PY_MAJOR_VERSION >= 3 |
---|
278 | pName = PyUnicode_FromString("traceback"); |
---|
279 | #else |
---|
280 | pName = PyString_FromString("traceback"); |
---|
281 | #endif |
---|
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); |
---|
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 |
---|
293 | if(PyString_Check(trace)) |
---|
294 | sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,PyString_AsString(trace)); |
---|
295 | #endif |
---|
296 | else |
---|
297 | sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations. %s",tpbt); |
---|
298 | free(tpbt); |
---|
299 | map* err=createMap("text",pbt); |
---|
300 | addToMap(err,"code","NoApplicableCode"); |
---|
301 | printExceptionReportResponse(m,err); |
---|
302 | res=-1; |
---|
303 | } |
---|
304 | } |
---|
305 | else{ |
---|
306 | char tmpS[1024]; |
---|
307 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
308 | map* tmps=createMap("text",tmpS); |
---|
309 | printExceptionReportResponse(m,tmps); |
---|
310 | res=-1; |
---|
311 | } |
---|
312 | } else{ |
---|
313 | char tmpS[1024]; |
---|
314 | sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value); |
---|
315 | map* tmps=createMap("text",tmpS); |
---|
316 | printExceptionReportResponse(m,tmps); |
---|
317 | if (PyErr_Occurred()) |
---|
318 | PyErr_Print(); |
---|
319 | PyErr_Clear(); |
---|
320 | res=-1; |
---|
321 | //exit(-1); |
---|
322 | } |
---|
323 | #if PY_MAJOR_VERSION < 3 |
---|
324 | PyGILState_Release(gstate); |
---|
325 | PyEval_AcquireLock(); |
---|
326 | #endif |
---|
327 | PyThreadState_Swap(mainstate); |
---|
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){ |
---|
336 | PyObject* value=(PyObject*)PyDict_FromMap(tmp->content); |
---|
337 | PyObject* name= |
---|
338 | #if PY_MAJOR_VERSION >= 3 |
---|
339 | PyUnicode_FromString(tmp->name); |
---|
340 | #else |
---|
341 | PyString_FromString(tmp->name); |
---|
342 | #endif |
---|
343 | if(PyDict_SetItem(res,name,value)<0){ |
---|
344 | fprintf(stderr,"Unable to set map value ..."); |
---|
345 | return NULL; |
---|
346 | } |
---|
347 | Py_DECREF(name); |
---|
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; |
---|
356 | int hasSize=0; |
---|
357 | map* isArray=getMap(tmp,"isArray"); |
---|
358 | map* size=getMap(tmp,"size"); |
---|
359 | map* tmap=getMapType(tmp); |
---|
360 | while(tmp!=NULL){ |
---|
361 | PyObject* name= |
---|
362 | #if PY_MAJOR_VERSION >= 3 |
---|
363 | PyUnicode_FromString(tmp->name); |
---|
364 | #else |
---|
365 | PyString_FromString(tmp->name); |
---|
366 | #endif |
---|
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){ |
---|
385 | #if PY_MAJOR_VERSION >= 3 |
---|
386 | lvalue=PyUnicode_FromString(vMap->value); |
---|
387 | #else |
---|
388 | lvalue=PyString_FromString(vMap->value); |
---|
389 | #endif |
---|
390 | lsvalue=Py_None; |
---|
391 | } |
---|
392 | else{ |
---|
393 | #if PY_MAJOR_VERSION >= 3 |
---|
394 | lvalue=PyUnicode_FromStringAndSize(vMap->value,atoi(sMap->value)); |
---|
395 | lsvalue=PyUnicode_FromString(sMap->value); |
---|
396 | #else |
---|
397 | lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value)); |
---|
398 | lsvalue=PyString_FromString(sMap->value); |
---|
399 | #endif |
---|
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){ |
---|
416 | #if PY_MAJOR_VERSION >= 3 |
---|
417 | lmvalue=PyUnicode_FromString(mMap->value); |
---|
418 | #else |
---|
419 | lmvalue=PyString_FromString(mMap->value); |
---|
420 | #endif |
---|
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 | } |
---|
435 | #if PY_MAJOR_VERSION >= 3 |
---|
436 | if(PyDict_SetItem(res,PyUnicode_FromString(tmap->name),mvalue)<0){ |
---|
437 | #else |
---|
438 | if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){ |
---|
439 | #endif |
---|
440 | fprintf(stderr,"Unable to set key value pair..."); |
---|
441 | return NULL; |
---|
442 | } |
---|
443 | if(hasSize>0) |
---|
444 | #if PY_MAJOR_VERSION >= 3 |
---|
445 | if(PyDict_SetItem(res,PyUnicode_FromString("size"),svalue)<0){ |
---|
446 | #else |
---|
447 | if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){ |
---|
448 | #endif |
---|
449 | fprintf(stderr,"Unable to set key value pair..."); |
---|
450 | return NULL; |
---|
451 | } |
---|
452 | } |
---|
453 | else if(size!=NULL){ |
---|
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 |
---|
460 | if(PyDict_SetItem(res,name,value)<0){ |
---|
461 | fprintf(stderr,"Unable to set key value pair..."); |
---|
462 | return NULL; |
---|
463 | } |
---|
464 | } |
---|
465 | else{ |
---|
466 | PyObject* value= |
---|
467 | #if PY_MAJOR_VERSION >= 3 |
---|
468 | PyUnicode_FromString(tmp->value); |
---|
469 | #else |
---|
470 | PyString_FromString(tmp->value); |
---|
471 | #endif |
---|
472 | if(PyDict_SetItem(res,name,value)<0){ |
---|
473 | fprintf(stderr,"Unable to set key value pair..."); |
---|
474 | return NULL; |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | else{ |
---|
479 | if(PyDict_GetItem(res,name)==NULL){ |
---|
480 | PyObject* value= |
---|
481 | #if PY_MAJOR_VERSION >= 3 |
---|
482 | PyUnicode_FromString(tmp->value); |
---|
483 | #else |
---|
484 | PyString_FromString(tmp->value); |
---|
485 | #endif |
---|
486 | if(PyDict_SetItem(res,name,value)<0){ |
---|
487 | fprintf(stderr,"Unable to set key value pair..."); |
---|
488 | return NULL; |
---|
489 | } |
---|
490 | } |
---|
491 | } |
---|
492 | Py_DECREF(name); |
---|
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 |
---|
514 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
515 | #if PY_MAJOR_VERSION >= 3 |
---|
516 | cursor->name=_PyUnicode_AsString(key); |
---|
517 | #else |
---|
518 | cursor->name=PyString_AsString(key); |
---|
519 | #endif |
---|
520 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
521 | #ifdef DEBUG |
---|
522 | dumpMap(cursor->content); |
---|
523 | #endif |
---|
524 | cursor->next=NULL; |
---|
525 | if(res==NULL) |
---|
526 | res=dupMaps(&cursor); |
---|
527 | else |
---|
528 | addMapsToMaps(&res,cursor); |
---|
529 | freeMap(&cursor->content); |
---|
530 | free(cursor->content); |
---|
531 | free(cursor); |
---|
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 |
---|
552 | #if PY_MAJOR_VERSION >= 3 |
---|
553 | if(strcmp(_PyUnicode_AsString(key),"value")==0){ |
---|
554 | #else |
---|
555 | if(strcmp(PyString_AsString(key),"value")==0){ |
---|
556 | #endif |
---|
557 | char *buffer=NULL; |
---|
558 | Py_ssize_t size; |
---|
559 | #if PY_MAJOR_VERSION >= 3 |
---|
560 | buffer=_PyUnicode_AsStringAndSize(value,&size); |
---|
561 | #else |
---|
562 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
563 | #endif |
---|
564 | if(res!=NULL){ |
---|
565 | #if PY_MAJOR_VERSION >= 3 |
---|
566 | addToMap(res,_PyUnicode_AsString(key),""); |
---|
567 | #else |
---|
568 | addToMap(res,PyString_AsString(key),""); |
---|
569 | #endif |
---|
570 | }else{ |
---|
571 | #if PY_MAJOR_VERSION >= 3 |
---|
572 | res=createMap(_PyUnicode_AsString(key),""); |
---|
573 | #else |
---|
574 | res=createMap(PyString_AsString(key),""); |
---|
575 | #endif |
---|
576 | } |
---|
577 | map* tmpR=getMap(res,"value"); |
---|
578 | free(tmpR->value); |
---|
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); |
---|
585 | }else{ |
---|
586 | if(res!=NULL){ |
---|
587 | if(PyString_Size(value)>0) |
---|
588 | #if PY_MAJOR_VERSION >= 3 |
---|
589 | addToMap(res,_PyUnicode_AsString(key),_PyUnicode_AsString(value)); |
---|
590 | #else |
---|
591 | addToMap(res,PyString_AsString(key),PyString_AsString(value)); |
---|
592 | #endif |
---|
593 | } |
---|
594 | else{ |
---|
595 | if(PyString_Size(value)>0) |
---|
596 | res= |
---|
597 | #if PY_MAJOR_VERSION >= 3 |
---|
598 | createMap(_PyUnicode_AsString(key),_PyUnicode_AsString(value)); |
---|
599 | #else |
---|
600 | createMap(PyString_AsString(key),PyString_AsString(value)); |
---|
601 | #endif |
---|
602 | } |
---|
603 | } |
---|
604 | } |
---|
605 | return res; |
---|
606 | } |
---|
607 | |
---|
608 | PyObject* |
---|
609 | PythonTranslate(PyObject* self, PyObject* args) |
---|
610 | { |
---|
611 | char *str; |
---|
612 | if (!PyArg_ParseTuple(args, "s", &str)){ |
---|
613 | #ifdef DEBUG |
---|
614 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
615 | #endif |
---|
616 | return NULL; |
---|
617 | } |
---|
618 | #if PY_MAJOR_VERSION >= 3 |
---|
619 | return PyUnicode_FromString(_ss(str)); |
---|
620 | #else |
---|
621 | return PyString_FromString(_ss(str)); |
---|
622 | #endif |
---|
623 | } |
---|
624 | |
---|
625 | PyObject* |
---|
626 | PythonUpdateStatus(PyObject* self, PyObject* args) |
---|
627 | { |
---|
628 | maps* conf; |
---|
629 | PyObject* confdict; |
---|
630 | int istatus; |
---|
631 | char* status; |
---|
632 | if (!PyArg_ParseTuple(args, "O!i", &PyDict_Type, &confdict, &istatus)){ |
---|
633 | #ifdef DEBUG |
---|
634 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
635 | #endif |
---|
636 | return NULL; |
---|
637 | } |
---|
638 | if (istatus < 0 || istatus > 100){ |
---|
639 | PyErr_SetString(ZooError, "Status must be a percentage."); |
---|
640 | return NULL; |
---|
641 | }else{ |
---|
642 | char tmpStatus[4]; |
---|
643 | snprintf(tmpStatus, 4, "%i", istatus); |
---|
644 | status = strdup(tmpStatus); |
---|
645 | } |
---|
646 | /* now update the map */ |
---|
647 | { |
---|
648 | PyObject* lenv = PyMapping_GetItemString(confdict, "lenv"); |
---|
649 | if (lenv && PyMapping_Check(lenv)){ |
---|
650 | PyObject* valobj = |
---|
651 | #if PY_MAJOR_VERSION >= 3 |
---|
652 | PyUnicode_FromString(status); |
---|
653 | #else |
---|
654 | PyString_FromString(status); |
---|
655 | #endif |
---|
656 | PyMapping_SetItemString(lenv, "status", valobj); |
---|
657 | Py_DECREF(valobj); |
---|
658 | } |
---|
659 | Py_DECREF(lenv); |
---|
660 | } |
---|
661 | conf = mapsFromPyDict((PyDictObject*)confdict); |
---|
662 | if (getMapFromMaps(conf,"lenv","status") != NULL){ |
---|
663 | fprintf(stderr,"STATUS RETURNED : %s\n",status); |
---|
664 | if(status!=NULL){ |
---|
665 | setMapInMaps(conf,"lenv","status",status); |
---|
666 | free(status); |
---|
667 | } |
---|
668 | else |
---|
669 | setMapInMaps(conf,"lenv","status","15"); |
---|
670 | updateStatus(conf); |
---|
671 | } |
---|
672 | freeMaps(&conf); |
---|
673 | free(conf); |
---|
674 | Py_RETURN_NONE; |
---|
675 | } |
---|