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