source: trunk/zoo-project/zoo-kernel/service_internal_python.c @ 362

Last change on this file since 362 was 360, checked in by djay, 12 years ago

Add support for multiple inputs values for the same identifier.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 10.9 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2012 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
27int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
28  maps* m=*main_conf;
29  maps* inputs=*real_inputs;
30  maps* outputs=*real_outputs;
31  char ntmp[1024];
32  getcwd(ntmp,1024);
33  map* tmp=NULL;
34  tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH");
35  char *python_path;
36#ifdef DEBUG
37  fprintf(stderr,"PYTHON SUPPORT \n");
38#endif
39  fflush(stderr);
40  if(tmp!=NULL){
41#ifdef DEBUG
42    fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value));
43#endif
44    python_path=(char*)malloc((strlen(tmp->value))*sizeof(char));
45    sprintf(python_path,"%s",tmp->value);
46  }
47  else{
48    python_path=strdup(".");
49  }
50  tmp=NULL;
51  tmp=getMap(request,"metapath");
52  char *pythonpath=(char*)malloc((1+strlen(python_path)+2048)*sizeof(char));
53  if(tmp!=NULL && strcmp(tmp->value,"")!=0)
54#ifdef WIN32
55    sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path);
56#else
57  sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
58#endif
59  else
60#ifdef WIN32
61    sprintf(pythonpath,"%s;%s",ntmp,python_path);
62#else
63  sprintf(pythonpath,"%s:%s",ntmp,python_path);
64#endif
65#ifdef DEBUG
66    fprintf(stderr,"PYTHONPATH=%s\n",pythonpath);
67#endif
68#ifndef WIN32
69  setenv("PYTHONPATH",pythonpath,1);
70#else
71  SetEnvironmentVariable("PYTHONPATH",pythonpath);
72#endif
73  free(python_path);
74  free(pythonpath);
75
76  PyThreadState *mainstate;
77  PyEval_InitThreads();
78  Py_Initialize();
79  mainstate = PyThreadState_Swap(NULL);
80  PyEval_ReleaseLock();
81  PyGILState_STATE gstate;
82  gstate = PyGILState_Ensure();
83  PyObject *pName, *pModule, *pFunc;
84  tmp=getMap(s->content,"serviceProvider");
85  if(tmp!=NULL)
86    pName = PyString_FromString(tmp->value);
87  else{
88    map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file.");
89    addToMap(err,"code","NoApplicableCode");
90    printExceptionReportResponse(m,err);
91    exit(-1);
92  }
93  pModule = PyImport_Import(pName);
94  int res=SERVICE_FAILED;
95  if (pModule != NULL) {
96    pFunc=PyObject_GetAttrString(pModule,s->name);
97    if (pFunc && PyCallable_Check(pFunc)){
98      PyObject *pValue;
99      PyDictObject* arg1=PyDict_FromMaps(m);
100      PyDictObject* arg2=PyDict_FromMaps(inputs);
101      PyDictObject* arg3=PyDict_FromMaps(outputs);
102      PyObject *pArgs=PyTuple_New(3);
103      if (!pArgs)
104        return -1;
105      PyTuple_SetItem(pArgs, 0, (PyObject *)arg1);
106      PyTuple_SetItem(pArgs, 1, (PyObject *)arg2);
107      PyTuple_SetItem(pArgs, 2, (PyObject *)arg3);
108      tmp=getMap(request,"storeExecuteResponse");
109#ifdef DEBUG
110      fprintf(stderr,"RUN IN NORMAL MODE \n");
111      fflush(stderr);
112#endif
113      pValue = PyObject_CallObject(pFunc, pArgs);
114      if (pValue != NULL) {
115        res=PyInt_AsLong(pValue);
116        freeMaps(real_outputs);
117        free(*real_outputs);
118        freeMaps(main_conf);
119        free(*main_conf);
120        *main_conf=mapsFromPyDict(arg1);
121        *real_outputs=mapsFromPyDict(arg3);
122#ifdef DEBUG
123        fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
124        dumpMaps(inputs);
125        dumpMaps(outputs);
126#endif
127      }else{     
128        PyObject *ptype,*pvalue, *ptraceback;
129        PyErr_Fetch(&ptype, &pvalue, &ptraceback);
130        PyObject *trace=PyObject_Str(pvalue);
131        char pbt[10240];
132        if(PyString_Check(trace))
133          sprintf(pbt,"TRACE : %s",PyString_AsString(trace));
134        else
135          fprintf(stderr,"EMPTY TRACE ?");
136        trace=NULL;
137        trace=PyObject_Str(ptype);
138        if(PyString_Check(trace)){
139          char *tpbt=strdup(pbt);
140          sprintf(pbt,"%s\n%s\0",tpbt,PyString_AsString(trace));
141          free(tpbt);
142        }
143        else
144          fprintf(stderr,"EMPTY TRACE ?");
145       
146        char *tpbt=strdup(pbt);
147        pName = PyString_FromString("traceback");
148        pModule = PyImport_Import(pName);
149        pArgs = PyTuple_New(1);
150        PyTuple_SetItem(pArgs, 0, ptraceback);
151        pFunc = PyObject_GetAttrString(pModule,"format_tb");
152        pValue = PyObject_CallObject(pFunc, pArgs);
153        trace=NULL;
154        trace=PyObject_Str(pValue);
155        if(PyString_Check(trace))
156          sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,PyString_AsString(trace));
157        else
158          sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations. %s",tpbt);
159        free(tpbt);
160        map* err=createMap("text",pbt);
161        addToMap(err,"code","NoApplicableCode");
162        printExceptionReportResponse(m,err);
163        res=-1;
164      }
165    }
166    else{
167      char tmpS[1024];
168      sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value);
169      map* tmps=createMap("text",tmpS);
170      printExceptionReportResponse(m,tmps);
171      res=-1;
172    }
173  } else{
174    char tmpS[1024];
175    sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value);
176    map* tmps=createMap("text",tmpS);
177    printExceptionReportResponse(m,tmps);
178    if (PyErr_Occurred())
179      PyErr_Print();
180    PyErr_Clear();
181    res=-1;
182    //exit(-1);
183  } 
184  PyGILState_Release(gstate);
185  PyEval_AcquireLock();
186  PyThreadState_Swap(mainstate);
187  Py_Finalize();
188  return res;
189}
190
191PyDictObject* PyDict_FromMaps(maps* t){
192  PyObject* res=PyDict_New( );
193  maps* tmp=t;
194  while(tmp!=NULL){
195    PyObject* value=(PyObject*)PyDict_FromMap(tmp->content);
196    PyObject* name=PyString_FromString(tmp->name);
197    if(PyDict_SetItem(res,name,value)<0){
198      fprintf(stderr,"Unable to set map value ...");
199      return NULL;
200    }
201    Py_DECREF(name);
202    tmp=tmp->next;
203  } 
204  return (PyDictObject*) res;
205}
206
207PyDictObject* PyDict_FromMap(map* t){
208  PyObject* res=PyDict_New( );
209  map* tmp=t;
210  int hasSize=0;
211  map* isArray=getMap(tmp,"isArray");
212  map* size=getMap(tmp,"size");
213  map* tmap=getMapType(tmp);
214  while(tmp!=NULL){
215    PyObject* name=PyString_FromString(tmp->name);
216    if(strcasecmp(tmp->name,"value")==0) {
217      if(isArray!=NULL){
218        map* len=getMap(tmp,"length");
219        int cnt=atoi(len->value);
220        PyObject* value=PyList_New(cnt);
221        PyObject* mvalue=PyList_New(cnt);
222        PyObject* svalue=PyList_New(cnt);
223
224        for(int i=0;i<cnt;i++){
225         
226          map* vMap=getMapArray(tmp,"value",i);     
227          map* sMap=getMapArray(tmp,"size",i);
228
229          if(vMap!=NULL){
230           
231            PyObject* lvalue;
232            PyObject* lsvalue;
233            if(sMap==NULL){
234              lvalue=PyString_FromString(vMap->value);
235              lsvalue=Py_None;
236            }
237            else{
238              lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value));
239              lsvalue=PyString_FromString(sMap->value);
240              hasSize=1;
241            }
242
243            if(PyList_SetItem(value,i,lvalue)<0){
244              fprintf(stderr,"Unable to set key value pair...");
245              return NULL;
246            } 
247            if(PyList_SetItem(svalue,i,lsvalue)<0){
248              fprintf(stderr,"Unable to set key value pair...");
249              return NULL;
250            } 
251          }
252         
253          map* mMap=getMapArray(tmp,tmap->name,i);
254          PyObject* lmvalue;
255          if(mMap!=NULL){
256            lmvalue=PyString_FromString(mMap->value);
257          }else
258            lmvalue=Py_None;
259         
260          if(PyList_SetItem(mvalue,i,lmvalue)<0){
261              fprintf(stderr,"Unable to set key value pair...");
262              return NULL;
263          } 
264         
265        }
266
267        if(PyDict_SetItem(res,name,value)<0){
268          fprintf(stderr,"Unable to set key value pair...");
269          return NULL;
270        }
271        if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){
272          fprintf(stderr,"Unable to set key value pair...");
273          return NULL;
274        }
275        if(hasSize>0)
276          if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){
277            fprintf(stderr,"Unable to set key value pair...");
278            return NULL;
279          }
280      }
281      else if(size!=NULL){
282        PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value));
283        if(PyDict_SetItem(res,name,value)<0){
284          fprintf(stderr,"Unable to set key value pair...");
285          return NULL;
286        }
287      }
288      else{
289        PyObject* value=PyString_FromString(tmp->value);
290        if(PyDict_SetItem(res,name,value)<0){
291          fprintf(stderr,"Unable to set key value pair...");
292          return NULL;
293        }
294      }
295    }
296    else{
297      if(PyDict_GetItem(res,name)==NULL){
298        PyObject* value=PyString_FromString(tmp->value);
299        if(PyDict_SetItem(res,name,value)<0){
300          fprintf(stderr,"Unable to set key value pair...");
301          return NULL;
302        }
303      }
304    }
305    Py_DECREF(name);
306    tmp=tmp->next;
307  }
308  return (PyDictObject*) res;
309}
310
311maps* mapsFromPyDict(PyDictObject* t){
312  maps* res=NULL;
313  maps* cursor=res;
314  PyObject* list=PyDict_Keys((PyObject*)t);
315  int nb=PyList_Size(list);
316  int i;
317  for(i=0;i<nb;i++){
318#ifdef DEBUG
319    fprintf(stderr,">> parsing maps %d\n",i);
320#endif
321    PyObject* key=PyList_GetItem(list,i);
322    PyObject* value=PyDict_GetItem((PyObject*)t,key);
323#ifdef DEBUG
324    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
325            PyString_AsString(key),PyString_AsString(value));
326#endif
327    cursor=(maps*)malloc(MAPS_SIZE);
328    cursor->name=PyString_AsString(key);
329    cursor->content=mapFromPyDict((PyDictObject*)value);
330#ifdef DEBUG
331    dumpMap(cursor->content);
332#endif
333    cursor->next=NULL;
334    if(res==NULL)
335      res=dupMaps(&cursor);
336    else
337      addMapsToMaps(&res,cursor);
338    freeMap(&cursor->content);
339    free(cursor->content);
340    free(cursor);
341#ifdef DEBUG
342    dumpMaps(res);
343    fprintf(stderr,">> parsed maps %d\n",i);
344#endif
345  }
346  return res;
347}
348
349map* mapFromPyDict(PyDictObject* t){
350  map* res=NULL;
351  PyObject* list=PyDict_Keys((PyObject*)t);
352  int nb=PyList_Size(list);
353  int i;
354  for(i=0;i<nb;i++){
355    PyObject* key=PyList_GetItem(list,i);
356    PyObject* value=PyDict_GetItem((PyObject*)t,key);
357#ifdef DEBUG
358    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
359            PyString_AsString(key),PyString_AsString(value));
360#endif
361    if(strcmp(PyString_AsString(key),"value")==0){
362      char *buffer=NULL;
363      Py_ssize_t size;
364      PyString_AsStringAndSize(value,&buffer,&size);
365      if(res!=NULL){
366        addToMap(res,PyString_AsString(key),"");
367      }else{
368        res=createMap(PyString_AsString(key),"");
369      }
370      map* tmpR=getMap(res,"value");
371      free(tmpR->value);
372      tmpR->value=(char*)malloc((size+1)*sizeof(char));
373      memmove(tmpR->value,buffer,size*sizeof(char));
374      tmpR->value[size]=0;
375      char sin[1024];
376      sprintf(sin,"%d",size);
377      addToMap(res,"size",sin);
378    }else{
379      if(res!=NULL)
380        addToMap(res,PyString_AsString(key),PyString_AsString(value));
381      else
382        res=createMap(PyString_AsString(key),PyString_AsString(value));
383    }
384  }
385  return res;
386}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png