Thomas Gläßle avatar Thomas Gläßle committed 2da3db7

Remove hardcoding udiskie.udisks module as a dependency

Comments (0)

Files changed (4)

 import udiskie.notify
 import udiskie.automount
 import udiskie.daemon
+import udiskie.common
 
 
 CONFIG_PATH = 'udiskie/filters.conf'
         DBusGMainLoop(set_as_default=True)
     bus = dbus.SystemBus()
 
+    # for now: just use the default udisks
+    udisks = udiskie.common.get_udisks()
+
     # create a mounter
     prompt = udiskie.prompt.password(options.password_prompt)
     filter = load_filter(options.filters)
-    mounter = udiskie.mount.Mounter(bus=bus, filter=filter, prompt=prompt)
+    mounter = udiskie.mount.Mounter(bus=bus, filter=filter, prompt=prompt, udisks=udisks)
 
     # run udiskie daemon if needed
     if run_daemon:
-        daemon = udiskie.daemon.Daemon(bus)
+        daemon = udiskie.daemon.Daemon(bus, udisks=udisks)
     if run_daemon and not options.suppress_notify:
         notify = udiskie.notify.Notify('udiskie.mount')
         notify.connect(daemon)
         parser.print_usage()
         return 1
 
+    # for now: use udisks v1 service
+    udisks = udiskie.common.get_udisks()
+
     if options.all:
-        unmounted = udiskie.mount.unmount_all(bus=bus)
+        unmounted = udiskie.mount.unmount_all(bus=bus, udisks=udisks)
     else:
         unmounted = []
         for path in posargs:
-            device = udiskie.mount.unmount(os.path.normpath(path), bus=bus)
+            device = udiskie.mount.unmount(os.path.normpath(path), bus=bus, udisks=udisks)
             if device:
                 unmounted.append(device)
 
     # automatically lock unused luks slaves of unmounted devices
     for device in unmounted:
-        udiskie.mount.lock_slave(device)
+        udiskie.mount.lock_slave(device, udisks=udisks)
 

udiskie/common.py

 """
 Common utilities.
 """
-__all__ = ['Properties']
+__all__ = ['Properties', 'get_udisks', 'default_udisks']
 import dbus
 
+DBUS_PROPS_INTERFACE = 'org.freedesktop.DBus.Properties'
+default_udisks = 'udiskie.udisks'
 
-DBUS_PROPS_INTERFACE = 'org.freedesktop.DBus.Properties'
+
+def get_udisks():
+    import importlib
+    return importlib.import_module(default_udisks)
 
 class Properties:
     """

udiskie/daemon.py

 
 import sys
 
-from udiskie.udisks import Device, get_all_handleable
-
 
 class DeviceState:
     """
     `disconnect` can be used to add or remove event handlers.
 
     """
-    def __init__(self, bus):
+    def __init__(self, bus, udisks):
         """
         Initialize object and start listening to udisks events.
         """
         self.log = logging.getLogger('udiskie.daemon.Daemon')
         self.bus = bus
         self.state = {}
+        self.udisks = udisks
 
         self.event_handlers = {
             'device_added': [],
             'device_changed': [self.on_device_changed]
         }
 
-        for device in get_all_handleable(bus):
+        for device in self.udisks.get_all_handleable(bus):
             self._store_device_state(device)
 
         self.bus.add_signal_receiver(
     # udisks event listeners
     def _device_added(self, device_name):
         try:
-            udevice = Device(self.bus, device_name)
+            udevice = self.udisks.Device(self.bus, device_name)
             if not udevice.is_handleable:
                 return
             self._store_device_state(udevice)
 
     def _device_changed(self, device_name):
         try:
-            udevice = Device(self.bus, device_name)
+            udevice = self.udisks.Device(self.bus, device_name)
             if not udevice.is_handleable:
                 return
             old_state = self._get_device_state(udevice)
     'unlock_device', 'lock_device',
     'add_device', 'remove_device',
     'mount_all', 'unmount_all',
-    'unmount',
-    'lock_luks_slave',
+    'mount', 'unmount',
+    'mount_holder', 'lock_slave',
     'Mounter']
 
 import sys
 import logging
 import dbus
 
-import udiskie.udisks
+from udiskie.common import get_udisks
 
 
 # mount/unmount
 
 
 # unlock/lock (LUKS)
-def unlock_device(device, prompt):
+def unlock_device(device, prompt, udisks=None):
     """
     Unlock the device if not already unlocked.
 
     log.info('attempting to unlock device %s' % (device,))
     try:
         device.unlock(password, [])
-        holder_dev = udiskie.udisks.Device(
-                device.bus,
-                device.luks_cleartext_holder)
+        udisks = udisks or get_udisks()
+        holder_dev = udisks.Device(device.bus, device.luks_cleartext_holder)
         holder_path = holder_dev.device_file
         log.info('unlocked device %s on %s' % (device, holder_path))
     except dbus.exceptions.DBusException:
 
 
 # add/remove (unlock/lock or mount/unmount)
-def add_device(device, filter=None, prompt=None):
+def add_device(device, filter=None, prompt=None, udisks=None):
     """Mount or unlock the device depending on its type."""
     log = logging.getLogger('udiskie.mount.add_device')
     if not device.is_handleable:
     if device.is_filesystem:
         return mount_device(device, filter)
     elif device.is_crypto:
-        return unlock_device(device, prompt)
+        return unlock_device(device, prompt, udisks=udisks)
 
 def remove_device(device):
     """Unmount or lock the device depending on device type."""
         return lock_device(device)
 
 # mount_all/unmount_all
-def mount_all(bus=None, filter=None, prompt=None):
+def mount_all(bus=None, filter=None, prompt=None, udisks=None):
     """Mount handleable devices that are already present."""
     bus = bus or dbus.SystemBus()
-    for device in udiskie.udisks.get_all_handleable(bus):
-        add_device(device, filter, prompt)
+    udisks = udisks or get_udisks()
+    for device in udisks.get_all_handleable(bus):
+        add_device(device, filter, prompt, udisks=udisks)
 
-def unmount_all(bus=None):
+def unmount_all(bus=None, udisks=None):
     """Unmount all filesystems handleable by udiskie."""
     unmounted = []
     bus = bus or dbus.SystemBus()
-    for device in udiskie.udisks.get_all_handleable(bus):
+    udisks = udisks or get_udisks()
+    for device in udisks.get_all_handleable(bus):
         if unmount_device(device):
             unmounted.append(device)
     return unmounted
 
 
 # mount a holder/lock a slave
-def mount_holder(device, filter=None, prompt=None):
+def mount_holder(device, filter=None, prompt=None, udisks=None):
     """
     Mount or unlock the holder device of this unlocked LUKS device.
 
     if not device.is_unlocked:
         logger.debug('skipping locked or non-luks device %s' % (device,))
         return False
+    udisks = udisks or get_udisks()
     holder_path = device.luks_cleartext_holder
-    holder = udiskie.udisks.Device(device.bus, holder_path)
-    return add_device(device, filter=filter, prompt=prompt)
+    holder = udisks.Device(device.bus, holder_path)
+    return add_device(device, filter=filter, prompt=prompt, udisks=udisks)
 
-def lock_slave(device):
+def lock_slave(device, udisks=None):
     """
     Lock the luks slave of this device.
 
     if not device.is_luks_cleartext:
         logger.debug('skipping non-luks-cleartext device %s' % (device,))
         return False
+    udisks = udisks or get_udisks()
     slave_path = device.luks_cleartext_slave
-    slave = udiskie.udisks.Device(device.bus, slave_path)
+    slave = udisks.Device(device.bus, slave_path)
     if slave.is_luks_cleartext_slave:
         return False
     return lock_device(slave)
 
 
 # mount/unmount by path
-def mount(path, bus=None, filter=None, prompt=None):
+def mount(path, bus=None, filter=None, prompt=None, udisks=None):
     """
     Mount or unlock a device.
 
     """
     logger = logging.getLogger('udiskie.mount.unmount')
     bus = bus or dbus.SystemBus()
-    device = udiskie.udisks.get_device(bus, path)
+    udisks = udisks or get_udisks()
+    device = udisks.get_device(bus, path)
     if device:
         logger.debug('found device owning "%s": "%s"' % (path, device))
-        if add_device(device, filter=filter, prompt=prompt):
+        if add_device(device, filter=filter, prompt=prompt, udisks=udisks):
             return device
     return None
 
-def unmount(path, bus=None):
+def unmount(path, bus=None, udisks=None):
     """
     Unmount or lock a filesystem
 
     """
     logger = logging.getLogger('udiskie.mount.unmount')
     bus = bus or dbus.SystemBus()
-    device = udiskie.udisks.get_device(bus, path)
+    udisks = udisks or get_udisks()
+    device = udisks.get_device(bus, path)
     if device:
         logger.debug('found device owning "%s": "%s"' % (path, device))
         if remove_device(device):
     """
     Mount utility.
 
-    Calls the global functions and remembers bus, filter and prompt.
+    Calls the global functions and remembers bus, filter and prompt as well
+    as the desired udisks module.
 
     """
-    def __init__(self, bus, filter=None, prompt=None):
+    def __init__(self, bus, filter=None, prompt=None, udisks=None):
         self.bus = bus or dbus.SystemBus()
         self.filter = filter
         self.prompt = prompt
+        self.udisks = udisks
 
     # mount/unmount
     def mount_device(self, device, filter=None):
         return unmount_device(device)
 
     # unlock/lock (LUKS)
-    def unlock_device(self, device, prompt=None):
-        return mount_device(device, filter=prompt or self.prompt)
+    def unlock_device(self, device, prompt=None, udisks=None):
+        return mount_device(
+                device,
+                filter=prompt or self.prompt,
+                udisks=udisks or self.udisks)
     def lock_device(self, device):
         return lock_device(device)
 
     # add/remove (unlock/lock or mount/unmount)
-    def add_device(self, device, filter=None, prompt=None):
+    def add_device(self, device, filter=None, prompt=None, udisks=None):
         return add_device(
                 device,
                 filter=filter or self.filter,
-                prompt=prompt or self.prompt)
+                prompt=prompt or self.prompt,
+                udisks=udisks or self.udisks)
     def remove_device(self, device):
         return remove_device(device)
 
     # mount_all/unmount_all
-    def mount_all(self, filter=None, prompt=None):
+    def mount_all(self, filter=None, prompt=None, udisks=None):
         return mount_all(
                 self.bus,
                 filter=filter or self.filter,
-                prompt=prompt or self.prompt)
-    def unmount_all(self):
-        return unmount_all(self.bus)
+                prompt=prompt or self.prompt,
+                udisks=udisks or self.udisks)
+    def unmount_all(self, udisks=None):
+        return unmount_all(self.bus, udisks=udisks or self.udisks)
 
     # mount/unmount
     def mount(self, path, filter=None, prompt=None):
         return unmount(path, bus=self.bus)
 
     # mount_holder/lock_slave
-    def mount_holder(self, device, filter=None, prompt=None):
+    def mount_holder(self, device, filter=None, prompt=None, udisks=None):
         return mount_holder(
                 device,
                 filter=filter or self.filter,
-                prompt=prompt or self.prompt)
-    def lock_slave(self, device):
-        return lock_slave(device)
+                prompt=prompt or self.prompt,
+                udisks=udisks or self.udisks)
+    def lock_slave(self, device, udisks=None):
+        return lock_slave(device, udisks=udisks or self.udisks)
 
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.