Thomas Gläßle avatar Thomas Gläßle committed 5e3468b

Reallocate state keeping responsibility to automount component

Comments (0)

Files changed (2)

udiskie/automount.py

 import logging
 import gobject
 
+import udiskie
+import udiskie.device
+import udiskie.mount
 import udiskie.notify
-import udiskie.mount
-import udiskie.device
 
+class DeviceState:
+    def __init__(self, mounted, has_media):
+        self.mounted = mounted
+        self.has_media = has_media
 
-class AutoMounter(udiskie.mount.Mounter):
+class AutoMounter:
 
     def __init__(self, bus=None, filter_file=None, notify=None, prompt=None):
-        udiskie.mount.Mounter.__init__(self, bus, filter_file, notify, prompt)
         self.log = logging.getLogger('udiskie.mount.AutoMounter')
+        self.mounter = udiskie.mount.Mounter(bus, filter_file, notify, prompt)
+        self.bus = self.mounter.bus
+
+        self.last_device_state = {}
 
         self.bus.add_signal_receiver(self.device_added,
                                      signal_name='DeviceAdded',
     def device_added(self, device):
         self.log.debug('device added: %s' % (device,))
         udiskie_device = udiskie.device.Device(self.bus, device)
-        # Since the device just appeared we don't want the old state.
-        self._remove_device_state(udiskie_device)
-        self.add_device(udiskie_device)
+        self._store_device_state(udiskie_device)
+        self.mounter.add_device(udiskie_device)
 
     def device_removed(self, device):
         self.log.debug('device removed: %s' % (device,))
 
         if not last_state:
             # First time we saw the device, try to mount it.
-            self.add_device(udiskie_device)
+            self.mounter.add_device(udiskie_device)
         else:
             media_added = False
             if udiskie_device.has_media() and not last_state.has_media:
 
             if media_added and not last_state.mounted:
                 # Wasn't mounted before, but it has new media now.
-                self.add_device(udiskie_device)
+                self.mounter.add_device(udiskie_device)
 
         self._store_device_state(udiskie_device)
 
 
+    def _store_device_state(self, device):
+        state = DeviceState(device.is_mounted(),
+                            device.has_media())
+        self.last_device_state[device.device_path] = state
+
+    def _remove_device_state(self, device):
+        if device.device_path in self.last_device_state:
+            del self.last_device_state[device.device_path]
+
+    def _get_device_state(self, device):
+        return self.last_device_state.get(device.device_path)
+
+
 
 def cli(args):
     import udiskie.mount
         mounter = AutoMounter(
                 bus=None, filter_file=options.filters,
                 notify=notify, prompt=prompt)
-        mounter.mount_present_devices()
+        mounter.mounter.mount_present_devices()
         return gobject.MainLoop().run()
 
 
 import udiskie.notify
 import udiskie.prompt
 
-class DeviceState:
-    def __init__(self, mounted, has_media):
-        self.mounted = mounted
-        self.has_media = has_media
-
 
 class Mounter:
     CONFIG_PATH = 'udiskie/filters.conf'
 
     def __init__(self, bus=None, filter_file=None, notify=None, prompt=None):
         self.log = logging.getLogger('udiskie.mount.Mounter')
-        self.last_device_state = {}
 
         if not bus:
             from dbus.mainloop.glib import DBusGMainLoop
             self.log.debug('skipping mounted device %s' % (device,))
             return False
 
+        fstype = str(device.id_type())
+        options = self.filters.get_mount_options(device)
+
+        S = 'attempting to mount device %s (%s:%s)'
+        self.log.info(S % (device, fstype, options))
+
         try:
-            fstype = str(device.id_type())
-            options = self.filters.get_mount_options(device)
+            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
 
-            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())
-            self.notify('mount')(device.device_file(), mount_paths)
-        finally:
-            self._store_device_state(device)
+        mount_paths = ', '.join(device.mount_paths())
+        self.notify('mount')(device.device_file(), mount_paths)
 
         return True
 
         elif device.is_crypto():
             return self.unlock_device(device)
 
-    def _store_device_state(self, device):
-        state = DeviceState(device.is_mounted(),
-                            device.has_media())
-        self.last_device_state[device.device_path] = state
-
-    def _remove_device_state(self, device):
-        if device.device_path in self.last_device_state:
-            del self.last_device_state[device.device_path]
-
-    def _get_device_state(self, device):
-        return self.last_device_state.get(device.device_path)
-
     def mount_present_devices(self):
         """Mount handleable devices that are already present."""
         for device in udiskie.device.get_all(self.bus):
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.