finish the TimerEntry editor... should work now for editing repeated timers
[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 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                         if (PyInt_Check(pValue))
108                                 res = PyInt_AsLong(pValue);
109                         else
110                                 res = 0;
111                         Py_DECREF(pValue);
112                 } else
113                 {
114                         PyErr_Print();
115                 }
116         }
117         return res;
118 }
119
120 PyObject *ePython::resolve(const std::string &pythonfile, const std::string &funcname)
121 {
122         PyObject *pName, *pModule, *pDict, *pFunc;
123
124         pName = PyString_FromString(pythonfile.c_str());
125
126         pModule = PyImport_Import(pName);
127         Py_DECREF(pName);
128         
129         if (pModule != NULL)
130         {
131                 pDict = PyModule_GetDict(pModule);
132                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
133                 Py_XINCREF(pFunc);
134                 Py_DECREF(pModule);
135                 return pFunc;
136         } else
137         {
138                 if (PyErr_Occurred())
139                         PyErr_Print();
140                 return 0;
141         }
142 }