store and restore pathes in channellist
[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_Initialize();
63         
64         init_enigma();
65 }
66
67 ePython::~ePython()
68 {
69         Py_Finalize();
70 }
71
72 int ePython::execute(const std::string &pythonfile, const std::string &funcname)
73 {
74         PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
75         
76         pName = PyString_FromString(pythonfile.c_str());
77         
78         pModule = PyImport_Import(pName);
79         Py_DECREF(pName);
80         
81         if (pModule != NULL)
82         {
83                 pDict = PyModule_GetDict(pModule);
84                 
85                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
86                 
87                 if (pFunc && PyCallable_Check(pFunc))
88                 {
89                         pArgs = PyTuple_New(0);
90                                 // implement arguments..
91                         pValue = PyObject_CallObject(pFunc, pArgs);
92                         Py_DECREF(pArgs);
93                         if (pValue != NULL)
94                         {
95                                 printf("Result of call: %ld\n", PyInt_AsLong(pValue));
96                                 Py_DECREF(pValue);
97                         } else
98                         {
99                                 Py_DECREF(pModule);
100                                 PyErr_Print();
101                                 return 1;
102                         }
103                 }
104         } else
105         {
106                 if (PyErr_Occurred())
107                         PyErr_Print();
108                 return 1;
109         }
110         return 0;
111 }
112
113 int ePython::call(PyObject *pFunc, PyObject *pArgs)
114 {
115         int res = -1;
116         PyObject *pValue;
117         if (pFunc && PyCallable_Check(pFunc))
118         {
119                 pValue = PyObject_CallObject(pFunc, pArgs);
120                 if (pValue != NULL)
121                 {
122                         if (PyInt_Check(pValue))
123                                 res = PyInt_AsLong(pValue);
124                         else
125                                 res = 0;
126                         Py_DECREF(pValue);
127                 } else
128                 {
129                         PyErr_Print();
130                 }
131         }
132         return res;
133 }
134
135 PyObject *ePython::resolve(const std::string &pythonfile, const std::string &funcname)
136 {
137         PyObject *pName, *pModule, *pDict, *pFunc;
138
139         pName = PyString_FromString(pythonfile.c_str());
140
141         pModule = PyImport_Import(pName);
142         Py_DECREF(pName);
143         
144         if (pModule != NULL)
145         {
146                 pDict = PyModule_GetDict(pModule);
147                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
148                 Py_XINCREF(pFunc);
149                 Py_DECREF(pModule);
150                 return pFunc;
151         } else
152         {
153                 if (PyErr_Occurred())
154                         PyErr_Print();
155                 return 0;
156         }
157 }