GConf in Python
- Magnus Therning
It just didn’t feel right to have KeySafe (defunct link) 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):
= gtk.glade.XML('Viewer/viewer.glade')
gui self.entry = gui.get_widget('entry')
= gconf.client_get_default()
client '/desktop/gnome/background',
client.add_dir(
gconf.CLIENT_PRELOAD_NONE)'/desktop/gnome/background/picture_filename',
client.notify_add(self.new_background)
self.new_background(client)
'window').show_all()
gui.get_widget(
def new_background(self, client, *args, **kwargs):
= client.get_string(
filename '/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:
- Get the default GConf client.
- Add directories.
- Tell it to watch a specific key, specifying a callback method to be called when its value is changed.
- 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):
= gconf.client_get_default()
client 1)
client.set_bool(key,
if __name__ == '__main__':
'/desktop/gnome/background/draw_background') set_bool_key(
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.