pythongio

How to unmount based on volume type using Python GIO library


I am trying to mimic the behaviour of shell command gio mount which unmounts all gphoto2 cameras

gio mount -s gphoto2

How to detect if volume is gphoto2? I tried getting volume type but there doesn't seem to be variable that holds it,

from gi.repository import Gio, GObject

def main():
    mo = Gio.MountOperation()
    mo.set_anonymous(True)

    vl = Gio.VolumeMonitor.get()

    loop = GObject.MainLoop()

    for v in vl.get_mounts():
        print(dir(v))
        print(v.get_name(), v.get_uuid(), v.g_type_instance, v.get_drive(),
              v.get_volume(),
              v.get_root(),
              v.get_drive(),
              v.get_sort_key(),
              v.get_uuid())

Solution

  • Get the root of the mount (https://developer.gnome.org/gio/stable/GMount.html#g-mount-get-root) and check its URI scheme (https://developer.gnome.org/gio/stable/GFile.html#g-file-has-uri-scheme). If the scheme is gphoto2, you can unmount it.

    See the C implementation of gio mount -s.