python - 如何从python将'a {sv}' dbus签名传递给udisks2.Mount()?
问题描述
dbus
api 使用一种特殊的格式来描述复杂的参数。
由于 dbus 规范不是用 Python 编写的,因此要找出您确切必须传递的参数结构是一项艰巨的任务。
在我的示例中,我想调用对象的Mount()
方法Filesystem
。这个方法得到了签名a{sv}
。
Mount()
是这样定义的
org.freedesktop.UDisks2.Filesystem
...
The Mount() method
Mount (IN a{sv} options,
OUT s mount_path);
挂载分区的完整代码如下:
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.
而标准选项是指
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 Options
并Filesystem Dependent Mount Options
来自https://linux.die.net/man/8/mount
所以最终的解决方案看起来像
obj.Mount(dict(fstype="vfat", options="ro"),
dbus_interface="org.freedesktop.UDisks2.Filesystem")
推荐阅读
- c++ - 如何使用lldb列出每个线程所在的函数?
- python - 如何仅从 boto3 cloudformation describe_stack API 获取 OutputKey?
- c# - C# 托管服务控制台应用程序的健康检查
- c# - UWP 应用程序的非托管内存分析?
- android - Android Toolbar 将 MenuItem 的图标移动到右侧
- angular - 错误:未找到集合,同时使用 in-memory-web-api(不使用任何实时 api 或 json 数据)
- html - 水平增长的列表,元素间的分隔符,没有前置元素的项目符号,没有垂直对齐。如何?
- python - ValueError:找不到资源“C:\Users\loginname\Desktop\2020\chatbot\.data\data.json”
- javascript - 使用 $.append 时的 jQuery 脚本标签执行顺序
- flutter - 在 Flutter 中如何创建数据库和搜索功能?