-
Notifications
You must be signed in to change notification settings - Fork 0
/
avp_transformer.c
140 lines (118 loc) · 3.21 KB
/
avp_transformer.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
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
#include <python3.10/Python.h>
#include "avp_transformer.h"
#include <string.h>
static const char *SCRIPT_PATH = ".";
static PyObject *callPythonFunctionFromModule(
char const *const moduleName,
char const *const functionName,
char const *const arg);
static PyObject *getPythonFunctionFromModule(
const char *const moduleName,
const char *const functionName);
static PyObject *getPythonFunctionFromModule(
const char *const moduleName,
const char *const functionName);
static PyObject *importPythonModule(const char *const moduleName);
static int updateValue(
char *const value,
unsigned int maxValSz,
PyObject *pyResult);
int AVP_TransformValue(
char const *const moduleName,
char const *const functionName,
char *const value,
unsigned int maxValSz)
{
int isFailed = 1;
if ((NULL != moduleName) &&
(NULL != functionName) &&
(NULL != value))
{
PyObject *pyResult = NULL;
setenv("PYTHONPATH", SCRIPT_PATH, 1);
Py_Initialize();
if ((pyResult = callPythonFunctionFromModule(moduleName, functionName, value)))
{
isFailed = updateValue(value, maxValSz, pyResult);
Py_CLEAR(pyResult);
}
if (0 != Py_FinalizeEx())
{
printf("Failed to finalise cpython\n");
}
}
return isFailed;
}
static PyObject *callPythonFunctionFromModule(
char const *const moduleName,
char const *const functionName,
char const *const arg)
{
char *result = NULL;
PyObject *pyFunction = NULL;
PyObject *pyResult = NULL;
if ((pyFunction = getPythonFunctionFromModule(moduleName, functionName)))
{
pyResult = PyObject_CallFunction(pyFunction, "s", arg);
Py_CLEAR(pyFunction);
}
return pyResult;
}
static PyObject *getPythonFunctionFromModule(
const char *const moduleName,
const char *const functionName)
{
PyObject *pyFunction = NULL;
PyObject *pyModule = NULL;
if ((pyModule = importPythonModule(moduleName)))
{
pyFunction = PyObject_GetAttrString(pyModule, functionName);
Py_CLEAR(pyModule);
}
return pyFunction;
}
static PyObject *importPythonModule(const char *const moduleName)
{
PyObject *pyModule = NULL;
PyObject *pyModuleName = NULL;
if ((pyModuleName = PyUnicode_FromString(moduleName)))
{
pyModule = PyImport_Import(pyModuleName);
Py_CLEAR(pyModuleName);
}
return pyModule;
}
static int setValue(
char *const value,
unsigned int maxValSz,
char const *const newVal)
{
int isFailed = 1;
size_t newValSz = strlen(newVal);
if (newValSz < maxValSz)
{
strcpy(value, newVal);
isFailed = 0;
}
else
{
printf("Transform returned a string larger than what we can hold\n");
}
return isFailed;
}
static int updateValue(
char *const value,
unsigned int maxValSz,
PyObject *pyResult)
{
int isFailed = 1;
char *newVal = NULL;
PyObject *pyBytes = NULL;
if ((pyBytes = PyUnicode_AsUTF8String(pyResult)))
{
newVal = PyBytes_AsString(pyBytes);
isFailed = setValue(value, maxValSz, newVal);
Py_CLEAR(pyBytes);
}
return isFailed;
}