From a8288ede3081f5dcad39f9c0fc71cf849045a6ec Mon Sep 17 00:00:00 2001 From: Johnny Jacob Date: Wed, 11 Jun 2008 16:25:48 +0000 Subject: EPlugin : Initial commit for python support for eplugins. svn path=/trunk/; revision=35626 --- plugins/python/ChangeLog | 5 + plugins/python/Makefile.am | 21 +++ .../python/org-gnome-evolution-python.eplug.xml | 10 ++ plugins/python/python-plugin-loader.c | 191 +++++++++++++++++++++ plugins/python/python-plugin-loader.h | 47 +++++ 5 files changed, 274 insertions(+) create mode 100644 plugins/python/ChangeLog create mode 100644 plugins/python/Makefile.am create mode 100644 plugins/python/org-gnome-evolution-python.eplug.xml create mode 100644 plugins/python/python-plugin-loader.c create mode 100644 plugins/python/python-plugin-loader.h (limited to 'plugins/python') diff --git a/plugins/python/ChangeLog b/plugins/python/ChangeLog new file mode 100644 index 0000000000..393e1034b5 --- /dev/null +++ b/plugins/python/ChangeLog @@ -0,0 +1,5 @@ +2008-06-09 Johnny Jacob + + * python-plugin-loader.c: Python plugin loader. + Initial Commit. + diff --git a/plugins/python/Makefile.am b/plugins/python/Makefile.am new file mode 100644 index 0000000000..854c7915f6 --- /dev/null +++ b/plugins/python/Makefile.am @@ -0,0 +1,21 @@ +INCLUDES = \ + -I$(top_srcdir) \ + $(E_UTIL_CFLAGS) \ + $(PY_INCLUDES) + +@EVO_PLUGIN_RULE@ + +plugin_DATA = org-gnome-evolution-python.eplug +plugin_LTLIBRARIES = liborg-gnome-evolution-python.la + +liborg_gnome_evolution_python_la_SOURCES = python-plugin-loader.c python-plugin-loader.h +liborg_gnome_evolution_python_la_LDFLAGS = -module -avoid-version +liborg_gnome_evolution_python_la_LIBADD = \ + -lpthread -ldl -lutil -lm \ + $(PY_LIBS) \ + $(E_UTIL_LIBS) + +EXTRA_DIST = org-gnome-evolution-python.eplug.xml + +BUILT_SOURCES = $(plugin_DATA) +CLEANFILES = $(BUILT_SOURCES) diff --git a/plugins/python/org-gnome-evolution-python.eplug.xml b/plugins/python/org-gnome-evolution-python.eplug.xml new file mode 100644 index 0000000000..c8cfd20f27 --- /dev/null +++ b/plugins/python/org-gnome-evolution-python.eplug.xml @@ -0,0 +1,10 @@ + + + + + <_description>A plugin which loads other plugins written using python. + + + + diff --git a/plugins/python/python-plugin-loader.c b/plugins/python/python-plugin-loader.c new file mode 100644 index 0000000000..2d09aba72f --- /dev/null +++ b/plugins/python/python-plugin-loader.c @@ -0,0 +1,191 @@ + /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Author: Johnny Jacob + * + * Copyright 2008 Novell, Inc. (www.novell.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include +#include +#include + +#include "python-plugin-loader.h" + +#define d(x) + +static void *epp_parent_class; + +typedef struct _EPluginPythonPrivate { + PyObject *pModule; + PyObject *pClass; + PyObject *pFunc; + PyObject *pDict; + GHashTable *methods; +} EPluginPythonPrivate; + +#define epp ((EPluginPython *)ep) + +void * load_plugin_type_register_function (void *a, void *b); + +static char * +get_xml_prop(xmlNodePtr node, const char *id) +{ + char *p = xmlGetProp(node, id); + char *out = NULL; + + if (p) { + out = g_strdup(p); + xmlFree(p); + } + + return out; +} + +static void * +epp_invoke(EPlugin *ep, const char *name, void *data) +{ + EPluginPythonPrivate *p = epp->priv; + PyObject *pModuleName, *pFunc; + PyObject *pInstance, *pValue = NULL; + + /* we need to do this every time since we may be called from any thread for some uses */ + Py_Initialize(); + + if (p->pModule == NULL) { + pModuleName = PyString_FromString(epp->module_name); + + PyRun_SimpleString(g_strdup_printf ("import sys ; sys.path.insert(0, '%s')", epp->location)); + + p->pModule = PyImport_Import(pModuleName); + + Py_DECREF(pModuleName); //Free + + if (p->pModule == NULL) { + PyErr_Print(); + g_warning("can't load python module '%s'", epp->location); + return NULL; + } + + p->pDict = PyModule_GetDict(p->pModule); + + if (epp->pClass) { + p->pClass = PyDict_GetItemString(p->pDict, epp->pClass); + } + } + + if (p->pClass) { + + if (PyCallable_Check(p->pClass)) + pInstance = PyObject_CallObject(p->pClass, NULL); + + pValue = PyObject_CallMethod(pInstance, name, NULL); + + } else { + + pFunc = PyDict_GetItemString(p->pDict, name); + + if (pFunc && PyCallable_Check(pFunc)) + pValue = PyObject_CallObject(pFunc, NULL); + else + PyErr_Print(); + } + + if (pValue) { + d(printf("%s(%d):%s: Result of call: %ld \n", __FILE__, __LINE__, __PRETTY_FUNCTION__, PyInt_AsLong(pValue))); + Py_DECREF(pValue); + /* Fixme */ + return NULL; + } else + return NULL; +} + +static int +epp_construct(EPlugin *ep, xmlNodePtr root) +{ + if (((EPluginClass *)epp_parent_class)->construct(ep, root) == -1) + return -1; + + epp->location = get_xml_prop(root, "location"); + epp->module_name = get_xml_prop (root, "module_name"); + epp->pClass = get_xml_prop(root, "pClass"); + + if (epp->location == NULL) + return -1; + + return 0; +} + +static void +epp_finalise(GObject *o) +{ + EPlugin *ep = (EPlugin *)o; + EPluginPythonPrivate *p = epp->priv; + + g_free(epp->location); + g_free(epp->module_name); + g_free(epp->pClass); + + g_hash_table_destroy(p->methods); + + g_free(epp->priv); + + ((GObjectClass *)epp_parent_class)->finalize(o); +} + +static void +epp_class_init(EPluginClass *klass) +{ + ((GObjectClass *)klass)->finalize = epp_finalise; + klass->construct = epp_construct; + klass->invoke = epp_invoke; + klass->type = "python"; +} + +static void +epp_init(GObject *o) +{ + EPlugin *ep = (EPlugin *)o; + + epp->priv = g_malloc0(sizeof(*epp->priv)); + epp->priv->methods = g_hash_table_new_full( + g_str_hash, g_str_equal, + (GDestroyNotify) g_free, + (GDestroyNotify) NULL); +} + +void * +load_plugin_type_register_function (void *a, void *b) +{ + static GType type = 0; + + if (!type) { + static const GTypeInfo info = { + sizeof(EPluginPythonClass), NULL, NULL, (GClassInitFunc) epp_class_init, NULL, NULL, + sizeof(EPluginPython), 0, (GInstanceInitFunc) epp_init, + }; + + epp_parent_class = g_type_class_ref(e_plugin_get_type()); + type = g_type_register_static(e_plugin_get_type(), "EPluginPython", &info, 0); + e_plugin_register_type (type); + + d(printf("\nType EPluginPython registered from the python-plugin-loader\n")); + + Py_Initialize(); //TODO : Does this mean i can cache the instance of pyobjects ? + } + + return GUINT_TO_POINTER(type); +} diff --git a/plugins/python/python-plugin-loader.h b/plugins/python/python-plugin-loader.h new file mode 100644 index 0000000000..a7c9ff1f07 --- /dev/null +++ b/plugins/python/python-plugin-loader.h @@ -0,0 +1,47 @@ + /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Author: Johnny Jacob + * + * Copyright 2008 Novell, Inc. (www.novell.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef _ORG_GNOME_EVOLUTION_PYTHON_H +#define _ORG_GNOME_EVOLUTION_PYTHON_H + +#include "e-util/e-plugin.h" + +typedef struct _EPluginPython EPluginPython; +typedef struct _EPluginPythonClass EPluginPythonClass; + +struct _EPluginPython { + EPlugin plugin; + + struct _EPluginPythonPrivate *priv; + + char *location; /* location */ + char *pClass; /* handler class */ + char *module_name; +}; + +struct _EPluginPythonClass { + EPluginClass plugin_class; +}; + +void *org_gnome_evolution_python_get_type(void *a, void *b); + + +#endif /* ! _ORG_GNOME_EVOLUTION_PYTHON_H */ -- cgit v1.2.3