GConf in Python

It just didn’t feel right to have KeySafe use a Windows-style INI file for its configuration so I started looking into using GConf instead.

There are good introductions to GConf here and here. Translating it all to Python is simple thanks to the brilliant people who gave us Gnome-Python.

I wrote this code for viewing the setting of the desktop background’s filename:

#! /usr/bin/python

import gtk
import gtk.glade
import gconf

class GConfViewer:
    def __init__(self):
        gui = gtk.glade.XML('Viewer/viewer.glade')
        self.entry = gui.get_widget('entry')

        client = gconf.client_get_default()
        client.add_dir('/desktop/gnome/background',
                gconf.CLIENT_PRELOAD_NONE)
        client.notify_add('/desktop/gnome/background/picture_filename',
                self.new_background)
        self.new_background(client)
        gui.get_widget('window').show_all()

    def new_background(self, client, *args, **kwargs):
        filename = client.get_string(
                '/desktop/gnome/background/picture_filename')
        self.entry.set_text(filename)

if __name__ == '__main__':
    GConfViewer()
    gtk.main()

I really like the client-server nature of it. A short explanation:

  1. Get the default GConf client.
  2. Add directories.
  3. Tell it to watch a specific key, specifying a callback method to be called when its value is changed.
  4. Define the callback method.

I also wrote code to change the value of a key:

#! /usr/bin/python

import gconf

def set_bool_key(key):
    client = gconf.client_get_default()
    client.set_bool(key, 1)

if __name__ == '__main__':
    set_bool_key('/desktop/gnome/background/draw_background')

Quite self-explanatory, isn’t it?

The next step in development would be to create a schema for the keys. Both introductions above contain pointers on how to write schemas, for more pointers just take a look in /usr/share/gconf/schemas/. The only problem I ran into was registering the schema. The following commandline does the trick:

GCONF_CONFIG_SOURCE=$(gconftool-2 --get-default-source) \
gconftool-2 --makefile-install-rule keysafe.schemas

Of course the kind Debian developers have done their best to shield packagers from nitty-gritty details. CDBS didn’t just work, it seems to make assumptions about the build system of the package (assuming it’s using auto-tools). Just making sure that the schema ends up in usr/share/gconf/schema during package binary-install, then call dh_gconf -ppackage in binary-post-install/package did the trick.

5 Comments

  1. Jay says:

    For people looking to use client.set_list() I found this link that helped me out http://www.advogato.org/person/clarkbw/diary.html?start=36

  2. Magnus says:

    It does look helpful. I believe the following link takes you straight to the info.

  3. [...] Estoy indagando más en python gtk, la verdad es que es muy sencillo menos por el gtreeview que es bastante complejo, pero de una potencia muy impresionante. Otro problema que estoy viendo es que no tengo documentación para gconf en python, lo cual me deja con el culo al aire y buscarme la vida mirando fuentes, algo bastante lamentable, menos mal que están ciertos blogs que ayudan mucho. Tambien recomendar un pequeño aplet que he descubierto que me permite desde el panel de Gnome tener una mini aplicación para enviar post’s a mi blog, lo cual creo que me permitirá enviar más a menudo post tan poco útiles como este. [...]

  4. Joxn says:

    Thank you for this entry. It may be several years old, but it helped me a great deal!

  5. Wolter says:

    Great tutorial, I just have some questions.

    Is the add dir method what one should normally use to initialize a key on a system? i.e. If your application is being used for the first time, is this the method that should create the gconf key?

    Also, the notify add method didn’t work for me. Isn’t it supposed to call the callback whenever the key is changed?

Leave a Reply

Please use markdown to make your comment beautiful.