plugins: python renderer: Load module

This commit is contained in:
Mario Hüttel 2019-11-17 00:02:42 +01:00
parent 583e5581c0
commit 6b5101ecec

View File

@ -5,6 +5,26 @@
#include <gds-render/gds-utils/gds-types.h> #include <gds-render/gds-utils/gds-types.h>
#include <gds-render/output-renderers/external-renderer-interfaces.h> #include <gds-render/output-renderers/external-renderer-interfaces.h>
/**
* @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) int FUNC_DECL(EXTERNAL_LIBRARY_RENDER_FUNCTION)(struct gds_cell *toplevel, GList *layer_info_list, const char *output_file_name, double scale)
{ {
if (!toplevel) 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 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); printf("Init with params: %s\ngds-render version: %s\n", params, version);
Py_Initialize(); 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) int FUNC_DECL(EXTERNAL_LIBRARY_FINALIZE_FUNCTION)(void)
{ {
Py_Finalize(); int ret;
return 0;
ret = Py_FinalizeEx();
return ret;
} }