diff --git a/plugins/python-renderer/src/plugin-main.c b/plugins/python-renderer/src/plugin-main.c index 19eecbb..a522f17 100644 --- a/plugins/python-renderer/src/plugin-main.c +++ b/plugins/python-renderer/src/plugin-main.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -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; }