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