[PyKDE] KFilePlugins, using Python from C++ code and vice versa

Jim Bublitz jbublitz at nwinternet.com
Thu Aug 31 23:18:13 BST 2006


On Thursday 31 August 2006 03:59, Joachim Werner wrote:
> Hi,
>
> quite a while ago I asked if anybody had working examples of KFilePlugins
> written in Python, but I got no answer. I guess the reason was that there
> are no such examples.
>
> So let me rephrase the problem:
>
> What I want to do is prototype a metadata pluging for KDE that can fetch
> file metadata from a server instead of reading the files.
>
> E.g., if I open a folder with dozens of large digital photographs in JPEG
> format, what will happen is that Konqueror, or actually the KFilePlugin
> that is registered for jpeg, reads the EXIF metadata (the info about when,
> with what lens, aperture etc. the photo was taken) from the file and
> generates a thumbnail. If the folder is on a remote drive this will create
> quite a lot of traffic and take quite a while if you have low bandwidth.
>
> But if the file plugin could just ask the server for the metadata and get
> the metadata and thumbnail from the server, things would be faster and need
> less bandwidth.
>
> The client-server part is easy. In my prototype I'm using a Python xmlrpc
> server and client. On the server side I can call kfile (the command line
> client for accessing file plugins) from Python. So that's no issue either.
>
> But on the client I'd now like to replace the existing file plugins by a
> remote metadata proxy that redirects the API calls to the remote server.
>
> KFilePlugins are available in PyKDE, but the missing piece for me is how to
> register them. Usually, KFilePlugins are referenced by name in a .desktop
> file, and the matching dynamic library is loaded when needed.
>
> So the question is how I can do that with PyKDE. Do I have to write a
> wrapper dynamic library in C++ that calls my Python code? If yes, how? (An
> example would be fine.)

Yes, you have to create a .so and a .la file. You can fake the .la file, but 
it has to exist or KDE won't find the .so.

Basically, your .so (in C++ of course) needs to load the Python interpreter, 
then use Python C API calls to load your script into the interpreter, and 
then invoke the entry point of the script to create the KDE object in 
Python... Finally, pass the pointer for Python-created KDE object back to 
the .so and from there back to the loader code that loaded the .so in the 
first place.

 You can pass pointers to Qt/KDE objects between C++ and Python as integers 
and use sip.wrapinstance() and sip.unwrapinstance() (in sip docs) to convert 
them back and forth.

It's actually very little C++ code, and sometimes it actually works :)

It usually works, but you may run into problems calling back into the Python 
created stuff from KDE when your new object is actually running. I've had 
that problem trying to create Control Center modules, but apparently Simon 
Edwards hasn't. You might want to look at his pykdeextension stuff (sorry, 
don't have URL handy), because it has to do what you're asking about.

I have example code somewhere, but it's written in terms of other libraries 
and wouldn't be very useful.

Jim





More information about the PyQt mailing list