Byron Clark avatar Byron Clark committed 8427de6

Move udiskie to the udiskie infrastructure.

Comments (0)

Files changed (4)

-- Move udiskie to the udiskie package infrastructure
 - Scan disks on startup
 #! /usr/bin/env python
 
-import dbus
-from dbus.mainloop.glib import DBusGMainLoop
-import gobject
+if __name__ == '__main__':
+    import sys
+    import udiskie.mount
 
-DBusGMainLoop(set_as_default=True)
-system_bus = dbus.SystemBus()
-
-def device_added(device):
-    print 'device added: %s' % (device,)
-    device_object = system_bus.get_object('org.freedesktop.UDisks', device)
-    properties_iface = dbus.Interface(device_object,
-                                      dbus_interface='org.freedesktop.DBus.Properties')
-    id_usage = properties_iface.Get('org.freedesktop.UDisks.Device', 'IdUsage')
-    if id_usage == 'filesystem':
-        filesystem = properties_iface.Get('org.freedsktop.UDisks.Device',
-                                          'IdType')
-        # TOOD - removable?
-        mount_path = device_object.FilesystemMount(filesystem, [],
-                                                   dbus_interface='org.freedesktop.UDisks.Device')
-        print 'mounted at: %s' % (mount_path,)
-
-def device_changed(device):
-    print 'device changed: %s' % (device,)
-
-def device_removed(device):
-    print 'device removed: %s' % (device,)
-
-system_bus.add_signal_receiver(device_added,
-                               signal_name='DeviceAdded',
-                               bus_name='org.freedesktop.UDisks')
-system_bus.add_signal_receiver(device_changed,
-                               signal_name='DeviceChanged',
-                               bus_name='org.freedesktop.UDisks')
-system_bus.add_signal_receiver(device_removed,
-                               signal_name='DeviceRemoved',
-                               bus_name='org.freedesktop.UDisks')
-
-loop = gobject.MainLoop()
-loop.run()
+    sys.exit(udiskie.mount.cli(sys.argv[1:]))

udiskie/device.py

         Currently this just means that the device is removable or that the
         device it is part of is removable."""
 
-        if self.is_removable():
+        if self.is_removable() and self.is_filesystem():
             return True
         else:
             if self.is_partition():
     def device_file(self):
         return self._get_property('DeviceFile')
 
+    def is_filesystem(self):
+        return self._get_property('IdUsage') == 'filesystem'
+
+    def id_type(self):
+        return self._get_property('IdType')
+
+    def mount(self, filesystem, options):
+        self.device.FilesystemMount(filesystem, options,
+                                    dbus_interface=UDISKS_DEVICE_INTERFACE)
+
     def unmount(self):
         self.device.FilesystemUnmount([], dbus_interface=UDISKS_DEVICE_INTERFACE)
 
+import logging
+import optparse
+
+import dbus
+import gobject
+
+import udiskie.device
+
+class AutoMounter:
+    def __init__(self, bus=None):
+        self.log = logging.getLogger('udiskie.mount.AutoMounter')
+
+        if not bus:
+            from dbus.mainloop.glib import DBusGMainLoop
+            DBusGMainLoop(set_as_default=True)
+            self.bus = dbus.SystemBus()
+        else:
+            self.bus = bus
+
+        self.bus.add_signal_receiver(self.device_added,
+                                     signal_name='DeviceAdded',
+                                     bus_name='org.freedesktop.UDisks')
+
+    def device_added(self, device):
+        self.log.debug('device added: %s' % (device,))
+        udevice = udiskie.device.Device(self.bus, device)
+        if udevice.is_handleable():
+            filesystem = str(udevice.id_type())
+            options = []
+            udevice.mount(filesystem, options)
+
+
+def cli(args):
+    parser = optparse.OptionParser()
+    parser.add_option('-v', '--verbose', action='store_true',
+                      dest='verbose', default=False,
+                      help='verbose output')
+    (options, args) = parser.parse_args(args)
+
+    log_level = logging.INFO
+    if options.verbose:
+        log_level = logging.DEBUG
+    logging.basicConfig(level=log_level, format='%(message)s')
+
+    mounter = AutoMounter()
+    return gobject.MainLoop().run()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.