1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/*
* python.c
*
* See the README file for copyright information and how to reach the author.
*
*/
#include "python.h"
cDbTable* Python::globalEventsDb = 0;
int Python::usages = 0;
int Python::globalNamingMode = 0;
const char* Python::globalTmplExpression = "";
//***************************************************************************
// Static Interface Methods (Table events)
//***************************************************************************
PyObject* Python::eventTitle(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("TITLE"));
}
PyObject* Python::eventShortText(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("SHORTTEXT"));
}
PyObject* Python::eventStartTime(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("i", 0);
return Py_BuildValue("i", globalEventsDb->getIntValue("STARTTIME"));
}
PyObject* Python::eventYear(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("YEAR"));
}
PyObject* Python::eventCategory(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("CATEGORY"));
}
PyObject* Python::episodeName(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("EPISODENAME"));
}
PyObject* Python::episodeShortName(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("EPISODESHORTNAME"));
}
PyObject* Python::episodePartName(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("EPISODEPARTNAME"));
}
PyObject* Python::extracol1(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("EPISODEEXTRACOL1"));
}
PyObject* Python::extracol2(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("EPISODEEXTRACOL2"));
}
PyObject* Python::extracol3(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("s", "");
return Py_BuildValue("s", globalEventsDb->getStrValue("EPISODEEXTRACOL3"));
}
PyObject* Python::episodeSeason(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("i", na);
return Py_BuildValue("i", globalEventsDb->getIntValue("EPISODESEASON"));
}
PyObject* Python::episodePart(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("i", na);
return Py_BuildValue("i", globalEventsDb->getIntValue("EPISODEPART"));
}
PyObject* Python::episodeNumber(PyObject* /*self*/, PyObject* /*args*/)
{
if (!globalEventsDb)
return Py_BuildValue("i", na);
return Py_BuildValue("i", globalEventsDb->getIntValue("EPISODENUMBER"));
}
PyObject* Python::namingMode(PyObject* /*self*/, PyObject* /*args*/)
{
return Py_BuildValue("i", globalNamingMode);
}
PyObject* Python::tmplExpression(PyObject* /*self*/, PyObject* /*args*/)
{
return Py_BuildValue("s", globalTmplExpression);
}
//***************************************************************************
// The Methods
//***************************************************************************
PyMethodDef Python::eventMethods[] =
{
{ "title", Python::eventTitle, METH_VARARGS, "Return the events ..." },
{ "shorttext", Python::eventShortText, METH_VARARGS, "Return the events ..." },
{ "starttime", Python::eventStartTime, METH_VARARGS, "Return the events ..." },
{ "year", Python::eventYear, METH_VARARGS, "Return the events ..." },
{ "category", Python::eventCategory, METH_VARARGS, "Return the events ..." },
{ "episodname", Python::episodeName, METH_VARARGS, "Return the events ..." },
{ "shortname", Python::episodeShortName, METH_VARARGS, "Return the events ..." },
{ "partname", Python::episodePartName, METH_VARARGS, "Return the events ..." },
{ "season", Python::episodeSeason, METH_VARARGS, "Return the events ..." },
{ "part", Python::episodePart, METH_VARARGS, "Return the events ..." },
{ "number", Python::episodeNumber, METH_VARARGS, "Return the events ..." },
{ "extracol1", Python::extracol1, METH_VARARGS, "Return the events ..." },
{ "extracol2", Python::extracol2, METH_VARARGS, "Return the events ..." },
{ "extracol3", Python::extracol3, METH_VARARGS, "Return the events ..." },
{ "namingmode", Python::namingMode, METH_VARARGS, "Return the ..." },
{ "tmplExpression", Python::tmplExpression, METH_VARARGS, "Return the ..." },
{ 0, 0, 0, 0 }
};
#if PY_MAJOR_VERSION >= 3
PyModuleDef Python::moduledef =
{
PyModuleDef_HEAD_INIT, // m_base
"event", // m_name - name of module
0, // m_doc - module documentation, may be NULL
-1, // m_size - size of per-interpreter state of the module, or -1 if the module keeps state in global variables.
eventMethods, // m_methods
0, // m_slots - array of slot definitions for multi-phase initialization, terminated by a {0, NULL} entry.
// when using single-phase initialization, m_slots must be NULL.
0, // traverseproc m_traverse - traversal function to call during GC traversal of the module object, or NULL if not needed.
0, // inquiry m_clear - clear function to call during GC clearing of the module object, or NULL if not needed.
0 // freefunc m_free - function to call during deallocation of the module object or NULL if not needed.
};
#endif
//***************************************************************************
// Object
//***************************************************************************
Python::Python(const char* aFile, const char* aFunction)
{
pFunc = 0;
pModule = 0;
result = 0;
file = strdup(aFile);
function = strdup(aFunction);
}
Python::~Python()
{
exit();
free(result);
free(file);
free(function);
}
//***************************************************************************
// Init / Exit
//***************************************************************************
int Python::init(const char* modulePath)
{
PyObject* pName;
tell(0, "Initialize python script '%s/%s.py'", modulePath, file);
// register event methods
#if PY_MAJOR_VERSION >= 3
PyImport_AppendInittab("event", &PyInitEvent);
if (!usages) Py_Initialize(); // initialize the Python interpreter
pName = PyUnicode_FromString(file);
#else
if (!usages) Py_Initialize(); // initialize the Python interpreter
Py_InitModule("event", eventMethods);
pName = PyString_FromString(file);
#endif
usages++;
// add search path for Python modules
if (modulePath)
{
char* p;
asprintf(&p, "sys.path.append(\"%s\")", modulePath);
PyRun_SimpleString("import sys");
PyRun_SimpleString(p);
free(p);
}
// import the module
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (!pModule)
{
showError();
tell(0, "Failed to load '%s.py'", file);
return fail;
}
pFunc = PyObject_GetAttrString(pModule, function);
// pFunc is a new reference
if (!pFunc || !PyCallable_Check(pFunc))
{
if (PyErr_Occurred())
showError();
tell(0, "Cannot find function '%s'", function);
return fail;
}
return success;
}
int Python::exit()
{
usages--;
if (pFunc)
Py_XDECREF(pFunc);
if (pModule)
Py_DECREF(pModule);
if (!usages)
Py_Finalize();
return success;
}
//***************************************************************************
// Execute
//***************************************************************************
int Python::execute(cDbTable* eventsDb, int namingmode, const char* tmplExpression)
{
PyObject* pValue;
free(result);
result = 0;
globalEventsDb = eventsDb;
globalNamingMode = namingmode;
globalTmplExpression = tmplExpression;
pValue = PyObject_CallObject(pFunc, 0);
if (!pValue)
{
showError();
tell(0, "Python: Call of function '%s()' failed", function);
return fail;
}
#if PY_MAJOR_VERSION >= 3
// PyObject* strExc = PyObject_Repr(pValue);
// PyObject* pyStr = PyUnicode_AsEncodedString(strExc, "utf-8", "replace");
PyObject* pyStr = PyUnicode_AsEncodedString(pValue, "utf-8", "replace");
result = strdup(PyBytes_AsString(pyStr));
// Py_XDECREF(strExc);
#else
result = strdup(PyString_AsString(pValue));
#endif
tell(3, "Result of call: %s", result);
Py_DECREF(pValue);
return success;
}
//***************************************************************************
// Dup Py String
//***************************************************************************
char* dupPyString(PyObject* pyObj)
{
char* s;
PyObject* pyString;
if (!pyObj || !(pyString=PyObject_Str(pyObj)))
{
s = strdup("unknown error");
return s;
}
#if PY_MAJOR_VERSION >= 3
PyObject* pyUni = PyObject_Repr(pyString); // Now a unicode object
PyObject* pyStr = PyUnicode_AsEncodedString(pyUni, "utf-8", "Error ~");
s = strdup(PyBytes_AsString(pyStr));
Py_XDECREF(pyUni);
Py_XDECREF(pyStr);
#else
s = strdup(PyString_AsString(pyString));
#endif
Py_DECREF(pyString);
return s;
}
//***************************************************************************
// PY String From String
//***************************************************************************
PyObject* pyStringFromString(const char* s)
{
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromString(s);
#else
return PyString_FromString(s);
#endif
}
//***************************************************************************
// Show Error
//***************************************************************************
void Python::showError()
{
char* error;
PyObject *ptype = 0, *pError = 0, *ptraceback = 0;
PyObject *moduleName, *pythModule;
PyErr_Fetch(&ptype, &pError, &ptraceback);
error = dupPyString(pError);
moduleName = pyStringFromString("traceback");
tell(0, "Python error was '%s'", error);
pythModule = PyImport_Import(moduleName);
Py_DECREF(moduleName);
// traceback
if (pythModule)
{
PyObject* pyFunc = PyObject_GetAttrString(pythModule, "format_exception");
if (pyFunc && PyCallable_Check(pyFunc))
{
PyObject *pythVal, *pystr;
char* s;
pythVal = PyObject_CallFunctionObjArgs(pyFunc, ptype, pError, ptraceback, 0);
if (pythVal)
{
s = dupPyString(pystr = PyObject_Str(pythVal));
tell(0, " %s", s);
free(s);
Py_DECREF(pystr);
Py_DECREF(pythVal);
}
}
}
free(error);
Py_DECREF(pError);
Py_DECREF(ptype);
Py_XDECREF(ptraceback);
}
|