GSettings – Flexible Configuration System

GSetttings is the standard way how Gnome 3 applications store their configuration. GSettings is the front-end interface for application, actual values are stored by back-end – standard one is called dconf. We can then use the tool dconf-editor to easily browse all stored configurations for all applications. Thanks to GObject introspection we can also work easily with GSettings from python.

Introduction to GSettings in Python

Before we can start with programming, we have to define so call schema of our configuration – schema is a XML file, which describes keys in our configuration.  Schema can look like this:

Each schema has an unique  id (using dot notation to make it unique) and also it needs to have a  path under whitch values are stored. But path is derived from id (dots converted to slashes), so it is not necessary to specify it, unless you plan to use some special path, then you can do it with path attribute on schema element.

Within schema you can define various elements with simple or complex types (values are stored as GLib.Variant – so see for its types definition notation). For integer types you can define ranges (see line 16), or define enumerated  type (see lines 3-7 and 36).  Also the schema can have a child schema – when this child schema will have path path/of/parent/child-name/ (and is accessible via get_child('child-name') method of Settings object).

Before we can work with schema we have to compile it, schemas normally reside in directory /usr/share/glib-2.0/schemas  – so we should copy our schema there (with mandatory extension gschema.xml)  and compile:

sudo cp my-schema.gschema.xml /usr/share/glib-2.0/schemas
glib-schema-compile  /usr/share/glib-2.0/schemas

Sometime it is not convenient (especially during development) to have schema in central location –  schema can also reside in the local directory and can be compiled there glib-compile-schemas --strict . (strict option provides rigid check of our schema).   However this will require bit more complicated way how to instantiate schema in program – I”l describe it latter.

In python we can use GObject Introspection to work with GSettings, GSettings are part of GIO library.  To import and instantiate schema we can use this code:

Now we can get and set values like this (for all available methods of Settings class see its C documentation– in python they are available without g_settings_ prefix) :

For complex type ‘as’ we  use Variant constructor and Variant unpack method to convert from/to python native types as show above.

 How to work with schema in local directory

In order to instantiate schema that is in a local directory we have to do these several steps:

This opens Settings with schema from DIRECTORY.    In an application it would be nice if it opens local schema, while in development, but when installed regularly  it’ uses schema from central location.   In my project thetool I created class that  does this – you can check it here.
On line 6 the second parameter, which is None, can be an instance of  configuration back-end, this way I think you can even have separated store for development – but did not play with it yet.

Binding of settings to object properties

One of very cools features of GSettings is that you can bind some setting directly to a property of GObject and setting will set this property initially and then setting changes whenever the property changes.   This is nice feature for a configuration dialogue, where we can bind settings to widgets like this (in __init__ method of configuration window):

 1-n relationship

The child element of schema enables 1-1 relationship (because name is already in schema).  However consider this scenario – I need do define some device configuration and my application works with many devices.  Each configuration is similar, but have different values.

For this we  define key  of type array of strings in main schema like this:

This will keep unique keys to all know devices.

And we  create schema (in same file as main schema) for device configuration with id eu.zderadicka.sample.device, where all necessary keys for device configuration are defined.
In program can then easily get instance of schema for particular device with this function

And of course we need to update devices list in main settings whenever we add or remove device from application configuration.


The only unpleasant issue I’ve found is that when you try to reach key, that is not defined in the schema – then application crashes (at least that was my case). Luckily GSettings write some warning on stderr first so it’s clear what is the problem.

Also I’m not sure how to delete unused configurations – does not seem you to be available  via GSetting interface.   dconf tool has option reset, which will remove path without attached schema – so in our example of devices we can do dconf reset -f /eu/zderadicka/sample/device-xyz to remove configuration for device-xyz.



2 thoughts on “GSettings – Flexible Configuration System”

    1. sorry to interrupt again
      but there is a typo in command
      sudo cp my-schema.gshema.xml /usr/share/glib-2.0/schemas
      gshema -> gschema
      also in later version seem glib-schema-compile changed to glib-compile-schemas

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">