xref: /openbmc/libcper/pycper.c (revision cd9b1c5826323f8035a10513c832cc1a0d351247)
1*cd9b1c58SEd Tanous #if LIBCPER_PYTHON
2*cd9b1c58SEd Tanous #define PY_SSIZE_T_CLEAN
3*cd9b1c58SEd Tanous #include <Python.h>
4*cd9b1c58SEd Tanous #include <libcper/cper-parse-str.h>
5*cd9b1c58SEd Tanous #include <libcper/cper-parse.h>
6*cd9b1c58SEd Tanous 
convert_to_pydict(json_object * jso)7*cd9b1c58SEd Tanous PyObject *convert_to_pydict(json_object *jso)
8*cd9b1c58SEd Tanous {
9*cd9b1c58SEd Tanous 	PyObject *ret = Py_None;
10*cd9b1c58SEd Tanous 	enum json_type type = json_object_get_type(jso);
11*cd9b1c58SEd Tanous 	//printf("type: %d ", type);
12*cd9b1c58SEd Tanous 	switch (type) {
13*cd9b1c58SEd Tanous 	case json_type_null:
14*cd9b1c58SEd Tanous 		//printf("json_type_null\n");
15*cd9b1c58SEd Tanous 		ret = Py_None;
16*cd9b1c58SEd Tanous 		break;
17*cd9b1c58SEd Tanous 	case json_type_boolean: {
18*cd9b1c58SEd Tanous 		//printf("json_type_boolean\n");
19*cd9b1c58SEd Tanous 		int b = json_object_get_boolean(jso);
20*cd9b1c58SEd Tanous 		ret = PyBool_FromLong(b);
21*cd9b1c58SEd Tanous 	} break;
22*cd9b1c58SEd Tanous 	case json_type_double: {
23*cd9b1c58SEd Tanous 		//printf("json_type_double\n");
24*cd9b1c58SEd Tanous 		double d = json_object_get_double(jso);
25*cd9b1c58SEd Tanous 		ret = PyFloat_FromDouble(d);
26*cd9b1c58SEd Tanous 	} break;
27*cd9b1c58SEd Tanous 
28*cd9b1c58SEd Tanous 	case json_type_int: {
29*cd9b1c58SEd Tanous 		//printf("json_type_int\n");
30*cd9b1c58SEd Tanous 		int64_t i = json_object_get_int64(jso);
31*cd9b1c58SEd Tanous 		ret = PyLong_FromLong(i);
32*cd9b1c58SEd Tanous 	} break;
33*cd9b1c58SEd Tanous 	case json_type_object: {
34*cd9b1c58SEd Tanous 		//printf("json_type_object\n");
35*cd9b1c58SEd Tanous 		ret = PyDict_New();
36*cd9b1c58SEd Tanous 
37*cd9b1c58SEd Tanous 		json_object_object_foreach(jso, key1, val)
38*cd9b1c58SEd Tanous 		{
39*cd9b1c58SEd Tanous 			PyObject *pyobj = convert_to_pydict(val);
40*cd9b1c58SEd Tanous 			if (key1 != NULL) {
41*cd9b1c58SEd Tanous 				//printf("Parsing %s\n", key1);
42*cd9b1c58SEd Tanous 				if (pyobj == NULL) {
43*cd9b1c58SEd Tanous 					pyobj = Py_None;
44*cd9b1c58SEd Tanous 				}
45*cd9b1c58SEd Tanous 				PyDict_SetItemString(ret, key1, pyobj);
46*cd9b1c58SEd Tanous 			}
47*cd9b1c58SEd Tanous 		}
48*cd9b1c58SEd Tanous 	} break;
49*cd9b1c58SEd Tanous 	case json_type_array: {
50*cd9b1c58SEd Tanous 		//printf("json_type_array\n");
51*cd9b1c58SEd Tanous 		ret = PyList_New(0);
52*cd9b1c58SEd Tanous 		int arraylen = json_object_array_length(jso);
53*cd9b1c58SEd Tanous 
54*cd9b1c58SEd Tanous 		for (int i = 0; i < arraylen; i++) {
55*cd9b1c58SEd Tanous 			//printf("Parsing %d\n", i);
56*cd9b1c58SEd Tanous 			json_object *val = json_object_array_get_idx(jso, i);
57*cd9b1c58SEd Tanous 			PyObject *pyobj = convert_to_pydict(val);
58*cd9b1c58SEd Tanous 			if (pyobj == NULL) {
59*cd9b1c58SEd Tanous 				pyobj = Py_None;
60*cd9b1c58SEd Tanous 			}
61*cd9b1c58SEd Tanous 			PyList_Append(ret, pyobj);
62*cd9b1c58SEd Tanous 		}
63*cd9b1c58SEd Tanous 	} break;
64*cd9b1c58SEd Tanous 	case json_type_string: {
65*cd9b1c58SEd Tanous 		//printf("json_type_string\n");
66*cd9b1c58SEd Tanous 		const char *strval = json_object_get_string(jso);
67*cd9b1c58SEd Tanous 		ret = PyUnicode_FromString(strval);
68*cd9b1c58SEd Tanous 	} break;
69*cd9b1c58SEd Tanous 	}
70*cd9b1c58SEd Tanous 	return ret;
71*cd9b1c58SEd Tanous }
72*cd9b1c58SEd Tanous 
parse(PyObject * self,PyObject * args)73*cd9b1c58SEd Tanous static PyObject *parse(PyObject *self, PyObject *args)
74*cd9b1c58SEd Tanous {
75*cd9b1c58SEd Tanous 	(void)self;
76*cd9b1c58SEd Tanous 	PyObject *ret;
77*cd9b1c58SEd Tanous 	const unsigned char *data;
78*cd9b1c58SEd Tanous 	Py_ssize_t count;
79*cd9b1c58SEd Tanous 
80*cd9b1c58SEd Tanous 	if (!PyArg_ParseTuple(args, "y#", &data, &count)) {
81*cd9b1c58SEd Tanous 		return NULL;
82*cd9b1c58SEd Tanous 	}
83*cd9b1c58SEd Tanous 
84*cd9b1c58SEd Tanous 	char *jstrout = cperbuf_to_str_ir(data, count);
85*cd9b1c58SEd Tanous 	if (jstrout == NULL) {
86*cd9b1c58SEd Tanous 		free(jstrout);
87*cd9b1c58SEd Tanous 		return NULL;
88*cd9b1c58SEd Tanous 	}
89*cd9b1c58SEd Tanous 	json_object *jout = cper_buf_to_ir(data, count);
90*cd9b1c58SEd Tanous 	if (jout == NULL) {
91*cd9b1c58SEd Tanous 		free(jstrout);
92*cd9b1c58SEd Tanous 		free(jout);
93*cd9b1c58SEd Tanous 		return NULL;
94*cd9b1c58SEd Tanous 	}
95*cd9b1c58SEd Tanous 
96*cd9b1c58SEd Tanous 	ret = convert_to_pydict(jout);
97*cd9b1c58SEd Tanous 
98*cd9b1c58SEd Tanous 	//ret = PyUnicode_FromString(jstrout);
99*cd9b1c58SEd Tanous 	free(jout);
100*cd9b1c58SEd Tanous 	free(jstrout);
101*cd9b1c58SEd Tanous 	return ret;
102*cd9b1c58SEd Tanous }
103*cd9b1c58SEd Tanous 
104*cd9b1c58SEd Tanous static PyMethodDef methods[] = {
105*cd9b1c58SEd Tanous 	{ "parse", (PyCFunction)parse, METH_VARARGS, NULL },
106*cd9b1c58SEd Tanous 	{ NULL, NULL, 0, NULL },
107*cd9b1c58SEd Tanous };
108*cd9b1c58SEd Tanous 
109*cd9b1c58SEd Tanous static struct PyModuleDef module = {
110*cd9b1c58SEd Tanous 	PyModuleDef_HEAD_INIT,
111*cd9b1c58SEd Tanous 	"cper",
112*cd9b1c58SEd Tanous 	NULL,
113*cd9b1c58SEd Tanous 	-1,
114*cd9b1c58SEd Tanous 	methods,
115*cd9b1c58SEd Tanous 	NULL,
116*cd9b1c58SEd Tanous 	NULL,
117*cd9b1c58SEd Tanous 	NULL,
118*cd9b1c58SEd Tanous 	NULL,
119*cd9b1c58SEd Tanous };
120*cd9b1c58SEd Tanous 
PyInit_cper(void)121*cd9b1c58SEd Tanous PyMODINIT_FUNC PyInit_cper(void)
122*cd9b1c58SEd Tanous {
123*cd9b1c58SEd Tanous 	return PyModule_Create(&module);
124*cd9b1c58SEd Tanous }
125*cd9b1c58SEd Tanous #endif
126