-
Notifications
You must be signed in to change notification settings - Fork 1
/
python.c
66 lines (58 loc) · 1.79 KB
/
python.c
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
#include "Python.h"
void python_startup() {
Py_SetProgramName("spreadlogd");
Py_Initialize();
}
void python_shutdown() {
Py_Exit(0);
}
int python_path(char *path) {
PyObject *sys_dict, *sys_path;
sys_dict = PyModule_GetDict(PyImport_Import(PyString_FromString("sys")));
sys_path = PyDict_GetItemString(sys_dict, "path");
PyList_Append(sys_path,PyString_FromString(path));
return 1;
}
int python_import(char *module) {
PyObject *pModule, *pName;
pName = PyString_FromString(module);
pModule = PyImport_Import(pName);
if(pModule == NULL) {
fprintf(stderr, "Failed to load \"%s\"\n", module);
return NULL;
}
return 1;
}
int python_log(char *func, char *sender, char *group, char *message) {
int retval;
char *ptr;
PyObject *pFunction, *pFunctionName, *pSender, *pGroup, *pMessage;
PyObject *pModuleName, *pDict, *pArgs;
pSender = PyString_FromString(sender);
pGroup = PyString_FromString(group);
pMessage = PyString_FromString(message);
ptr = strrchr(func, '.');
if(ptr == NULL) {
fprintf(stderr, "Function call must be <package>.<name>\n");
return NULL;
}
pFunctionName = PyString_FromString(ptr + 1);
pModuleName = PyString_FromStringAndSize(func, ptr - func);
pDict = PyModule_GetDict(PyImport_Import(pModuleName));
Py_DECREF(pModuleName);
if(pDict == NULL) {
return NULL;
}
pFunction = PyDict_GetItem(pDict, pFunctionName);
if(pFunction == NULL) {
fprintf(stderr, "Function not found\n");
return NULL;
}
pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, pSender);
PyTuple_SetItem(pArgs, 1, pGroup);
PyTuple_SetItem(pArgs, 2, pMessage);
PyObjectCallObject(pFunction, pArgs);
Py_DECREF(pArgs);
return 1;
}