[pygtk] How to write a python plugin ?

Sébastien Granjoux seb.sfo at free.fr
Tue May 8 21:44:39 WST 2007


Hi Johan,

Thanks for you fast reply.

Johan Dahlin a écrit :
> You should try looking at the gedit python plugin, which is very similar 
> to what you want.

Yes, I want to do the same thing and it was my first example. But it 
doesn't fit completely. It is doing exactly what I have described as my 
first try.

I mean that for each python plugin, Gedit create 2 GeditPlugin objects. 
One is created as the base of GeditPythonObject in C and one as the base 
of by example ModelinePlugin in python through the pygobject library.

It doesn't really matter if GeditPlugin doesn't have any data which is 
the case in gedit. But, my plugin class has some data. If I use this 
way, I need to copy them each time there is change. It's possible as 
these are gobject properties, so I can get a notification when it 
changes. But, there is no need to have two times the same object.


Anyway, in the meantime, it seems that I have found a way to do it. It's 
   a derivate of my third trial: Create the Plugin object in C and try 
to use it in the python code.

The instance initialization function of the PythonPlugin object is the same

static void python_plugin_init (PythonPlugin *object)
{
     object->instance = pygobject_new (G_OBJECT(object));
}

But, before when I load the python file and I have the type of the real 
plugin object written in python (in PyObject* variable type), I register 
it as a custom type like in the following code:

pyg_type_register_custom_callback (type_name, mylib_custom_type, type);

static PyTypeObject*
mylib_custom_type (const gchar *name, gpointer data)
{
	return (PyTypeObject *)data;
}


It seems to work fine for me. If I set a property of the PythonPlugin 
object in C, I can read it back in the plugin implementation in python 
without any additional code.

Do you know if it's right way to do this or if there is an easier way to 
achieve this ?

Regards,

Sébastien


More information about the pygtk mailing list