- add ePython::call return support (only int)
[vuplus_dvbapp] / lib / python / python.cpp
1 #include <lib/python/python.h>
2 #include <lib/base/eerror.h>
3 #include <Python.h>
4
5 extern "C" void init_enigma();
6
7 #if 0
8 ePyObject::ePyObject(void *ptr): m_object(ptr)
9 {
10         Py_XINCREF((PyObject*)ptr);
11 }
12
13 ePyObject::ePyObject(ePyObject &p)
14 {
15         m_object = p.m_object;
16         Py_XINCREF((PyObject*)m_object);
17 }
18
19 ePyObject::ePyObject(): m_object(0)
20 {
21 }
22
23 ePyObject::~ePyObject()
24 {
25         Py_XDECREF((PyObject*)m_object);
26 }
27
28 ePyObject &ePyObject::operator=(ePyObject &p)
29 {
30         Py_XDECREF((PyObject*)m_object);
31         m_object = p.m_object;
32         Py_XINCREF((PyObject*)m_object);
33         return *this;
34 }
35
36 ePyObject &ePyObject::operator=(void *object)
37 {
38         Py_XDECREF((PyObject*)m_object);
39         m_object = object;
40         Py_XINCREF((PyObject*)m_object);
41         return *this;
42 }
43 #endif
44
45 ePython::ePython()
46 {
47         Py_Initialize();
48         
49         init_enigma();
50 }
51
52 ePython::~ePython()
53 {
54         Py_Finalize();
55 }
56
57 int ePython::execute(const std::string &pythonfile, const std::string &funcname)
58 {
59         PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
60         
61         pName = PyString_FromString(pythonfile.c_str());
62         
63         pModule = PyImport_Import(pName);
64         Py_DECREF(pName);
65         
66         if (pModule != NULL)
67         {
68                 pDict = PyModule_GetDict(pModule);
69                 
70                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
71                 
72                 if (pFunc && PyCallable_Check(pFunc))
73                 {
74                         pArgs = PyTuple_New(0);
75                                 // implement arguments..
76                         pValue = PyObject_CallObject(pFunc, pArgs);
77                         Py_DECREF(pArgs);
78                         if (pValue != NULL)
79                         {
80                                 printf("Result of call: %ld\n", PyInt_AsLong(pValue));
81                                 Py_DECREF(pValue);
82                         } else
83                         {
84                                 Py_DECREF(pModule);
85                                 PyErr_Print();
86                                 return 1;
87                         }
88                 }
89         } else
90         {
91                 if (PyErr_Occurred())
92                         PyErr_Print();
93                 return 1;
94         }
95         return 0;
96 }
97
98 int ePython::call(PyObject *pFunc, PyObject *pArgs)
99 {
100         int res = -1;
101         PyObject *pValue;
102         if (pFunc && PyCallable_Check(pFunc))
103         {
104                 pValue = PyObject_CallObject(pFunc, pArgs);
105                 if (pValue != NULL)
106                 {
107                         res = PyInt_AsLong(pValue);
108                         Py_DECREF(pValue);
109                 } else
110                 {
111                         PyErr_Print();
112                 }
113         }
114         return res;
115 }
116
117 PyObject *ePython::resolve(const std::string &pythonfile, const std::string &funcname)
118 {
119         PyObject *pName, *pModule, *pDict, *pFunc;
120
121         pName = PyString_FromString(pythonfile.c_str());
122
123         pModule = PyImport_Import(pName);
124         Py_DECREF(pName);
125         
126         if (pModule != NULL)
127         {
128                 pDict = PyModule_GetDict(pModule);
129                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
130                 Py_XINCREF(pFunc);
131                 Py_DECREF(pModule);
132                 eDebug("resolved to %p", pFunc);
133                 return pFunc;
134         } else
135         {
136                 if (PyErr_Occurred())
137                         PyErr_Print();
138                 return 0;
139         }
140 }