How can I find out whether a GSettings schema exists or not before attempting to use it?
Asked Answered
T

2

9

If a GSettings schema exists and has been compiled, there is usually no problem reading from it. However, if it doesn't exist, an error is usually thrown which cannot be handled. Try this in a Python file or console:

from gi.repository import Gio
try:
    settings = Gio.Settings("com.example.doesnotexist")
except:
    print "Couldn't load those settings!"

I'm being as broad as possible with the except, but this is the error that is thrown.

(process:10248): GLib-GIO-ERROR **: Settings schema 'com.example.doesnotexist' is not installed

What I basically want to do is find out if the com.example.doesnotexist schema exists or not; if not, then tell the user to run my setup script before using my application. Any other suggestions on doing this would be welcome.

Trawl answered 3/11, 2012 at 10:18 Comment(0)
P
7

You can use GSettingsSchemaSource. For instance:

> from gi.repository import Gio
> source = Gio.SettingsSchemaSource.get_default()
> source.lookup("org.gnome.Epiphany", True)
<GSettingsSchema at 0xa462800>
> source.lookup("com.example.doesnotexist", True)

>

According to the documentation, lookup should return NULL (None) if the schema does not exists, however in PyGObject returns NoneType. Anyway, you can use it to check whether the schema exists or not.

Plasticize answered 17/1, 2013 at 3:55 Comment(1)
Lovely, thanks so much. I don't know how you figured it out but I wasn't able to, so I appreciate you taking the time out to do this.Trawl
H
0

I know is is for Python. But here a solution for people using C:

gboolean g_settings_schema_exist (const char * id)
{ 
  gboolean ret = FALSE;
  GSettingsSchema * res = g_settings_schema_source_lookup (
                                 g_settings_schema_source_get_default(), 
                                 id, FALSE);                                 
  if (res != NULL)
  {
    ret = TRUE;
    g_object_unref (res);
  }
  
  return ret;
}
Hexyl answered 21/6, 2021 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.