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

Last change on this file since 451 was 451, checked in by djay, 10 years ago

Small fixes for building on windows platform.

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