From 2ac4a183d0f5c8d579d69ded25ece833d5678808 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 17 Aug 2013 22:51:25 +0100 Subject: [PATCH] Added python_plugin_create() --- src/plugins/plugins.c | 18 +++--------------- src/plugins/python_plugins.c | 23 +++++++++++++++++++++++ src/plugins/python_plugins.h | 2 ++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index ab87e118..573f10ee 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -35,7 +35,6 @@ void plugins_init(void) { plugins = NULL; - PyObject *p_module; // initialse python and path Py_Initialize(); @@ -57,22 +56,11 @@ plugins_init(void) { gchar *filename = plugins_load[i]; if (g_str_has_suffix(filename, ".py")) { - gchar *module_name = g_strndup(filename, strlen(filename) - 3); - p_module = PyImport_ImportModule(module_name); - python_check_error(); - if (p_module != NULL) { - cons_show("Loaded plugin: %s", module_name); - ProfPlugin *plugin = malloc(sizeof(ProfPlugin)); - plugin->name = module_name; - plugin->lang = PYTHON; - plugin->module = p_module; - plugin->init_func = python_init_hook; - plugin->on_start_func = python_on_start_hook; - plugin->on_connect_func = python_on_connect_hook; - plugin->on_message_func = python_on_message_hook; + ProfPlugin *plugin = python_plugin_create(filename); + if (plugin != NULL) { plugins = g_slist_append(plugins, plugin); + cons_show("Loaded plugin: %s", filename); } - g_free(module_name); } } diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index f285776b..7750724d 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -29,6 +29,29 @@ #include "plugins/python_plugins.h" #include "ui/ui.h" +ProfPlugin * +python_plugin_create(const char * const filename) +{ + gchar *module_name = g_strndup(filename, strlen(filename) - 3); + PyObject *p_module = PyImport_ImportModule(module_name); + python_check_error(); + if (p_module != NULL) { + ProfPlugin *plugin = malloc(sizeof(ProfPlugin)); + plugin->name = module_name; + plugin->lang = PYTHON; + plugin->module = p_module; + plugin->init_func = python_init_hook; + plugin->on_start_func = python_on_start_hook; + plugin->on_connect_func = python_on_connect_hook; + plugin->on_message_func = python_on_message_hook; + g_free(module_name); + return plugin; + } else { + g_free(module_name); + return NULL; + } +} + void python_init_hook(ProfPlugin *plugin, const char * const version, const char * const status) { diff --git a/src/plugins/python_plugins.h b/src/plugins/python_plugins.h index 705d439c..62de4461 100644 --- a/src/plugins/python_plugins.h +++ b/src/plugins/python_plugins.h @@ -25,6 +25,8 @@ #include "plugins/plugins.h" +ProfPlugin* python_plugin_create(const char * const filename); + void python_init_hook(ProfPlugin *plugin, const char * const version, const char * const status); void python_on_start_hook(ProfPlugin *plugin); void python_on_connect_hook(ProfPlugin *plugin);