NLAIPYSERVER::CPyExport Class Reference

#include <pylib.h>


Public Member Functions

PyObject * addModule (char *, PyMethodDef *)
 CPyExport ()
char * getCodeFromFile (const char *FileName)
 permet d'avoir un poiteur sur le contenu d'un fichier text, le pointeur est à detruir par l'utulisateur.

sint32 getDebugStat ()
 return the debug mode status

sint32 getGlobal (char *NameSpace, char *VarName, char *Format, void *VarValue)
sint32 getMembreValue (PyObject *Obj, char *AttribName, char *ResFormat, void *ResTarget)
PyObject * getRefVar (char *VarName)
bool isDebug ()
 test debug mode is on

sint32 runCode (const char *Code, char *NameSpace=NULL, sint32 ModeExec=Py_file_input, char *Format=NULL, void *ResultTarget=NULL, sint32 Error=true)
sint32 runFunction (char *ModuleName, char *MethodName, char *ResFormat, void *ResTarget, char *Format,...)
sint32 runMethod (PyObject *Obj, char *MethodName, char *ResFormat, void *ResTarget, char *Format,...)
bool setDebug ()
 Put the debug mode on.

sint32 setGlobal (char *NameSpace, char *VarName, char *Format,...)
sint32 setMembreValue (PyObject *Obj, char *AttribName, char *ResFormat,...)
bool unSetDebug ()
 Put the debug mode off.

 ~CPyExport ()

Static Public Member Functions

const char * getPathSeparator ()

Private Member Functions

sint32 convertResult (PyObject *Result, char *Format, void *Target)
PyObject * debugCode (sint32 RunMode, const char *code, PyObject *ModDict)
PyObject * debugFunction (PyObject *Func, PyObject *Arg)
const char * getNameSpace (const char *Name) const
PyObject * loadAttruibut (char *, char *)
PyObject * loadModule (char *)
void printLastError ()

Private Attributes

bool _DebugMode


Constructor & Destructor Documentation

NLAIPYSERVER::CPyExport::CPyExport  )  [inline]
 

Definition at line 57 of file pylib.h.

References _DebugMode.

00058                 {
00059                         _DebugMode = false;
00060                         //Py_Initialize();              
00061                         _DebugMode = false;                     
00062                 }

NLAIPYSERVER::CPyExport::~CPyExport  )  [inline]
 

Definition at line 218 of file pylib.h.

00219                 {
00220 
00221                 }


Member Function Documentation

PyObject * NLAIPYSERVER::CPyExport::addModule char *  ,
PyMethodDef * 
 

Definition at line 299 of file pylib.cpp.

00300         {
00301                 return Py_InitModule(ModuleName,DefCodeModule);
00302         }

sint32 NLAIPYSERVER::CPyExport::convertResult PyObject *  Result,
char *  Format,
void *  Target
[private]
 

Definition at line 63 of file pylib.cpp.

References sint32.

Referenced by getGlobal(), getMembreValue(), runCode(), runFunction(), and runMethod().

00064         {
00065                 if(Result)
00066                 {
00067                         if(!Target)//nodefaultlib:libcpmtd.lib
00068                         {
00069                                 Py_DECREF(Result);
00070                                 return 0;
00071                         }
00072                         if(!PyArg_Parse(Result,Format,Target))
00073                         {
00074                                 Py_DECREF(Result);
00075                                 return -1;
00076                         }
00077                         /*if(!strcmp(Format,"O"))
00078                         {
00079                                 Py_DECREF(Result);
00080                                 return 0;
00081                         }*/
00082                         return 0;
00083                 }
00084 
00085                 return -1;
00086         }

PyObject * NLAIPYSERVER::CPyExport::debugCode sint32  RunMode,
const char *  code,
PyObject *  ModDict
[private]
 

Definition at line 103 of file pylib.cpp.

References runFunction(), and sint32.

Referenced by runCode().

00104         {
00105                 sint32 Stat;
00106                 PyObject *Result;
00107                 const char *pdbName = (RunMode == Py_eval_input ? "runeval":"run");
00108                 
00109 
00110                 //UnSetDebug();
00111                 Stat = runFunction("pdb",(char *)pdbName,"O",&Result,"(sOO)",code,ModDict,ModDict);
00112 
00113                 //SetDebug();
00114 
00115                 return (Stat != 0) ? NULL : Result;
00116         }

PyObject * NLAIPYSERVER::CPyExport::debugFunction PyObject *  Func,
PyObject *  Arg
[private]
 

Definition at line 88 of file pylib.cpp.

Referenced by runFunction(), and runMethod().

00089         {
00090                 /*sint32 oops,res;
00091                 PyObject *Result = NULL;
00092 
00093                 oops = _PyTuple_Resize(&Arg,(1 + PyTuple_Size(Arg)));
00094                 oops |= PyTuple_SetItem(Arg,0,Func);
00095 
00096                 if(oops) return NULL;
00097 
00098                 res = runFunction("pdb","runcall","O",&Result,"O",Arg);*/
00099 
00100                 return NULL;//(res != 0) ? NULL : Result;
00101         }

char * NLAIPYSERVER::CPyExport::getCodeFromFile const char *  FileName  ) 
 

permet d'avoir un poiteur sur le contenu d'un fichier text, le pointeur est à detruir par l'utulisateur.

Definition at line 304 of file pylib.cpp.

References NLMISC::CIFile::getFileSize(), NLMISC::CIFile::serialBuffer(), sint32, uint16, and uint8.

00305         {               
00306                 uint8 *buf;
00307                 char* code;
00308                 sint32 si;
00309                 uint16 ui,uk;
00310 
00311                 NLMISC::CIFile f(FileName);
00312                 si = f.getFileSize ();
00313 //              fseek(f,0,SEEK_END);
00314 //              si = ftell(f);
00315 //              rewind(f);
00316                 buf = new uint8 [si + 1];
00317                 f.serialBuffer(buf, sizeof( uint8  )*si);
00318                 buf[si] = 0;
00319 
00320                 code = new char[si + 1];
00321                 for(ui=0,uk=0; ui < si; ui++)
00322                 {
00323                         if(buf[ui] != '\r') 
00324                         {
00325                                 code[uk] = buf[ui];
00326                                 uk ++;
00327                         }
00328                 }       
00329                 code[uk] = 0;
00330                 return code;
00331                 delete buf;
00332         }

sint32 NLAIPYSERVER::CPyExport::getDebugStat  )  [inline]
 

return the debug mode status

Definition at line 71 of file pylib.h.

References _DebugMode, and sint32.

00072                 {
00073                         return _DebugMode;
00074                 }

sint32 NLAIPYSERVER::CPyExport::getGlobal char *  NameSpace,
char *  VarName,
char *  Format,
void *  VarValue
 

Definition at line 252 of file pylib.cpp.

References convertResult(), loadAttruibut(), and sint32.

00253         {       
00254                 PyObject *Var;
00255 
00256                 Var = loadAttruibut(NameSpace,VarName);
00257                 return convertResult(Var,Format,VarValue);
00258         }

sint32 NLAIPYSERVER::CPyExport::getMembreValue PyObject *  Obj,
char *  AttribName,
char *  ResFormat,
void *  ResTarget
 

get valu of an attribut in an python object.

Parameters:
Obj: python object.
AttribName: name of attribute to eval.
ResFormat: Type of expected result.
ResTarget: The return of the result.
Sample: sint32 PyTheXOfA; sint32 PyTheYOfA; pExePythonExpression->GetMembreValue(PyObjetA,"X","i",&PyTheXOfA); pExePythonExpression->GetMembreValue(PyObjetA,"Y","i",&PyTheYOfA);

The method return 0 if Ok; -1 if fail.

Definition at line 227 of file pylib.cpp.

References convertResult(), and sint32.

00228         {
00229                 PyObject *Attrib;
00230                 
00231                 if((Attrib = PyObject_GetAttrString(Obj,AttribName)) == NULL) 
00232                 {
00233                         PyErr_Print ();
00234                         return -1;
00235                 }
00236 
00237                 return convertResult(Attrib,ResFormat,ResTarget);  
00238         }

const char* NLAIPYSERVER::CPyExport::getNameSpace const char *  Name  )  const [inline, private]
 

Definition at line 43 of file pylib.h.

Referenced by loadAttruibut(), and loadModule().

00044                 {
00045                         return Name == NULL ? "__main__" : Name;
00046                 }

const char * NLAIPYSERVER::CPyExport::getPathSeparator  )  [static]
 

Definition at line 334 of file pylib.cpp.

References s.

00335         {
00336                 static const char s[] = {DELIM,0};
00337                 return s;
00338         }

PyObject * NLAIPYSERVER::CPyExport::getRefVar char *  VarName  ) 
 

Definition at line 284 of file pylib.cpp.

References runCode(), and NLAIC::stringGetBuild().

00285         {
00286                 PyObject *Objet;
00287                 std::string text;
00288                 text = NLAIC::stringGetBuild("%s",VarName);
00289                 runCode(text.c_str(),NULL,Py_eval_input,"O",&Objet);
00290                 if(Objet)
00291                 {
00292                         Py_INCREF(Objet);
00293                         return Objet;
00294                 }
00295                 return NULL;
00296         }

bool NLAIPYSERVER::CPyExport::isDebug  )  [inline]
 

test debug mode is on

Definition at line 84 of file pylib.h.

References _DebugMode.

Referenced by runCode(), runFunction(), and runMethod().

00085                 {
00086                         return (_DebugMode == true);
00087                 }

PyObject * NLAIPYSERVER::CPyExport::loadAttruibut char *  ,
char * 
[private]
 

Definition at line 53 of file pylib.cpp.

References getNameSpace(), and loadModule().

Referenced by getGlobal(), and runFunction().

00054         {
00055                 PyObject *Module;
00056 
00057                 ModeName = (char *)getNameSpace((const char *)ModeName);
00058                 Module = loadModule(ModeName);
00059                 if(Module == NULL) return NULL;
00060                 return PyObject_GetAttrString(Module,Attribut);
00061         }

PyObject * NLAIPYSERVER::CPyExport::loadModule char *   )  [private]
 

Definition at line 34 of file pylib.cpp.

References getNameSpace().

Referenced by loadAttruibut(), runCode(), and setGlobal().

00035         {
00036                 char *Name = (char *)getNameSpace(name);
00037 
00038                 PyObject *Module;
00039 
00040                 Module = PyDict_GetItemString(PyImport_GetModuleDict(),Name);
00041 
00042 
00043                 if(strcmp(Name,"__main__") == 0) return PyImport_AddModule(Name);
00044                 else    
00045                 if(Module != NULL && PyDict_GetItemString(Module,"__dummy__")) return Module;   
00046                 else
00047                 {       
00048                         Module = PyImport_ImportModule(Name);
00049                         return Module;
00050                 }
00051         }

void NLAIPYSERVER::CPyExport::printLastError  )  [private]
 

sint32 NLAIPYSERVER::CPyExport::runCode const char *  Code,
char *  NameSpace = NULL,
sint32  ModeExec = Py_file_input,
char *  Format = NULL,
void *  ResultTarget = NULL,
sint32  Error = true
 

Run Python script on debug/release mode depend on _DebugMode status.

Parameters:
Code: Python script on text mode, warrning, there are difference between UNIX & Window, on UNIX system there are a
to mark the end of line, and Python parser don't like the syntax, use the methode GetCodeFromFile to clear all .
NameSpace: Name space od code to execute. NULL is the default Python name space that is __main__.
ModeExec: execution type file_input or eval_input.
Format: Argument format, for exempel that can bee: "O" (as Object), "i" (intger) ........ see python manual.
ResultTarget: The return of the result on eval_input mode.
Sample: pExePythonExpression->RunCode( "class C():\n" "\t def __init__(self):\n" "\t\t self.x = 5\n" "\t\t self.y = 25\n" "\t def M(self, nX, nY):\n" "\t\t return self.x*nX,self.y*nY\n"); PyObject *PyObjetA; pExePythonExpression->RunCode("A = C()",NULL,eval_input,"O",&PyObjetA);
the method return 0 if Ok; -1 if fail.

Definition at line 148 of file pylib.cpp.

References convertResult(), debugCode(), isDebug(), loadModule(), and sint32.

Referenced by getRefVar(), and NLAIPYSERVER::initPythonLib().

00149         {
00150                 PyObject *Module,*Dict,*Result;
00151                 
00152                 Module = loadModule(NameSpace);
00153                 if(Module == NULL) return -1;
00154 
00155                 Dict = PyModule_GetDict(Module);
00156 
00157                 if(Dict == NULL) return -1;
00158 
00159                 if(isDebug())
00160                 {
00161                         Result = debugCode(ModeExec,Code,Dict);                 
00162                 }
00163                 else Result = PyRun_String((char *)Code,ModeExec,Dict,Dict);
00164                 
00165                 if(Result == NULL)
00166                 {               
00167                         /*if(Error) PrintLastError();           
00168                         else PyErr_Print ();*/
00169                         PyErr_Print ();
00170                         return -1;
00171                 }
00172                 else 
00173                 {
00174                         if(ModeExec == Py_file_input)
00175                         {
00176                                 Py_XDECREF(Result);
00177                                 return 0;
00178                         }
00179                         else 
00180                         return convertResult(Result,Format,Target);
00181                 }
00182 
00183         }

sint32 NLAIPYSERVER::CPyExport::runFunction char *  ModuleName,
char *  MethodName,
char *  ResFormat,
void *  ResTarget,
char *  Format,
... 
 

Run a python function on an module.

Parameters:
ModuleName: name of the module.
MethodName: name of method to run.
ResFormat: Type of expected result.
ResTarget: The return of the result.
Format: Argument format, for exempel that can bee: "O" (as Object), "i" (intger) ........ see python manual.
The method return 0 if Ok; -1 if fail.

Definition at line 118 of file pylib.cpp.

References convertResult(), debugFunction(), isDebug(), loadAttruibut(), and sint32.

Referenced by debugCode().

00119         {       
00120 
00121                 PyObject *Func,*Arg,*Result;
00122                 va_list ArgList;
00123                 va_start(ArgList,Format);
00124                 
00125                 if((Func = loadAttruibut(ModuleName,MethodName)) == NULL ) return -1;
00126 
00127                 if((Arg = Py_VaBuildValue(Format,ArgList)) == NULL)
00128                 {
00129                         Py_DECREF(Func);
00130                         return -1;
00131                 }
00132 
00133                 if(isDebug() && strcmp(ModuleName,"pdb"))
00134                 {
00135                         Result = debugFunction(Func,Arg);
00136                 }
00137                 else
00138                 {
00139                         Result = PyEval_CallObject(Func,Arg);
00140                         if(!Result) 
00141                                                 PyErr_Print ();
00142                 }
00143                 Py_DECREF(Func);
00144                 Py_DECREF(Arg);
00145                 return convertResult(Result,ResFormat,ResTarget); 
00146         }

sint32 NLAIPYSERVER::CPyExport::runMethod PyObject *  Obj,
char *  MethodName,
char *  ResFormat,
void *  ResTarget,
char *  Format,
... 
 

Run a python method on a python class.

Parameters:
Obj: Pointer of the module that contain method, see exemple on runCode or python manual.
MethodName: Name of metod to run.
ResFormat: Type of expected result.
ResTarget: The return of the result.
Format: Argument format, for exempel that can bee: "O" (as Object), "i" (intger) ........ see python manual.
Sample: PyObject *PyObjetA,*PyObjetResult; pExePythonExpression->RunCode("A = C()",NULL,eval_input,"O",&PyObjetA); pExePythonExpression->RunMethod(PyObjetA,"M","O",PyObjetResult,"ii",5,16);
The result is a Python tupule as (25,400); The method return 0 if Ok; -1 if fail.

Definition at line 185 of file pylib.cpp.

References convertResult(), debugFunction(), isDebug(), and sint32.

00186         {       
00187                 if(Obj)
00188                 {
00189                         /*PyObject *s = PyObject_Str(Obj);
00190                         char *S = PyString_AsString(s);*/
00191                         PyObject *Method,*Arg,*Result;
00192                         va_list ArgList;
00193                         va_start(ArgList,Format);
00194 
00195                         if((Method = PyObject_GetAttrString(Obj,MethodName)) == NULL) 
00196                         {
00197                                 PyErr_Print ();
00198                                 return -1;
00199                         }
00200 
00201                         if((Arg = Py_VaBuildValue(Format,ArgList)) == NULL) 
00202                         {       
00203                                 PyErr_Print();
00204                                 Py_DECREF(Method);
00205                                 return -1;
00206                         }
00207 
00208                         if(isDebug())
00209                         {
00210                                 Result = debugFunction(Method,Arg);                     
00211                         }
00212                         else 
00213                         {
00214                                 Result = PyEval_CallObject(Method,Arg);
00215                                 if(!Result) 
00216                                                 PyErr_Print ();
00217                         }
00218                         
00219                         Py_DECREF(Method);
00220                         Py_DECREF(Arg);
00221 
00222                         return convertResult(Result,ResFormat,ResTarget); 
00223                 }
00224                 return -1;
00225         }

bool NLAIPYSERVER::CPyExport::setDebug  )  [inline]
 

Put the debug mode on.

Definition at line 65 of file pylib.h.

References _DebugMode.

00066                 {
00067                         return (_DebugMode = true); 
00068                 }

sint32 NLAIPYSERVER::CPyExport::setGlobal char *  NameSpace,
char *  VarName,
char *  Format,
... 
 

Definition at line 261 of file pylib.cpp.

References loadModule(), and sint32.

00262         {
00263                 sint32 Result;
00264                 PyObject *Module,*Var;
00265 
00266                 va_list CVals;
00267                 va_start(CVals,Format);
00268 
00269                 if( (Module = loadModule(NameSpace)) == NULL) 
00270                                                                                                         return -1;
00271 
00272                 Var = Py_VaBuildValue(Format,CVals);
00273                 va_end(CVals);
00274 
00275                 if(Var == NULL) 
00276                                 return -1;
00277 
00278                 Result = PyObject_SetAttrString(Module,VarName,Var);
00279                 Py_DECREF(Var);
00280 
00281                 return Result;
00282         }

sint32 NLAIPYSERVER::CPyExport::setMembreValue PyObject *  Obj,
char *  AttribName,
char *  ResFormat,
... 
 

Change attribut value of an Python object.

Parameters:
Obj: python object.
AttribName: name of attribute to eval.
ResTarget: The return of the result.
exemple: pExePythonExpression->SetMembreValue(PyObjetA,"X","i",20); pExePythonExpression->SetMembreValue(PyObjetA,"Y","i",2); pExePythonExpression->RunMethod(PyObjetA,"M","O",PyObjetResult,"ii",5,16);

The method return 0 if Ok; -1 if fail.

Definition at line 240 of file pylib.cpp.

References sint32.

00241         {
00242                 PyObject *Val;
00243 
00244                 va_list ArgList;
00245                 va_start(ArgList,ResFormat);
00246                 if((Val = Py_VaBuildValue(ResFormat,ArgList)) == NULL) return -1;
00247                 sint32 Res = PyObject_SetAttrString(Obj,AttribName,Val);
00248                 Py_DECREF(Val);
00249                 return Res;     
00250         }

bool NLAIPYSERVER::CPyExport::unSetDebug  )  [inline]
 

Put the debug mode off.

Definition at line 77 of file pylib.h.

References _DebugMode.

00078                 {
00079                         return (_DebugMode = false);
00080                 }


Field Documentation

bool NLAIPYSERVER::CPyExport::_DebugMode [private]
 

Definition at line 41 of file pylib.h.

Referenced by CPyExport(), getDebugStat(), isDebug(), setDebug(), and unSetDebug().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 11:11:19 2004 for NeL by doxygen 1.3.6