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

Last change on this file since 58 was 58, checked in by djay, 13 years ago

Add binary files support for DataInputs?. Managing this binary format for the Python language, this should resolve the GRASS support in case of binary DataInputs?.

File size: 8.5 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        *main_conf=mapsFromPyDict(arg1);
111        *real_outputs=mapsFromPyDict(arg3);
112#ifdef DEBUG
113        fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
114        dumpMaps(inputs);
115        dumpMaps(outputs);
116#endif
117        Py_DECREF(arg1);
118        Py_DECREF(arg2);
119        Py_DECREF(arg3);
120        Py_DECREF(pArgs);
121        Py_DECREF(pValue);
122        Py_XDECREF(pFunc);
123        Py_DECREF(pModule);
124      }else{     
125        PyObject *ptype,*pvalue, *ptraceback;
126        PyErr_Fetch(&ptype, &pvalue, &ptraceback);
127        PyObject *trace=PyObject_Str(pvalue);
128        char tb[1024];
129        char pbt[10240];
130        if(PyString_Check(trace))
131          sprintf(pbt,"TRACE : %s",PyString_AsString(trace));
132        else
133          fprintf(stderr,"EMPTY TRACE ?");
134        trace=NULL;
135        trace=PyObject_Str(ptype);
136        if(PyString_Check(trace))
137          sprintf(pbt,"%s\nTRACE : %s",strdup(pbt),PyString_AsString(trace));
138        else
139          fprintf(stderr,"EMPTY TRACE ?");
140        PyObject *t;
141        pName = PyString_FromString("traceback");
142        pModule = PyImport_Import(pName);
143        pArgs = PyTuple_New(1);
144        PyTuple_SetItem(pArgs, 0, ptraceback);
145        pFunc = PyObject_GetAttrString(pModule,"format_tb");
146        pValue = PyObject_CallObject(pFunc, pArgs);
147        trace=NULL;
148        trace=PyObject_Str(pValue);
149        if(PyString_Check(trace))
150          sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",strdup(pbt),PyString_AsString(trace));
151        else
152          sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",strdup(pbt));
153        map* err=createMap("text",pbt);
154        addToMap(err,"code","NoApplicableCode");
155        printExceptionReportResponse(m,err);
156        Py_XDECREF(pFunc);
157        Py_DECREF(pArgs);
158        Py_DECREF(pModule);
159        exit(-1);
160      }
161    }
162    else{
163      char tmpS[1024];
164      sprintf(tmpS, "Cannot find the %s function int the %s file.\n", s->name, tmp->value);
165      map* tmps=createMap("text",tmpS);
166      printExceptionReportResponse(m,tmps);
167      Py_XDECREF(pFunc);
168      Py_DECREF(pModule);
169      exit(-1);
170    }
171  } else{
172    char tmpS[1024];
173    sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value);
174    map* tmps=createMap("text",tmpS);
175    printExceptionReportResponse(m,tmps);
176    if (PyErr_Occurred())
177      PyErr_Print();
178    exit(-1);
179  } 
180#ifndef DEBUG
181  // Failed when DEBUG is defined
182  Py_Finalize();
183#endif
184  return res;
185}
186
187PyDictObject* PyDict_FromMaps(maps* t){
188  PyObject* res=PyDict_New( );
189  maps* tmp=t;
190  while(tmp!=NULL){
191    if(PyDict_SetItem(res,PyString_FromString(tmp->name),(PyObject*)PyDict_FromMap(tmp->content))<0){
192      fprintf(stderr,"Unable to parse params...");
193      exit(1);
194    }
195    tmp=tmp->next;
196  } 
197  return (PyDictObject*) res;
198}
199
200PyDictObject* PyDict_FromMap(map* t){
201  PyObject* res=PyDict_New( );
202  map* tmp=t;
203  map* size=getMap(tmp,"size");
204  dumpMap(t);
205  while(tmp!=NULL){
206    fprintf(stderr,"%s => %s\n"),tmp->name,tmp->value;
207    if(strcasecmp(tmp->name,"value")==0){
208      if(size!=NULL){
209        if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromStringAndSize(tmp->value,(Py_ssize_t) atoi(size->value)))<0){
210          fprintf(stderr,"Unable to parse params...");
211          exit(1);
212        }
213      }
214      else
215        if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromString(tmp->value))<0){
216          fprintf(stderr,"Unable to parse params...");
217          exit(1);
218        }
219    } 
220    else
221      if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromString(tmp->value))<0){
222        fprintf(stderr,"Unable to parse params...");
223        exit(1);
224      }
225    tmp=tmp->next;
226  }
227  return (PyDictObject*) res;
228}
229
230maps* mapsFromPyDict(PyDictObject* t){
231  maps* res=NULL;
232  maps* cursor=res;
233  PyObject* list=PyDict_Keys((PyObject*)t);
234  int nb=PyList_Size(list);
235  int i;
236  for(i=0;i<nb;i++){
237#ifdef DEBUG
238    fprintf(stderr,">> parsing maps %d\n",i);
239#endif
240    PyObject* key=PyList_GetItem(list,i);
241    PyObject* value=PyDict_GetItem((PyObject*)t,key);
242#ifdef DEBUG
243    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
244            PyString_AsString(key),PyString_AsString(value));
245#endif
246    while(cursor!=NULL){
247      cursor=cursor->next;
248    }
249    cursor=(maps*)malloc(MAPS_SIZE);
250    cursor->name=PyString_AsString(key);
251#ifdef DEBUG
252    dumpMap(mapFromPyDict((PyDictObject*)value));
253#endif
254    cursor->content=mapFromPyDict((PyDictObject*)value);
255    cursor->next=NULL;
256    if(res==NULL)
257      res=cursor;
258    else
259      addMapsToMaps(&res,cursor);
260#ifdef DEBUG
261    dumpMaps(res);
262    fprintf(stderr,">> parsed maps %d\n",i);
263#endif
264  }
265  return res;
266}
267
268map* mapFromPyDict(PyDictObject* t){
269  map* res=NULL;
270  PyObject* list=PyDict_Keys((PyObject*)t);
271  int nb=PyList_Size(list);
272  int i;
273  for(i=0;i<nb;i++){
274    PyObject* key=PyList_GetItem(list,i);
275    PyObject* value=PyDict_GetItem((PyObject*)t,key);
276#ifdef DEBUG
277    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
278            PyString_AsString(key),PyString_AsString(value));
279#endif
280    if(res!=NULL)
281      addToMap(res,PyString_AsString(key),PyString_AsString(value));
282    else
283      res=createMap(PyString_AsString(key),PyString_AsString(value));
284  }
285  return res;
286}
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