9

如果 GSettings 模式存在并且已经编译,那么读取它通常没有问题。但是,如果它不存在,通常会抛出无法处理的错误。在 Python 文件或控制台中试试这个:

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

我尽可能广泛地使用except,但这是引发的错误。

(进程:10248):GLib-GIO-ERROR **:未安装设置架构“com.example.doesnotexist”

我基本上想要做的是找出com.example.doesnotexist模式是否存在;如果没有,那么告诉用户在使用我的应用程序之前运行我的设置脚本。任何其他关于这样做的建议都将受到欢迎。

4

2 回答 2

7

您可以使用GSettingsSchemaSource。例如:

> 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)

>

根据文档,如果模式不存在,查找应该返回NULL( ),但是在 PyGObject 中返回 NoneType。None无论如何,您可以使用它来检查模式是否存在。

于 2013-01-17T03:55:38.843 回答
0

我知道是针对 Python 的。但这里为使用 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;
}
于 2021-06-21T05:43:09.263 回答