【问题标题】:How can I find out whether a GSettings schema exists or not before attempting to use it?在尝试使用 GSettings 架构之前,如何确定它是否存在?
【发布时间】:2012-10-23 21:02:47
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: python pygobject gio


    【解决方案1】:

    您可以使用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 (None),但是在 PyGObject 中返回 NoneType。无论如何,您可以使用它来检查架构是否存在。

    【讨论】:

    • 可爱,非常感谢。我不知道你是怎么想出来的,但我没能弄明白,所以我很感谢你抽出时间来做这件事。
    【解决方案2】:

    我知道是针对 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多