簡體   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