Ignore:
Timestamp:
Nov 5, 2014, 4:58:11 PM (9 years ago)
Author:
david
Message:
  • remove metapath parameter for python and js support
  • use conf in memory for C,python and js services
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_python.c

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

Search

Context Navigation

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