繁体   English   中英

如何从 python 将 'a{sv}' dbus 签名传递给 udisks2.Mount()?

[英]how to pass 'a{sv}' dbus signature to udisks2.Mount() from python?

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

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

在我的示例中,我想调用Filesystem object 的Mount()方法。 这个方法得到了签名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")

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

答案分为不同的层:

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM