Skip to content
Matthias Braun edited this page Jun 15, 2022 · 1 revision

One frequently asked question is whether udiskie exposes a Python API that can be used inside Python scripts.

Because of the way that udiskie is implemented, there is currently no convenient Python API. This is mostly due to the fact that udiskie is based on a homebrewn async layer. (I would prefer too to have a Python API that is useful for outsiders and may reconsider)

However, udiskie is just a thin client for UDisks which provides a well-documented UDisks2 DBus API. There is also a UDisks2 PyGI binding which provides a higher-level API and allows to avoid a low-level access using a generic DBus library.

The following example shows how to synchronously mount and unmount a device in python using PyGI to access the DBus API:

from gi.repository import Gio

proxy = Gio.DBusProxy.new_for_bus_sync(
    Gio.BusType.SYSTEM,
    Gio.DBusProxyFlags.NONE,
    info=None,
    name='org.freedesktop.UDisks2',
    object_path='/org/freedesktop/UDisks2/block_devices/sdb1',
    interface_name='org.freedesktop.UDisks2.Filesystem',
    cancellable=None,
)

proxy.Mount('(a{sv})', {})
proxy.Unmount('(a{sv})', {})

For more information, see the PyGI Gio documentation. For more examples take a look at the udiskie source itself. In particular, consider the modules udiskie.dbus, udiskie.async_, udiskie.udisks2.