diff --git a/plugins/python-renderer/src/plugin-main.c b/plugins/python-renderer/src/plugin-main.c index b933bc0..19eecbb 100644 --- a/plugins/python-renderer/src/plugin-main.c +++ b/plugins/python-renderer/src/plugin-main.c @@ -5,6 +5,26 @@ #include #include +/** + * @brief Global variable for loaded module. This is not very nice. + * @warning This has to be changed, if this api wants to be re-entrant. + */ +static PyObject *p_module = NULL; + +static int check_file_exists(const char *path) +{ + FILE *file; + int ret = -1; + + file = fopen(path, "rb"); + if (file) { + fclose(file); + ret = 0; + } + + return ret; +} + int FUNC_DECL(EXTERNAL_LIBRARY_RENDER_FUNCTION)(struct gds_cell *toplevel, GList *layer_info_list, const char *output_file_name, double scale) { if (!toplevel) @@ -16,14 +36,40 @@ int FUNC_DECL(EXTERNAL_LIBRARY_RENDER_FUNCTION)(struct gds_cell *toplevel, GList int FUNC_DECL(EXTERNAL_LIBRARY_INIT_FUNCTION)(const char *params, const char *version) { + int ret = 0; + PyObject *p_name; + + if (!params || !version) + return -1000; + printf("Init with params: %s\ngds-render version: %s\n", params, version); Py_Initialize(); - return 0; + /* This renderer has only one parameter: The name of the file */ + if (check_file_exists(params)) + return -1001; + + p_name = PyUnicode_DecodeFSDefault(params); + p_module = PyImport_Import(p_name); + Py_DECREF(p_name); + + if (!p_module) { + PyErr_Print(); + fprintf(stderr, "Failed to load %s\n", params); + ret = -1; + goto return_value; + } + + + +return_value: + return ret; } int FUNC_DECL(EXTERNAL_LIBRARY_FINALIZE_FUNCTION)(void) { - Py_Finalize(); - return 0; + int ret; + + ret = Py_FinalizeEx(); + return ret; }