Thomas Gläßle avatar Thomas Gläßle committed 7796d92

Move Mounter.* methods to udiskie.mount.* module level functions

This is more natural since these functions have no side effects inside
the scope of the Mounter class. The Mounter class is kept however as a
utility to remember bus, prompt and filter.

For useful diffstat compare with

git --ignore-all-space

Comments (0)

Files changed (2)

 
     # mount all present devices
     if options.all:
-        mounter.mount_present_devices()
+        mounter.mount_all()
 
     # only mount the desired devices
     elif len(posargs) > 0:
 
     # run in daemon mode
     elif run_daemon:
-        mounter.mount_present_devices()
+        mounter.mount_all()
         return daemon.run()
 
     # print command line options
 """
 Udiskie mount utilities.
 """
-__all__ = ['Mounter']
+__all__ = [
+    'mount_device', 'unlock_device', 'add_device',
+    'mount_all',
+    'Mounter']
 
 import logging
 import dbus
 
 import udiskie.device
 
+
+def mount_device(device, filter=None):
+    """
+    Mount the device if not already mounted.
+
+    Return value indicates whether an action was performed successfully.
+    The special value `None` means unknown/unreliable.
+
+    """
+    log = logging.getLogger('udiskie.mount.mount_device')
+    if not device.is_handleable or not device.is_filesystem:
+        log.debug('skipping unhandled device %s' % (device,))
+        return False
+    if device.is_mounted:
+        log.debug('skipping mounted device %s' % (device,))
+        return False
+
+    fstype = str(device.id_type)
+    options = filter.get_mount_options(device) if filter else []
+
+    S = 'attempting to mount device %s (%s:%s)'
+    log.info(S % (device, fstype, options))
+
+    try:
+        device.mount(fstype, options)
+        log.info('mounted device %s' % (device,))
+    except dbus.exceptions.DBusException, dbus_err:
+        log.error('failed to mount device %s: %s' % (
+                                            device, dbus_err))
+        return None
+
+    mount_paths = ', '.join(device.mount_paths)
+    return True
+
+def unlock_device(device, prompt):
+    """
+    Unlock the device if not already unlocked.
+
+    Return value indicates whether an action was performed successfully.
+    The special value `None` means unknown/unreliable.
+
+    """
+    log = logging.getLogger('udiskie.mount.unlock_device')
+    if not device.is_handleable or not device.is_crypto:
+        log.debug('skipping unhandled device %s' % (device,))
+        return False
+    if device.is_unlocked:
+        log.debug('skipping unlocked device %s' % (device,))
+        return False
+
+    # prompt user for password
+    password = prompt and prompt(
+            'Enter password for %s:' % (device,),
+            'Unlock encrypted device')
+    if password is None:
+        return False
+
+    # unlock device
+    log.info('attempting to unlock device %s' % (device,))
+    try:
+        device.unlock(password, [])
+        holder_dev = udiskie.device.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, dbus_err:
+        log.error('failed to unlock device %s:\n%s'
+                                    % (device, dbus_err))
+        return None
+    return True
+
+def add_device(device, filter=None, prompt=None):
+    """Mount or unlock the device depending on its type."""
+    log = logging.getLogger('udiskie.mount.add_device')
+    if not device.is_handleable:
+        log.debug('skipping unhandled device %s' % (device,))
+        return False
+    if device.is_filesystem:
+        return mount_device(device, filter)
+    elif device.is_crypto:
+        return unlock_device(device, prompt)
+
+def mount_all(bus=None, filter=None, prompt=None):
+    """Mount handleable devices that are already present."""
+    bus = bus or dbus.SystemBus()
+    for device in udiskie.device.get_all_handleable(bus):
+        add_device(device, filter, prompt)
+
+
 class Mounter:
+    """
+    Mount utility.
 
+    Calls the global functions and remembers bus, filter and prompt.
+
+    """
     def __init__(self, bus, filter=None, prompt=None):
-        self.log = logging.getLogger('udiskie.mount.Mounter')
         self.bus = bus or dbus.SystemBus()
+        self.filter = filter
         self.prompt = prompt
-        self.filter = filter
 
-    def mount_device(self, device):
-        """
-        Mount the device if not already mounted.
+    def mount_device(self, device, filter=None):
+        return mount_device(device, filter=filter or self.filter)
 
-        Return value indicates whether an action was performed successfully.
-        The special value `None` means unknown/unreliable.
+    def unlock_device(self, device, prompt=None):
+        return mount_device(device, filter=prompt or self.prompt)
 
-        """
-        if not device.is_handleable or not device.is_filesystem:
-            self.log.debug('skipping unhandled device %s' % (device,))
-            return False
-        if device.is_mounted:
-            self.log.debug('skipping mounted device %s' % (device,))
-            return False
+    def add_device(self, device, filter=None, prompt=None):
+        return add_device(
+                device,
+                filter=filter or self.filter, 
+                prompt=prompt or self.prompt)
 
-        fstype = str(device.id_type)
-        options = self.filter.get_mount_options(device) if self.filter else []
+    def mount_all(self, filter=None, prompt=None):
+        return mount_all(
+                self.bus,
+                filter=filter or self.filter,
+                prompt=prompt or self.prompt)
 
-        S = 'attempting to mount device %s (%s:%s)'
-        self.log.info(S % (device, fstype, options))
 
-        try:
-            device.mount(fstype, options)
-            self.log.info('mounted device %s' % (device,))
-        except dbus.exceptions.DBusException, dbus_err:
-            self.log.error('failed to mount device %s: %s' % (
-                                                device, dbus_err))
-            return None
-
-        mount_paths = ', '.join(device.mount_paths)
-        return True
-
-    def unlock_device(self, device):
-        """
-        Unlock the device if not already unlocked.
-
-        Return value indicates whether an action was performed successfully.
-        The special value `None` means unknown/unreliable.
-
-        """
-        if not device.is_handleable or not device.is_crypto:
-            self.log.debug('skipping unhandled device %s' % (device,))
-            return False
-        if device.is_unlocked:
-            self.log.debug('skipping unlocked device %s' % (device,))
-            return False
-
-        # prompt user for password
-        password = self.prompt and self.prompt(
-                'Enter password for %s:' % (device,),
-                'Unlock encrypted device')
-        if password is None:
-            return False
-
-        # unlock device
-        self.log.info('attempting to unlock device %s' % (device,))
-        try:
-            device.unlock(password, [])
-            holder_dev = udiskie.device.Device(
-                    self.bus,
-                    device.luks_cleartext_holder)
-            holder_path = holder_dev.device_file
-            self.log.info('unlocked device %s on %s' % (device, holder_path))
-        except dbus.exceptions.DBusException, dbus_err:
-            self.log.error('failed to unlock device %s:\n%s'
-                                        % (device, dbus_err))
-            return None
-        return True
-
-    def add_device(self, device):
-        """Mount or unlock the device depending on its type."""
-        if not device.is_handleable:
-            self.log.debug('skipping unhandled device %s' % (device,))
-            return False
-        if device.is_filesystem:
-            return self.mount_device(device)
-        elif device.is_crypto:
-            return self.unlock_device(device)
-
-    def mount_present_devices(self):
-        """Mount handleable devices that are already present."""
-        for device in udiskie.device.get_all_handleable(self.bus):
-            self.add_device(device)
-
-
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.