plugins: python renderer: Implement first draft that loads a python module and calls a test function. This will be changed later

This commit is contained in:
Mario Hüttel 2019-11-17 00:53:24 +01:00
parent 6b5101ecec
commit 112a5ac74c

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <glib.h>
#include <unistd.h>
#include <Python.h>
#include <gds-render/gds-utils/gds-types.h>
@ -27,17 +28,45 @@ static int check_file_exists(const char *path)
int FUNC_DECL(EXTERNAL_LIBRARY_RENDER_FUNCTION)(struct gds_cell *toplevel, GList *layer_info_list, const char *output_file_name, double scale)
{
int ret = 0;
PyObject *p_render_func;
PyObject *p_return_value;
if (!toplevel)
return -1000;
if (!p_module)
return -2000;
printf("Rendering %s\n", toplevel->name);
return 0;
p_render_func = PyObject_GetAttrString(p_module, "test_func");
if (!p_render_func && !PyCallable_Check(p_render_func)) {
if (PyErr_Occurred())
PyErr_Print();
else
fprintf(stderr, "function not found in python module\n");
ret = -1;
goto return_value;
}
p_return_value = PyObject_CallObject(p_render_func, NULL);
if (p_return_value) {
printf("Result of call: %ld\n", PyLong_AsLong(p_return_value));
Py_DECREF(p_return_value);
}
return_value:
return ret;
}
int FUNC_DECL(EXTERNAL_LIBRARY_INIT_FUNCTION)(const char *params, const char *version)
{
int ret = 0;
PyObject *p_name;
PyObject *p_sys_path;
PyObject *p_cwd_string;
char cwd[256];
if (!params || !version)
return -1000;
@ -45,9 +74,19 @@ int FUNC_DECL(EXTERNAL_LIBRARY_INIT_FUNCTION)(const char *params, const char *ve
printf("Init with params: %s\ngds-render version: %s\n", params, version);
Py_Initialize();
if (!getcwd(cwd, sizeof(cwd))) {
return -2;
}
p_sys_path = PySys_GetObject("path");
p_cwd_string = PyUnicode_FromString(cwd);
PyList_Append(p_sys_path, p_cwd_string);
Py_DECREF(p_cwd_string);
/* This renderer has only one parameter: The name of the file */
if (check_file_exists(params))
/*if (check_file_exists(params))
return -1001;
*/
p_name = PyUnicode_DecodeFSDefault(params);
p_module = PyImport_Import(p_name);
@ -60,8 +99,6 @@ int FUNC_DECL(EXTERNAL_LIBRARY_INIT_FUNCTION)(const char *params, const char *ve
goto return_value;
}
return_value:
return ret;
}
@ -70,6 +107,10 @@ int FUNC_DECL(EXTERNAL_LIBRARY_FINALIZE_FUNCTION)(void)
{
int ret;
printf("Finalizing\n");
if (p_module)
Py_DECREF(p_module);
ret = Py_FinalizeEx();
return ret;
}