如何在 Gio.Settings 中获取可重定位架构的路径?

问题描述 投票:0回答:3

在 Gio.Settings 中,我可以使用列出可重定位架构

Gio.Settings.list_relocatable_schemas()

我可以使用

Gio.Settings.new_with_path(schema_id, path)

获取

Gio.Settings
实例。但是我怎样才能获得当前用于给定
path
schema_id
的所有值?

gtk gtk3 gio gsettings
3个回答
2
投票

通常,模式具有固定路径,用于确定模式的位置 设置存储在概念性全局设置树中。 然而,模式也可以是“可重定位的”,即不配备 固定路径。这很有用,例如当模式描述一个 ‘account’,并且你希望能够存储任意数量的 账户。

new_with_path
不就是为了这个吗?您必须将架构存储在与帐户关联的某个位置,但这不是设置系统的责任。我认为
new_with_path
适用于您的架构依赖于帐户的情况。

我认为您可以通过 GSettingsSchemas 找到更多信息 - 这是描述中架构是插件一部分的示例。


0
投票

不能,至少不能对于任意模式,这就是可重定位模式的定义:可以有多个实例的模式,存储在多个任意路径中。

由于可重定位模式实例基本上可以存储在 DConf 中的

任何地方,因此gsettings

无法列出它们的路径,因此它不会跟踪实例。而且 
dconf
 也帮不了你,因为它根本没有 
schemas 的概念,它只知道路径和键。它可以列出给定路径的子路径,但仅此而已。

当创建给定可重定位模式的多个实例时,应用程序需要将每个实例存储在合理的、易于发现的路径中,例如(不可重定位)应用程序模式的子路径。或者将实例路径(或后缀)存储为此类架构中的列表键。

或者两者兼而有之,就像 Gnome Terminal 对其配置文件所做的那样:

  • org.gnome.Terminal.ProfilesList
     是不可重定位的常规模式,存储在 DConf 路径 
    /org/gnome/terminal/legacy/profiles:/
    
    
  • 该架构有 2 个键,一个带有单个 UUID 的
  • default
     字符串,以及包含 UUID 的 
    list
     字符串列表。
  • 每个配置文件都是
  • 可重定位模式org.gnome.Terminal.Legacy.Profile
    的一个实例,并且存储在,你猜......
    /org/gnome/terminal/legacy/profiles:/:<UUID>/
这样,客户端可以使用

gsettings

 读取 
list
 并从 UUID 构建路径,或者从 
dconf
 直接列出 
/org/gnome/terminal/legacy/profiles:/
 的子路径来访问所有实例。

当然,对于

不可重定位模式,您始终可以通过以下方式获取它们的路径:

gsettings list-schemas --print-paths
    

0
投票
不幸的是,您无法通过 Gio.Settings 进行此操作。

我在这里看到两个选项:

  • 保留单独的gsetting来存储可重定位模式的路径

  • 利用

    dconf API,这是一个低级配置系统。由于没有 Python 绑定(猜测是 Python 问题),我建议使用 ctypes 与 C 进行绑定。 如果您知道可重定位架构的根路径,您可以使用下面的代码片段列出它们。

    import ctypes from ctypes import Structure, POINTER, byref, c_char_p, c_int, util from typing import List class DconfClient: def __init__(self): self.__dconf_client = _DCONF_LIB.dconf_client_new() def list(self, directory: str) -> List[str]: length_c = c_int() directory_p = c_char_p(directory.encode()) result_list_c = _DCONF_LIB.dconf_client_list(self.__dconf_client, directory_p, byref(length_c)) result_list = self.__decode_list(result_list_c, length_c.value) return result_list def __decode_list(self, list_to_decode_c, length): new_list = [] for i in range(length): # convert to str and remove slash at the end decoded_str = list_to_decode_c[i].decode().rstrip("/") new_list.append(decoded_str) return new_list class _DConfClient(Structure): _fields_ = [] _DCONF_LIB = ctypes.CDLL(util.find_library("dconf")) _DCONF_LIB.dconf_client_new.argtypes = [] _DCONF_LIB.dconf_client_new.restype = POINTER(_DConfClient) _DCONF_LIB.dconf_client_new.argtypes = [] _DCONF_LIB.dconf_client_list.argtypes = [POINTER(_DConfClient), c_char_p, POINTER(c_int)] _DCONF_LIB.dconf_client_list.restype = POINTER(c_char_p)
    
    
© www.soinside.com 2019 - 2024. All rights reserved.