source: trunk/zoo-kernel/service_internal_python.c @ 20

Last change on this file since 20 was 9, checked in by djay, 14 years ago

Update of both ZOO Kernel and ZOO Services (ogr base-vect-ops ServicesProvider?).
All the ZCFG files have been corrected to remove all references to wrong metadata (Test = Demo) to avoid validation issues.
Main Memory leaks has been removed from this version.
Addition of the Simplify Service in the C ogr base-vect-ops ServicesProvider? and addition of the Python version (without Simplify).
Update of the configure.ac and Makefile.in to follow dicussions on the mailing list and ensure to use our cgic206 and not another one, path to our cgic library is now directly in the Makefile.in file.
Accept the "-" character to name inputs, to solve issue on GRASS 7 integration.
Addition of the extension keyword for ZCFG file to be able to store resulting outputs in a file name using the extension suffix.
This version after a testing period shall be considerate as 1.0.1 version of the ZOO Project.

File size: 8.0 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2010 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  fprintf(stderr,"PYTHON SUPPORT \n");
37  fflush(stderr);
38  if(tmp!=NULL){
39    fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value));
40    python_path=(char*)malloc((strlen(tmp->value))*sizeof(char));
41    sprintf(python_path,"%s",tmp->value);
42  }
43  else{
44    python_path=strdup(".");
45  }
46  tmp=NULL;
47  tmp=getMap(request,"metapath");
48  char *pythonpath=(char*)malloc((1+strlen(python_path)+2048)*sizeof(char));
49  if(tmp!=NULL && strcmp(tmp->value,"")!=0)
50#ifdef WIN32
51    sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path);
52#else
53  sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
54#endif
55  else
56#ifdef WIN32
57    sprintf(pythonpath,"%s;%s",ntmp,python_path);
58#else
59  sprintf(pythonpath,"%s:%s",ntmp,python_path);
60#endif
61  //#ifdef DEBUG
62    fprintf(stderr,"PYTHONPATH=%s\n",pythonpath);
63  //#endif
64#ifndef WIN32
65  setenv("PYTHONPATH",pythonpath,1);
66#else
67  SetEnvironmentVariable("PYTHONPATH",pythonpath);
68#endif
69  free(python_path);
70  free(pythonpath);
71
72  Py_Initialize();
73  PyObject *pName, *pModule, *pFunc;
74  tmp=getMap(s->content,"serviceProvider");
75  if(tmp!=NULL)
76    pName = PyString_FromString(tmp->value);
77  else{
78    map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file.");
79    addToMap(err,"code","NoApplicableCode");
80    printExceptionReportResponse(m,err);
81    exit(-1);
82  }
83  pModule = PyImport_Import(pName);
84  int i;
85  int res=SERVICE_FAILED;
86  int cpid=getpid();
87  if (pModule != NULL) {
88    pFunc=PyObject_GetAttrString(pModule,s->name);
89    if (pFunc && PyCallable_Check(pFunc)){
90      PyDictObject* arg1=PyDict_FromMaps(m);
91      PyDictObject* arg2=PyDict_FromMaps(inputs);
92      PyDictObject* arg3=PyDict_FromMaps(outputs);
93      PyObject *pArgs=PyTuple_New(3);
94      PyObject *pValue;
95      PyTuple_SetItem(pArgs, 0, (PyObject *)arg1);
96      PyTuple_SetItem(pArgs, 1, (PyObject *)arg2);
97      PyTuple_SetItem(pArgs, 2, (PyObject *)arg3);
98      tmp=getMap(request,"storeExecuteResponse");
99#ifdef DEBUG
100      fprintf(stderr,"RUN IN NORMAL MODE \n");
101      fflush(stderr);
102#endif
103      pValue = PyObject_CallObject(pFunc, pArgs);
104      if (pValue != NULL) {
105        res=PyInt_AsLong(pValue);
106        freeMaps(real_outputs);
107        free(*real_outputs);
108        //*real_inputs=mapsFromPyDict(arg2);
109        //createMapsFromPyDict(real_outputs,arg3);
110        *real_outputs=mapsFromPyDict(arg3);
111#ifdef DEBUG
112        fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
113        dumpMaps(inputs);
114        dumpMaps(outputs);
115#endif
116        Py_DECREF(arg1);
117        Py_DECREF(arg2);
118        Py_DECREF(arg3);
119        Py_DECREF(pArgs);
120        Py_DECREF(pValue);
121        Py_XDECREF(pFunc);
122        Py_DECREF(pModule);
123      }else{     
124        PyObject *ptype,*pvalue, *ptraceback;
125        PyErr_Fetch(&ptype, &pvalue, &ptraceback);
126        PyObject *trace=PyObject_Str(pvalue);
127        char tb[1024];
128        char pbt[10240];
129        if(PyString_Check(trace))
130          sprintf(pbt,"TRACE : %s",PyString_AsString(trace));
131        else
132          fprintf(stderr,"EMPTY TRACE ?");
133        trace=NULL;
134        trace=PyObject_Str(ptype);
135        if(PyString_Check(trace))
136          sprintf(pbt,"%s\nTRACE : %s",strdup(pbt),PyString_AsString(trace));
137        else
138          fprintf(stderr,"EMPTY TRACE ?");
139        PyObject *t;
140        pName = PyString_FromString("traceback");
141        pModule = PyImport_Import(pName);
142        pArgs = PyTuple_New(1);
143        PyTuple_SetItem(pArgs, 0, ptraceback);
144        pFunc = PyObject_GetAttrString(pModule,"format_tb");
145        pValue = PyObject_CallObject(pFunc, pArgs);
146        trace=NULL;
147        trace=PyObject_Str(pValue);
148        if(PyString_Check(trace))
149          sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",strdup(pbt),PyString_AsString(trace));
150        else
151          sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",strdup(pbt));
152        map* err=createMap("text",pbt);
153        addToMap(err,"code","NoApplicableCode");
154        printExceptionReportResponse(m,err);
155        Py_XDECREF(pFunc);
156        Py_DECREF(pArgs);
157        Py_DECREF(pModule);
158        exit(-1);
159      }
160    }
161    else{
162      char tmpS[1024];
163      sprintf(tmpS, "Cannot find the %s function int the %s file.\n", s->name, tmp->value);
164      map* tmps=createMap("text",tmpS);
165      printExceptionReportResponse(m,tmps);
166      Py_XDECREF(pFunc);
167      Py_DECREF(pModule);
168      exit(-1);
169    }
170  } else{
171    char tmpS[1024];
172    sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value);
173    map* tmps=createMap("text",tmpS);
174    printExceptionReportResponse(m,tmps);
175    if (PyErr_Occurred())
176      PyErr_Print();
177    exit(-1);
178  } 
179#ifndef DEBUG
180  // Failed when DEBUG is defined
181  Py_Finalize();
182#endif
183  return res;
184}
185
186PyDictObject* PyDict_FromMaps(maps* t){
187  PyObject* res=PyDict_New( );
188  maps* tmp=t;
189  while(tmp!=NULL){
190    if(PyDict_SetItem(res,PyString_FromString(tmp->name),(PyObject*)PyDict_FromMap(tmp->content))<0){
191      fprintf(stderr,"Unable to parse params...");
192      exit(1);
193    }
194    tmp=tmp->next;
195  } 
196  return (PyDictObject*) res;
197}
198
199PyDictObject* PyDict_FromMap(map* t){
200  PyObject* res=PyDict_New( );
201  map* tmp=t;
202  while(tmp!=NULL){
203    if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromString(tmp->value))<0){
204      fprintf(stderr,"Unable to parse params...");
205      exit(1);
206    }
207    tmp=tmp->next;
208  }
209  return (PyDictObject*) res;
210}
211
212maps* mapsFromPyDict(PyDictObject* t){
213  maps* res=NULL;
214  maps* cursor=res;
215  PyObject* list=PyDict_Keys((PyObject*)t);
216  int nb=PyList_Size(list);
217  int i;
218  for(i=0;i<nb;i++){
219#ifdef DEBUG
220    fprintf(stderr,">> parsing maps %d\n",i);
221#endif
222    PyObject* key=PyList_GetItem(list,i);
223    PyObject* value=PyDict_GetItem((PyObject*)t,key);
224#ifdef DEBUG
225    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
226            PyString_AsString(key),PyString_AsString(value));
227#endif
228    while(cursor!=NULL){
229      cursor=cursor->next;
230    }
231    cursor=(maps*)malloc(MAPS_SIZE);
232    cursor->name=PyString_AsString(key);
233#ifdef DEBUG
234    dumpMap(mapFromPyDict((PyDictObject*)value));
235#endif
236    cursor->content=mapFromPyDict((PyDictObject*)value);
237    cursor->next=NULL;
238    if(res==NULL)
239      res=cursor;
240    else
241      addMapsToMaps(&res,cursor);
242#ifdef DEBUG
243    dumpMaps(res);
244    fprintf(stderr,">> parsed maps %d\n",i);
245#endif
246  }
247  return res;
248}
249
250map* mapFromPyDict(PyDictObject* t){
251  map* res=NULL;
252  PyObject* list=PyDict_Keys((PyObject*)t);
253  int nb=PyList_Size(list);
254  int i;
255  for(i=0;i<nb;i++){
256    PyObject* key=PyList_GetItem(list,i);
257    PyObject* value=PyDict_GetItem((PyObject*)t,key);
258#ifdef DEBUG
259    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
260            PyString_AsString(key),PyString_AsString(value));
261#endif
262    if(res!=NULL)
263      addToMap(res,PyString_AsString(key),PyString_AsString(value));
264    else
265      res=createMap(PyString_AsString(key),PyString_AsString(value));
266  }
267  return res;
268}
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