How persist values with Gio.Settings and Gio.SettingsSchema (Python)

Hi there!

I’m trying to find the right place to discuss GNOME application development questions (tried on IRC #gnome before). The GNOME developer docs don’t clarify this. I hope I can place developer questions here.

I need to write and read Gio.Settings from a GEdit plugin, written in Python. I’m looking for good examples. Reading is pretty straight-forward, but I’d like to write the value to Gio.Settings when they’re not yet there. Hence, create a GSettingsSchema first, and a value therein. That seems tricky.

What is the correct approach to do this? My strategy is:

  1. Try to read out the value, e.g.
    settings = Gio.Settings.new('org.gnome.gedit.plugins.foobar')
    
  2. If that fails create the schema and write the default value in it.
    # Gio.Settings doesn't seem to provide an interface for that
    # Gio.SettingsSchema doesn't create an instance with the constructor
    
  3. In the (GEdit plugin Preferences dialog) update the value as required after user input.
    my_value = settings.get_string('my-key-in-foobar')
    ...
    settings.set_string('my-key-in-foobar', my_value)
    

Next time GEdit accesses the Gio.Settings 1. should succeed, and 2. can be skipped.


Where can I find related coding examples? Preferably in Python, but C/C++/Vala would also be fine, as long as it’s sufficiently easy to match the implementation to the Python world, mentally.

Thanks in advance,
Peter

1 Like

Related docs

That’s not how GSettings works.

GSettings are defined by a schema—which operates like a database schema. Each schema defines the keys it contains, the type of data they contain, and their default value.

You install a schema inside a well-known directory, and call glib-compile-schemas to build a binary representation of all installed schemas that can be shared across processes.

If the key is unset in the user database (whichever that might be), then you’re guaranteed to get its default out of its schema.

The GSettings reference has an entire section describing the basic concepts of the API.

I’m not sure I understand. Maybe I shouldn’t have said, “default values”.

How do I write my application preferences when the schema is not yet there? Does the installation process have to cover creating the schema?

Yes. You need to write the schema; install it; and then call glib-compile-schemas. You can look at how gedit does it, since you’re writing a gedit plugin.

Okay. What happens if someone messes with the settings data store. Shouldn’t the application be capable to handle that, i.e. recreate the schema and write out their application settings again?

Otherwise it may become unusable for the end user who relies on settings being successfully persisted. I would expect an application to handle situations like that.

That’s really not how anything works. You do not “recreate” a schema: a schema is a description of the data you consider valid.

The schema specifies the type of the values, and optionally even ranges, so the storage cannot contain something that isn’t at least valid.

Applications can reset a key to its default value, but that default must exist in the schema; resetting a key is an action you make to roll back the existing setting—an explicit “reset to default” control in your UI.

It seems you’re overthinking this a lot. All you need, when using GSettings, is:

  • provide a schema
  • install the schema (and call glib-compile-schemas as part of that operation)
  • create a GSettings instance for that schema
  • read settings when building the UI, write them when the user changes something
  • connect to the changed signal to know if a setting has changed, and reflect that change in your UI

That’s it.

1 Like

Okay, I think I get it. This approach has advantages. As a result, you only can update settings. Extending the structure is not meant to happen. Data structures are predefined.

What do you recommend for a simple plugin, something that people download from GitHub to install in their local path? Do I have to, e.g., provide a Debian package with a post-install hook that runs glib-compile-schemas, or can I allow a simpler approach?

Yes. You define your own settings and the data they can contain.

Have you looked at other gedit plugins? Because a lot of them have schemas.

You don’t need a distribution package, but you must have some sort of installation script to install your ancillary files—like settings schemas.

The schema can be dynamically loaded using GSettingsSchemaSource instead of being installed in a known schema location. This allows the schema file to be kept within the plugin directory and not require an installation script.

This is how Control Your Tabs (which is my plugin, full disclosure?) loads its schema and gets a settings object.

settings.bind() (sorry I can’t link to the GSettings documentation right now) is also useful in keeping a pref pane widget in sync with a setting. (Keeping track of how the setting affects your plugin’s functionality is a different matter.)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.