update python
[enigma2.git] / 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 void ePython::call(PyObject *pFunc, PyObject *pArgs)
99 {
100         PyObject *pValue;
101         if (pFunc && PyCallable_Check(pFunc))
102         {
103                 pValue = PyObject_CallObject(pFunc, pArgs);
104                 if (pValue != NULL)
105                 {
106 //                      printf("Result of call: %ld\n", PyInt_AsLong(pValue));
107                         Py_DECREF(pValue);
108                 } else
109                 {
110                         PyErr_Print();
111                 }
112         }
113 }
114
115 PyObject *ePython::resolve(const std::string &pythonfile, const std::string &funcname)
116 {
117         PyObject *pName, *pModule, *pDict, *pFunc;
118
119         pName = PyString_FromString(pythonfile.c_str());
120
121         pModule = PyImport_Import(pName);
122         Py_DECREF(pName);
123         
124         if (pModule != NULL)
125         {
126                 pDict = PyModule_GetDict(pModule);
127                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
128                 Py_XINCREF(pFunc);
129                 Py_DECREF(pModule);
130                 eDebug("resolved to %p", pFunc);
131                 return pFunc;
132         } else
133         {
134                 if (PyErr_Occurred())
135                         PyErr_Print();
136                 return 0;
137         }
138 }