首页 > 解决方案 > 如何从python将'a {sv}' dbus签名传递给udisks2.Mount()?

问题描述

dbusapi 使用一种特殊的格式来描述复杂的参数。

由于 dbus 规范不是用 Python 编写的,因此要找出您确切必须传递的参数结构是一项艰巨的任务。

在我的示例中,我想调用对象的Mount()方法Filesystem。这个方法得到了签名a{sv}

Mount()是这样定义的

org.freedesktop.UDisks2.Filesystem
...
The Mount() method
Mount (IN  a{sv} options,
       OUT s     mount_path);

来源:http ://storaged.org/doc/udisks2-api/latest/gdbus-org.freedesktop.UDisks2.Filesystem.html#gdbus-method-org-freedesktop-UDisks2-Filesystem.Mount

挂载分区的完整代码如下:

bus = dbus.SystemBus()
device = "/org/freedesktop/UDisks2/block_devices/sdi1"
obj = bus.get_object('org.freedesktop.UDisks2', device)
obj.Mount(..., dbus_interface="org.freedesktop.UDisks2.Filesystem")

其中 ... 是有问题的参数。

标签: pythonparameter-passingdbus

解决方案


答案分为不同的层:

  • 参数结构
  • 键名
  • 法律价值

dbus的参数结构在这里定义:https ://dbus.freedesktop.org/doc/dbus-specification.html#type-system

我们从中了解到,这a{sv}是一个包含一个(或多个?)DICT(键值对列表)的数组。键是 STRING,值是 VARIANT,它是前面带有类型代码的任何类型的数据。

值得庆幸的是,我们不必处理低级细节。Python 将处理这个问题。

所以解决方案很简单:

obj.Mount(dict(key="value", key2="value2"), 
dbus_interface="org.freedesktop.UDisks2.Filesystem")

实际的键名在 udisks 文档中定义

IN a{sv} options:   Options - known options (in addition to standard options) 
                    includes fstype (of type 's') and options (of type 's').
    
OUT s mount_path:   The filesystem path where the device was mounted.

来自http://storaged.org/doc/udisks2-api/latest/gdbus-org.freedesktop.UDisks2.Filesystem.html#gdbus-method-org-freedesktop-UDisks2-Filesystem.Mount

而标准选项是指

Option name, Value type, Description
auth.no_user_interaction, 'b', If set to TRUE, then no user interaction will happen when checking if the method call is authorized.

来自http://storaged.org/doc/udisks2-api/latest/udisks-std-options.html

因此,添加我们拥有的键名

obj.Mount(dict(fstype="value", options="value2"), 
dbus_interface="org.freedesktop.UDisks2.Filesystem")

关于,我认为您必须研究这些部分Filesystem Independent Mount OptionsFilesystem Dependent Mount Options来自https://linux.die.net/man/8/mount

所以最终的解决方案看起来像

obj.Mount(dict(fstype="vfat", options="ro"), 
dbus_interface="org.freedesktop.UDisks2.Filesystem")

推荐阅读