[libpeas/proxys: 22/25] [Python] Port loader to PyGI



commit ef12d2937e8355d276e3c89f1a5c834b72b671a2
Author: Steve Frécinaux <code istique net>
Date:   Fri May 21 00:32:56 2010 +0200

    [Python] Port loader to PyGI

 loaders/python/peas-plugin-loader-python.c   |  120 +++++++------------------
 peas-demo/peas-demo-window.c                 |   16 +++-
 peas-demo/plugins/pythonhello/pythonhello.py |   23 +++--
 3 files changed, 61 insertions(+), 98 deletions(-)
---
diff --git a/loaders/python/peas-plugin-loader-python.c b/loaders/python/peas-plugin-loader-python.c
index b22b3ac..2675dc8 100644
--- a/loaders/python/peas-plugin-loader-python.c
+++ b/loaders/python/peas-plugin-loader-python.c
@@ -23,18 +23,11 @@
 #include "peas-extension-python.h"
 #include "peas-plugin-loader-python.h"
 
-#if 0
-#define NO_IMPORT_PYGOBJECT
-#define NO_IMPORT_PYGTK
-#endif
-
 #include <Python.h>
 #include <pygobject.h>
 #include <signal.h>
 #include "config.h"
 
-extern PyTypeObject PyGObject_Type;
-
 #if PY_VERSION_HEX < 0x02050000
 typedef int Py_ssize_t;
 #define PY_SSIZE_T_MAX INT_MAX
@@ -51,6 +44,8 @@ typedef struct {
   PyObject *module;
 } PythonInfo;
 
+static PyObject *PyGObject_Type;
+
 static gboolean   peas_plugin_loader_python_add_module_path (PeasPluginLoaderPython *self,
                                                              const gchar            *module_path);
 
@@ -79,25 +74,29 @@ find_python_extension_type (PeasPluginInfo *info,
                             GType           exten_type,
                             PyObject       *pymodule)
 {
+  PyObject *pygtype, *pytype;
   PyObject *locals, *key, *value;
   Py_ssize_t pos = 0;
-  GObject *object;
 
   locals = PyModule_GetDict (pymodule);
 
+  pygtype = pyg_type_wrapper_new (exten_type);
+  pytype = PyObject_GetAttrString (pygtype, "pytype");
+  g_return_val_if_fail (pytype != NULL, NULL);
+
   while (PyDict_Next (locals, &pos, &key, &value))
     {
       if (!PyType_Check (value))
         continue;
 
-      if (!PyObject_IsSubclass (value, (PyObject *) &PyGObject_Type))
-        continue;
-
-      object = pygobject_get (value);
-      if (G_TYPE_CHECK_INSTANCE_TYPE (object, exten_type))
-        return (PyTypeObject *) value;
+      if (PyObject_IsSubclass (value, pytype))
+        {
+          Py_DECREF (pygtype);
+          return (PyTypeObject *) value;
+        }
     }
 
+  Py_DECREF (pygtype);
   g_debug ("No %s derivative found in Python plugin '%s'",
            g_type_name (exten_type), peas_plugin_info_get_name (info));
   return NULL;
@@ -215,13 +214,12 @@ peas_plugin_loader_python_load (PeasPluginLoader *loader,
                                 PeasPluginInfo   *info)
 {
   PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
-  PyObject *main_module, *main_locals;
   PyObject *pymodule, *fromlist;
   gchar *module_name;
 
   if (pyloader->priv->init_failed)
     {
-      g_warning ("Cannot load python plugin Python '%s' since libpeas was"
+      g_warning ("Cannot load python plugin Python '%s' since libpeas was "
                  "not able to initialize the Python interpreter.",
                  peas_plugin_info_get_name (info));
       return FALSE;
@@ -231,28 +229,16 @@ peas_plugin_loader_python_load (PeasPluginLoader *loader,
   if (g_hash_table_lookup (pyloader->priv->loaded_plugins, info))
     return TRUE;
 
-  main_module = PyImport_AddModule ("libpeas.plugins");
-  if (main_module == NULL)
-    {
-      g_warning ("Could not get libpeas.plugins.");
-      return FALSE;
-    }
-
   /* If we have a special path, we register it */
   peas_plugin_loader_python_add_module_path (pyloader,
                                              peas_plugin_info_get_module_dir (info));
 
-  main_locals = PyModule_GetDict (main_module);
-
   /* we need a fromlist to be able to import modules with a '.' in the
      name. */
   fromlist = PyTuple_New (0);
   module_name = g_strdup (peas_plugin_info_get_module_name (info));
 
-  pymodule = PyImport_ImportModuleEx (module_name,
-                                      main_locals,
-                                      main_locals,
-                                      fromlist);
+  pymodule = PyImport_ImportModuleEx (module_name, NULL, NULL, fromlist);
 
   Py_DECREF (fromlist);
 
@@ -263,9 +249,6 @@ peas_plugin_loader_python_load (PeasPluginLoader *loader,
       return FALSE;
     }
 
-  PyDict_SetItemString (main_locals, module_name, pymodule);
-  g_free (module_name);
-
   add_python_info (pyloader, info, pymodule);
 
   return TRUE;
@@ -374,49 +357,10 @@ peas_init_pygobject (void)
   init_pygobject_check (2, 11, 5);      /* FIXME: get from config */
 }
 
-static void
-peas_init_libpeas (void)
-{
-  PyObject *libpeas, *mdict, *version, *required_version, *pluginsmodule;
-
-  libpeas = PyImport_ImportModule ("libpeas");
-  if (libpeas == NULL)
-    {
-      PyErr_SetString (PyExc_ImportError, "could not import libpeas module");
-      return;
-    }
-
-  mdict = PyModule_GetDict (libpeas);
-  version = PyDict_GetItemString (mdict, "version");
-  if (!version)
-    {
-      PyErr_SetString (PyExc_ImportError,
-                       "could not get libpeas module version");
-      return;
-    }
-
-  required_version = Py_BuildValue ("(iii)",
-                                    PEAS_MAJOR_VERSION,
-                                    PEAS_MINOR_VERSION, PEAS_MICRO_VERSION);
-
-  if (PyObject_Compare (version, required_version) == -1)
-    {
-      PyErr_SetString (PyExc_ImportError, "libpeas module version too old");
-      Py_DECREF (required_version);
-      return;
-    }
-
-  Py_DECREF (required_version);
-
-  /* initialize empty libpeas.plugins module */
-  pluginsmodule = Py_InitModule ("libpeas.plugins", NULL);
-  PyDict_SetItemString (mdict, "plugins", pluginsmodule);
-}
-
 static gboolean
 peas_python_init (PeasPluginLoaderPython *loader)
 {
-  PyObject *mdict, *gettext, *install, *gettext_args;
+  PyObject *mdict, *gobject, *gettext, *install, *gettext_args;
   char *argv[] = { "libpeas", NULL };
 #ifdef HAVE_SIGACTION
   struct sigaction old_sigint;
@@ -482,21 +426,26 @@ peas_python_init (PeasPluginLoaderPython *loader)
   peas_init_pygobject ();
   if (PyErr_Occurred ())
     {
-      g_warning
-        ("Error initializing Python interpreter: could not import pygobject.");
+      g_warning ("Error initializing Python interpreter: could not "
+                 "import pygobject.");
 
       goto python_init_error;
     }
 
-  /* import libpeas */
-  peas_init_libpeas ();
-  if (PyErr_Occurred ())
+  gobject = PyImport_ImportModule ("gobject");
+  if (gobject == NULL)
     {
-      PyErr_Print ();
-
-      g_warning
-        ("Error initializing Python interpreter: could not import libpeas module.");
+      g_warning ("Error initializing Python interpreter: cound not "
+                 "import gobject.");
+      goto python_init_error;
+    }
 
+  mdict = PyModule_GetDict (gobject);
+  PyGObject_Type = PyDict_GetItemString (mdict, "GObject");
+  if (!PyGObject_Type)
+    {
+      g_warning ("Error initializing Python interpreter: cound not "
+                 "get gobject.GObject");
       goto python_init_error;
     }
 
@@ -504,8 +453,8 @@ peas_python_init (PeasPluginLoaderPython *loader)
   gettext = PyImport_ImportModule ("gettext");
   if (gettext == NULL)
     {
-      g_warning
-        ("Error initializing Python interpreter: could not import gettext.");
+      g_warning ("Error initializing Python interpreter: could not "
+                 "import gettext.");
 
       goto python_init_error;
     }
@@ -523,9 +472,8 @@ peas_python_init (PeasPluginLoaderPython *loader)
 
 python_init_error:
 
-  g_warning
-    ("Please check the installation of all the Python related packages required "
-     "by libpeas and try again.");
+  g_warning ("Please check the installation of all the Python related packages "
+             "required by libpeas and try again.");
 
   PyErr_Clear ();
 
diff --git a/peas-demo/peas-demo-window.c b/peas-demo/peas-demo-window.c
index 660db4c..fcdcfa2 100644
--- a/peas-demo/peas-demo-window.c
+++ b/peas-demo/peas-demo-window.c
@@ -60,17 +60,31 @@ on_extension_removed (PeasExtensionSet *set,
   peas_extension_call (exten, "deactivate", dw);
 }
 
+static gboolean
+on_delete_event (GtkWidget *window,
+                 GdkEvent  *event,
+                 gpointer   user_data)
+{
+  DemoWindow *dw = DEMO_WINDOW (window);
+  peas_extension_set_call (dw->exten_set, "deactivate", dw);
+
+  return FALSE;
+}
+
 static void
 demo_window_set_data (DemoWindow *dw,
                       PeasEngine *engine)
 {
   dw->engine = engine;
+  g_object_ref (dw->engine);
+
   dw->exten_set = peas_extension_set_new (engine, PEAS_TYPE_ACTIVATABLE);
 
   peas_extension_set_call (dw->exten_set, "activate", dw);
 
   g_signal_connect (dw->exten_set, "extension-added", G_CALLBACK (on_extension_added), dw);
   g_signal_connect (dw->exten_set, "extension-removed", G_CALLBACK (on_extension_removed), dw);
+  g_signal_connect (dw, "delete-event", G_CALLBACK (on_delete_event), NULL);
 }
 
 static void
@@ -78,8 +92,6 @@ demo_window_finalize (GObject *object)
 {
   DemoWindow *dw = DEMO_WINDOW (object);
 
-  peas_extension_set_call (dw->exten_set, "deactivate", dw);
-
   g_object_unref (dw->exten_set);
   g_object_unref (dw->engine);
 
diff --git a/peas-demo/plugins/pythonhello/pythonhello.py b/peas-demo/plugins/pythonhello/pythonhello.py
index e5e7bab..e820fc2 100644
--- a/peas-demo/plugins/pythonhello/pythonhello.py
+++ b/peas-demo/plugins/pythonhello/pythonhello.py
@@ -1,22 +1,25 @@
 # -*- coding: utf-8 -*-
 # ex:set ts=4 et sw=4 ai:
 
-import gobject
-import libpeas
-import gtk
+from gi.repository import Peas
+from gi.repository import Gtk
 
 LABEL_STRING="Python Says Hello!"
 
-class PythonHelloPlugin(libpeas.Plugin):
+class PythonHelloPlugin(Peas.Plugin, Peas.Activatable):
+    __gtype_name__ = 'PythonHelloPlugin'
+
     def do_activate(self, window):
         print "PythonHelloPlugin.do_activate", repr(window)
-        window._pythonhello_label = gtk.Label(LABEL_STRING)
-        window._pythonhello_label.show()
-        window.get_child().pack_start(window._pythonhello_label)
+#        window._pythonhello_label = Gtk.Label()
+#        window._pythonhello_label.set_text(LABEL_STRING)
+#        window._pythonhello_label.show()
+#        window.get_child().pack_start(window._pythonhello_label)
 
     def do_deactivate(self, window):
         print "PythonHelloPlugin.do_deactivate", repr(window)
-        window.get_child().remove(window._pythonhello_label)
-        window._pythonhello_label.destroy()
+#        window.get_child().remove(window._pythonhello_label)
+#        window._pythonhello_label.destroy()
 
-gobject.type_register(PythonHelloPlugin)
+    def do_update_state(self, window):
+        print "PythonHelloPlugin.do_update_state", repr(window)



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