[anjuta] python loader plugin



commit 81092cb18adf5951d1f3a9981aab4ae44f72d0ad
Author: Abderrahim Kitouni <a kitouni gmail com>
Date:   Tue Jun 8 22:11:33 2010 +0100

    python loader plugin

 configure.in                                       |   27 +++
 plugins/Makefile.am                                |    1 +
 plugins/python-loader/Makefile.am                  |   55 +++++++
 .../python-loader/anjuta-python-loader.plugin.in   |    9 +
 plugins/python-loader/plugin.c                     |  168 ++++++++++++++++++++
 plugins/python-loader/plugin.h                     |   46 ++++++
 6 files changed, 306 insertions(+), 0 deletions(-)
---
diff --git a/configure.in b/configure.in
index 6708310..73457da 100644
--- a/configure.in
+++ b/configure.in
@@ -276,6 +276,32 @@ fi
 
 AM_CONDITIONAL(ENABLE_VALA, [test x$enable_vala = xyes])
 
+<<<<<<< HEAD
+=======
+dnl Check for Python
+dnl -------------------------------------------------------------
+
+AM_PATH_PYTHON([],
+	[have_python="yes"],
+	[have_python="no"])
+
+if test x$have_python = xyes; then
+	if test -x $PYTHON-config; then
+		PYTHON_CFLAGS=`$PYTHON-config --cflags`
+		PYTHON_LIBS=`$PYTHON-config --libs`
+		AC_SUBST(PYTHON_CFLAGS)
+		AC_SUBST(PYTHON_LIBS)
+	else
+		have_python="no"
+	fi
+fi
+
+AM_CONDITIONAL(HAVE_PYTHON, [test x$have_python = xyes])
+
+PKG_CHECK_MODULES(PLUGIN_SYMBOL_DB,
+		[libgda-4.0 >= $GDA_REQUIRED])
+
+>>>>>>> python loader plugin
 dnl Setup Anjuta Library flags
 dnl --------------------------
 LIBANJUTA_CFLAGS='$(ANJUTA_CFLAGS) $(DEPRECATED_FLAGS) -I$(top_srcdir) -I$(top_builddir)/libanjuta -DPACKAGE_PIXMAPS_DIR=\""$(datadir)/pixmaps/$(PACKAGE)"\" -DPACKAGE_LIB_DIR=\""$(pkglibdir)"\" -DPACKAGE_DATA_DIR=\""$(datadir)/$(PACKAGE)"\"'
@@ -884,6 +910,7 @@ plugins/starter/Makefile
 plugins/language-support-js/Makefile
 plugins/language-support-vala/Makefile
 plugins/language-support-python/Makefile
+plugins/python-loader/Makefile
 anjuta.desktop.in
 manuals/Makefile
 manuals/reference/Makefile
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 9ef5bd3..2a488f7 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -38,5 +38,6 @@ SUBDIRS = . \
 	language-support-js \
 	language-support-vala \
 	language-support-python
+	python-loader
 
 -include $(top_srcdir)/git.mk
diff --git a/plugins/python-loader/Makefile.am b/plugins/python-loader/Makefile.am
new file mode 100644
index 0000000..0af1d21
--- /dev/null
+++ b/plugins/python-loader/Makefile.am
@@ -0,0 +1,55 @@
+if HAVE_PYTHON
+
+# Where to install the plugin
+plugindir = $(anjuta_plugin_dir)
+
+# Plugin description file
+plugin_in_files = anjuta-python-loader.plugin.in
+%.plugin: %.plugin.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*po) ; $(INTLTOOL_MERGE) $(top_srcdir)/po $< $@ -d -u -c $(top_builddir)/po/.intltool-merge-cache
+
+plugin_DATA = $(plugin_in_files:.plugin.in=.plugin)
+
+# NOTE :
+# The naming convention is very intentional
+# We are forced to use the prefix 'lib' by automake and libtool
+#    There is probably a way to avoid it but it is not worth to effort
+#    to find out.
+# The 'anjuta_' prfix is a safety measure to avoid conflicts where the
+#    plugin 'libpython.so' needs to link with the real 'libpython.so'
+
+# Include paths
+AM_CPPFLAGS = \
+	$(WARN_CFLAGS) \
+	-DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\" \
+	-DANJUTA_DATA_DIR=\"$(anjuta_data_dir)\" \
+	-DANJUTA_PLUGIN_DIR=\"$(anjuta_plugin_dir)\" \
+	-DANJUTA_IMAGE_DIR=\"$(anjuta_image_dir)\" \
+	-DANJUTA_GLADE_DIR=\"$(anjuta_glade_dir)\" \
+	-DANJUTA_UI_DIR=\"$(anjuta_ui_dir)\" \
+	$(LIBANJUTA_CFLAGS) \
+	$(PYTHON_CFLAGS) \
+	$(PYGOBJECT_CFLAGS) \
+	-DPACKAGE_SRC_DIR=\"$(srcdir)\" \
+	-DG_LOG_DOMAIN=\"python-loader\"
+
+# The plugin
+plugin_LTLIBRARIES = libanjuta-python-loader.la
+
+# Plugin sources
+libanjuta_python_loader_la_SOURCES = plugin.c plugin.h
+
+libanjuta_python_loader_la_LDFLAGS = $(ANJUTA_PLUGIN_LDFLAGS)
+
+# Plugin dependencies
+libanjuta_python_loader_la_LIBADD = \
+	$(LIBANJUTA_LIBS) \
+	$(PYTHON_LIBS) \
+	$(PYGOBJECT_LIBS)
+
+endif # HAVE_PYTHON
+
+EXTRA_DIST = \
+	$(plugin_in_files)
+
+-include $(top_srcdir)/git.mk
+
diff --git a/plugins/python-loader/anjuta-python-loader.plugin.in b/plugins/python-loader/anjuta-python-loader.plugin.in
new file mode 100644
index 0000000..4cb3e93
--- /dev/null
+++ b/plugins/python-loader/anjuta-python-loader.plugin.in
@@ -0,0 +1,9 @@
+[Anjuta Plugin]
+Name=Python loader
+Description=Allows loading Anjuta plugins written in Python
+Location=anjuta-pyloader:PythonLoaderPlugin
+#Icon=anjuta-python-loader-plugin.png
+Interfaces=IAnjutaPluginLoader
+
+[Plugin Loader]
+SupportedLanguage=Python
diff --git a/plugins/python-loader/plugin.c b/plugins/python-loader/plugin.c
new file mode 100644
index 0000000..8992b7b
--- /dev/null
+++ b/plugins/python-loader/plugin.c
@@ -0,0 +1,168 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
+/**
+ *  anjuta-pyloader : a Python plugin loader for Anjuta.
+ *
+ *  Copyright (C) 2007 Sebastien Granjoux
+ *  Copyright (C) 2009 Abderrahim Kitouni
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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 <Python.h>
+
+#include <libanjuta/anjuta-debug.h>
+#include <libanjuta/interfaces/libanjuta-interfaces.h>
+#include "plugin.h"
+
+
+/* IAnjutaPluginFactory interface
+ *---------------------------------------------------------------------------*/
+
+static AnjutaPlugin*
+pyl_ianjuta_plugin_factory_new_plugin (IAnjutaPluginFactory *factory,
+									   AnjutaPluginHandle *handle,
+									   AnjutaShell *shell, GError **error)
+{
+	const gchar* module_path;
+	PyObject *module;
+	gchar **pieces;
+	GType type;
+	AnjutaPlugin *plugin;
+
+	g_return_val_if_fail (handle != NULL, NULL);
+	g_return_val_if_fail (shell != NULL, NULL);
+
+	pieces = g_strsplit (anjuta_plugin_handle_get_id (handle), ":", -1);
+	if (pieces == NULL)
+		return NULL;
+
+	type = g_type_from_name (pieces[1]);
+	if (type == G_TYPE_INVALID)	/* The plugin is not loaded */
+		{
+			module_path = anjuta_plugin_handle_get_path (handle);
+			/* If we have a special path, add it to sys.path */
+			if (module_path != NULL)
+				{
+					PyObject *sys_path = PySys_GetObject ("path");
+					PyObject *path = PyString_FromString (module_path);
+
+					if (PySequence_Contains(sys_path, path) == 0)
+						PyList_Insert (sys_path, 0, path);
+
+					Py_DECREF(path);
+				}
+
+			module = PyImport_ImportModule (pieces[0]);
+			if (!module)
+				{
+					PyErr_Print ();
+					return NULL;
+				}
+
+			type = g_type_from_name (pieces[1]);
+			if (type == G_TYPE_INVALID)
+				return NULL;
+		}
+
+	/* Create plugin */
+	plugin = (AnjutaPlugin *)g_object_new (type, "shell", shell, NULL);
+	return plugin;
+}
+
+static void
+pyl_ianjuta_plugin_factory_iface_init (IAnjutaPluginFactoryIface *iface)
+{
+	iface->new_plugin = pyl_ianjuta_plugin_factory_new_plugin;
+}
+
+/* AnjutaPlugin functions
+ *---------------------------------------------------------------------------*/
+
+static gboolean
+pyl_plugin_activate (AnjutaPlugin *plugin)
+{
+	//PythonLoaderPlugin *self = ANJUTA_PYTHON_LOADER_PLUGIN (plugin);
+
+	char *argv[] = { "anjuta", NULL };
+	PyTypeObject *plugin_type = NULL;
+	PyObject *m;
+
+	/* Python initialization */
+	if (Py_IsInitialized ())
+		return TRUE;
+
+	Py_InitializeEx (0);
+	if (PyErr_Occurred()) {
+		PyErr_Print();
+		return FALSE;
+	}
+
+	PySys_SetArgv (1, argv);
+
+	/* Retrieve the Python type for anjuta plugin */
+	m = PyImport_ImportModule ("gi.repository.Anjuta");
+	if (m != NULL)
+		plugin_type = (PyTypeObject *) PyObject_GetAttrString (m, "Plugin");
+
+	if (plugin_type == NULL) {
+		PyErr_Print ();
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+static gboolean
+pyl_plugin_deactivate (AnjutaPlugin *plugin)
+{
+	//PythonLoaderPlugin *self = ANJUTA_PYTHON_LOADER_PLUGIN (plugin);
+
+	if (Py_IsInitialized ()) {
+		while (PyGC_Collect ())
+			;
+
+		Py_Finalize ();
+	}
+
+	return TRUE;
+}
+
+
+/* GObject functions
+ *---------------------------------------------------------------------------*/
+
+static gpointer parent_class;
+
+static void
+pyl_plugin_instance_init (GObject *obj)
+{
+}
+
+static void
+pyl_plugin_class_init (GObjectClass *klass)
+{
+	AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
+
+	parent_class = g_type_class_peek_parent (klass);
+
+	plugin_class->activate = pyl_plugin_activate;
+	plugin_class->deactivate = pyl_plugin_deactivate;
+}
+
+ANJUTA_PLUGIN_BEGIN (PythonLoaderPlugin, pyl_plugin);
+ANJUTA_PLUGIN_ADD_INTERFACE(pyl_ianjuta_plugin_factory, IANJUTA_TYPE_PLUGIN_FACTORY);
+ANJUTA_PLUGIN_END;
+
+ANJUTA_SIMPLE_PLUGIN (PythonLoaderPlugin, pyl_plugin);
diff --git a/plugins/python-loader/plugin.h b/plugins/python-loader/plugin.h
new file mode 100644
index 0000000..3a686a7
--- /dev/null
+++ b/plugins/python-loader/plugin.h
@@ -0,0 +1,46 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
+/*
+    plugin.h
+    Copyright (C) 2007 Sebastien Granjoux
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    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 __PYTHON_LOADER_PLUGIN__
+#define __PYTHON_LOADER_PLUGIN__
+
+#include <libanjuta/anjuta-plugin.h>
+
+
+extern GType pyl_plugin_get_type (AnjutaGluePlugin *plugin);
+#define ANJUTA_TYPE_PYTHON_LOADER_PLUGIN         (pyl_plugin_get_type (NULL))
+#define ANJUTA_PYTHON_LOADER_PLUGIN(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), ANJUTA_TYPE_PYTHON_LOADER_PLUGIN, PythonLoaderPlugin))
+#define ANJUTA_PYTHON_LOADER_PLUGIN_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), ANJUTA_TYPE_PYTHON_LOADER_PLUGIN, PythonLoaderPluginClass))
+#define ANJUTA_IS_PYTHON_LOADER_PLUGIN(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), ANJUTA_TYPE_PYTHON_LOADER_PLUGIN))
+#define ANJUTA_IS_PYTHON_LOADER_PLUGIN_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), ANJUTA_TYPE_PYTHON_LOADER_PLUGIN))
+#define ANJUTA_PYTHON_LOADER_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), ANJUTA_TYPE_PYTHON_LOADER_PLUGIN, PythonLoaderPluginClass))
+
+typedef struct _PythonLoaderPlugin PythonLoaderPlugin;
+typedef struct _PythonLoaderPluginClass PythonLoaderPluginClass;
+
+struct _PythonLoaderPlugin {
+	AnjutaPlugin parent;
+};
+
+struct _PythonLoaderPluginClass {
+	AnjutaPluginClass parent_class;
+};
+
+#endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]