18 Nov 2005 iagorubio   » (Journeyer)

Python embedding, easy and sweet

Really, when I put my hands and brains to add scripting capabilities to cssed, I didn't thought I was gonna be so sweet.

There were things I thought I was going to face as real problems. First, the scripting support must be added as a plugin, so It must be contained on an external library. Second it had to access a really limited amount of cssed's code, because it can use just the plugin's API, and furthermore it must resemble the plugin's API without extra boilerplate. Third I need more than just some processing capabilities, because I wanted the scripts to be able to show dialogs and windows of its own, and the script's UI should interact with the main application smoothly.

So when I put it all on paper, I thought "It's gonna be a piece of work ...".

Then I came to python. I'd used it, it's a nice scripting language, and it has good GTK bindings - cssed is a GTK application.

So I read the python docs, I looked for some helpers as SWIG - but it was like killing flies with a howitzer - and let's take a try.

Frankly, in two hours I'd got Python embedded into a cssed plugin, from which I can import the python "cssed" object and call some cssed functions.

I was really easy, direct pointer conversion between C and python lets you pass your C structure to any Python function. Lots of the logic falls cleverly into Python, as example, just returning a NULL caused the right exception at the python level. Easy parameter conversion from Python object to C types, and even easier to return Python object from C types, or structures.

Not just that, PyGTK worked so smooth that you'll never be able to tell what dialog is created by cssed, or by a python script. You can even easily embed PyGTK widgets into your application UI, such as adding a tool bar, menu or panel. And I'm not speaking just about Linux/Unix but it runs smoothly on Win32 as well.

If you've been reluctant to take the extra work to add scripting capabilities to your GTK application, because you think it will be a hard work - as I did - just put your hands on Python.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!