在 Gio.Settings 我可以使用列出可重定位模式
Gio.Settings.list_relocatable_schemas()
我可以使用
Gio.Settings.new_with_path(schema_id, path)
获取Gio.Settings
实例。但是我怎样才能获得path
当前用于给定的所有值schema_id
?
通常,模式具有确定设置在概念全局设置树中的存储位置的固定路径。然而,模式也可以是“可重定位的”,即不配备固定路径。这很有用,例如当模式描述一个“帐户”,并且您希望能够存储任意数量的帐户时。
不new_with_path
就是为了这个吗?您必须将架构存储在与帐户关联的某个位置,但这不是设置系统的责任。我认为new_with_path
适用于您的架构依赖于帐户的情况。
我认为您可以使用GSettingsSchemas找到更多信息- 这是描述中的一个示例,说明 Schema 是插件的一部分。
不幸的是,你不能从 Gio.Settings 中做到这一点。
我在这里看到两个选项:
利用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)
你不能,至少不能用于任意模式,这是根据可重定位模式的定义:可以有多个实例,存储在多个任意路径中的模式。
由于可重定位模式实例基本上可以存储在 DConf 内的任何位置gsettings
,无法列出它们的路径,因此它不跟踪实例。也dconf
帮不了你,因为它根本没有模式的概念,它只知道路径和键。它可以列出给定路径的子路径,仅此而已。
在创建给定可重定位模式的多个实例时,应用程序应将每个实例存储在合理、易于发现的路径中,例如(不可重定位的)应用程序模式的子路径。或者将实例路径(或后缀)存储为此类架构中的列表键。
或者两者兼而有之,就像 Gnome 终端对其配置文件所做的那样:
org.gnome.Terminal.ProfilesList
是一个不可重定位的常规模式,存储在 DConf 路径/org/gnome/terminal/legacy/profiles:/
default
带有单个 UUID 的字符串,以及一个list
包含 UUID 的字符串列表。org.gnome.Terminal.Legacy.Profile
,并且存储在您猜想... /org/gnome/terminal/legacy/profiles:/:<UUID>/
!gsettings
通过这种方式,客户list
端可以dconf
通过直接列出/org/gnome/terminal/legacy/profiles:/
.
当然,对于不可重定位的模式,您总是可以通过以下方式获取它们的路径:
gsettings list-schemas --print-paths