Thomas Gläßle avatar Thomas Gläßle committed 74b9f93 Merge

Merge branch 'property-abstraction' into separate-mvc-components

Comments (0)

Files changed (5)

udiskie/automount.py

             self.mounter.add_device(udiskie_device)
         else:
             media_added = False
-            if udiskie_device.has_media() and not last_state.has_media:
+            if udiskie_device.has_media and not last_state.has_media:
                 media_added = True
 
             if media_added and not last_state.mounted:
 
 
     def _store_device_state(self, device):
-        state = DeviceState(device.is_mounted(),
-                            device.has_media())
+        state = DeviceState(device.is_mounted,
+                            device.has_media)
         self.last_device_state[device.device_path] = state
 
     def _remove_device_state(self, device):

udiskie/device.py

 import logging
 import os
+import dbus
 
 DBUS_PROPS_INTERFACE = 'org.freedesktop.DBus.Properties'
 UDISKS_INTERFACE = 'org.freedesktop.UDisks'
 UDISKS_OBJECT = 'org.freedesktop.UDisks'
 UDISKS_OBJECT_PATH = '/org/freedesktop/UDisks'
 
+class DbusProperties:
+    """
+    Dbus property map abstraction.
+
+    Properties of the object can be accessed as attributes.
+
+    """
+    def __init__(self, dbus_object, interface):
+        """Initialize a proxy object with standard dbus property interface."""
+        self.__proxy = dbus.Interface(
+                dbus_object,
+                dbus_interface=DBUS_PROPS_INTERFACE)
+        self.__interface = interface
+
+    def __getattr__(self, property):
+        """Retrieve the property via the dbus proxy."""
+        return self.__proxy.Get(self.__interface, property)
+
 class Device:
     def __init__(self, bus, device_path):
         self.log = logging.getLogger('udiskie.device.Device')
         self.bus = bus
         self.device_path = device_path
-        self.device = self.bus.get_object(UDISKS_OBJECT, device_path)
+        device_object = self.bus.get_object(UDISKS_OBJECT, device_path)
+        self.property = DbusProperties(device_object, UDISKS_DEVICE_INTERFACE)
+        self.method = dbus.Interface(device_object, UDISKS_DEVICE_INTERFACE)
 
     def __str__(self):
         return self.device_path
 
-    def _get_property(self, property):
-        return self.device.Get(UDISKS_DEVICE_INTERFACE, property,
-                               dbus_interface=DBUS_PROPS_INTERFACE)
+    # properties
+    @property
+    def partition_slave(self):
+        """Get the partition slave."""
+        return self.property.PartitionSlave
 
-    def partition_slave(self):
-        return self._get_property('PartitionSlave')
+    @property
+    def is_partition_table(self):
+        """Check if the device is a partition table."""
+        return self.property.DeviceIsPartitionTable
 
-    def is_partition_table(self):
-        return self._get_property('DeviceIsPartitionTable')
+    @property
+    def is_systeminternal(self):
+        """Check if the device is internal."""
+        return self.property.DeviceIsSystemInternal
 
-    def is_systeminternal(self):
-        return self._get_property('DeviceIsSystemInternal')
-
+    @property
     def is_handleable(self):
-        """Should this device be handled by udiskie?
+        """
+        Should this device be handled by udiskie?
 
         Currently this just means that the device is removable and holds a
-        filesystem."""
-        return (self.is_filesystem() or self.is_crypto()) and not self.is_systeminternal()
+        filesystem or the device is a LUKS encrypted volume.
 
+        """
+        return (self.is_filesystem or self.is_crypto) and not self.is_systeminternal
+
+    @property
     def is_mounted(self):
-        return self._get_property('DeviceIsMounted')
+        """Check if the device is mounted."""
+        return self.property.DeviceIsMounted
 
+    @property
     def is_unlocked(self):
         """Check if device is already unlocked."""
-        return self.is_luks() and self.luks_cleartext_holder()
+        return self.is_luks and self.luks_cleartext_holder
 
+    @property
     def mount_paths(self):
-        raw_paths = self._get_property('DeviceMountPaths')
+        raw_paths = self.property.DeviceMountPaths
         return [os.path.normpath(path) for path in raw_paths]
 
+    @property
     def device_file(self):
-        return os.path.normpath(self._get_property('DeviceFile'))
+        return os.path.normpath(self.property.DeviceFile)
 
+    @property
     def is_filesystem(self):
-        return self._get_property('IdUsage') == 'filesystem'
+        return self.id_usage == 'filesystem'
 
+    @property
     def is_crypto(self):
-        return self._get_property('IdUsage') == 'crypto'
+        return self.id_usage == 'crypto'
 
+    @property
     def is_luks(self):
-        return self._get_property('DeviceIsLuks')
+        return self.property.DeviceIsLuks
 
+    @property
     def is_luks_cleartext(self):
         """Check whether this is a luks cleartext device."""
-        return self._get_property('DeviceIsLuksCleartext')
+        return self.property.DeviceIsLuksCleartext
 
+    @property
     def luks_cleartext_slave(self):
         """Get luks crypto device."""
-        return self._get_property('LuksCleartextSlave')
+        return self.property.LuksCleartextSlave
 
+    @property
     def luks_cleartext_holder(self):
         """Get unlocked luks cleartext device."""
-        return self._get_property('LuksHolder')
+        return self.property.LuksHolder
 
+    @property
     def is_luks_cleartext_slave(self):
         """Check whether the luks device is currently in use."""
-        if not self.is_luks():
+        if not self.is_luks:
             return False
         for device in get_all(self.bus):
-            if (not device.is_filesystem() or device.is_mounted()) and (
-                    device.is_luks_cleartext() and
-                    device.luks_cleartext_slave() == self.device_path):
+            if (not device.is_filesystem or device.is_mounted) and (
+                    device.is_luks_cleartext and
+                    device.luks_cleartext_slave == self.device_path):
                 return True
         return False
 
+    @property
     def has_media(self):
-        return self._get_property('DeviceIsMediaAvailable')
+        return self.property.DeviceIsMediaAvailable
 
+    @property
     def id_type(self):
-        return self._get_property('IdType')
+        return self.property.IdType
 
+    @property
+    def id_usage(self):
+        return self.property.IdUsage
+
+    @property
     def id_uuid(self):
-        return self._get_property('IdUuid')
+        return self.property.IdUuid
 
+    # methods
     def mount(self, filesystem, options):
-        self.device.FilesystemMount(filesystem, options,
-                                    dbus_interface=UDISKS_DEVICE_INTERFACE)
+        self.method.FilesystemMount(filesystem, options)
 
     def unmount(self):
-        self.device.FilesystemUnmount([], dbus_interface=UDISKS_DEVICE_INTERFACE)
+        self.method.FilesystemUnmount([])
 
     def lock(self, options):
         """Lock Luks device."""
-        return self.device.LuksLock(options,
-                                    dbus_interface=UDISKS_DEVICE_INTERFACE)
+        return self.method.LuksLock(options)
 
     def unlock(self, password, options):
         """Unlock Luks device."""
-        return self.device.LuksUnlock(password, options,
-                               dbus_interface=UDISKS_DEVICE_INTERFACE)
+        return self.method.LuksUnlock(password, options)
 
 
 
 def get_device(bus, path):
     logger = logging.getLogger('udiskie.device.get_device')
     for device in get_all(bus):
-        if os.path.samefile(path, device.device_file()):
+        if os.path.samefile(path, device.device_file):
             return device
-        for p in device.mount_paths():
+        for p in device.mount_paths:
             if os.path.samefile(path, p):
                 return device
     logger.warn('Device not found: %s' % path)
 
     def get_mount_options(self, device):
         device_info = {
-            'fstype' : device.id_type(),
-            'uuid' : device.id_uuid().lower()
+            'fstype' : device.id_type,
+            'uuid' : device.id_uuid.lower()
         }
 
         mount_options = set()
         The special value `None` means unknown/unreliable.
 
         """
-        if not device.is_handleable() or not device.is_filesystem():
+        if not device.is_handleable or not device.is_filesystem:
             self.log.debug('skipping unhandled device %s' % (device,))
             return False
-        if device.is_mounted():
+        if device.is_mounted:
             self.log.debug('skipping mounted device %s' % (device,))
             return False
 
-        fstype = str(device.id_type())
+        fstype = str(device.id_type)
         options = self.filters.get_mount_options(device)
 
         S = 'attempting to mount device %s (%s:%s)'
                                                 device, dbus_err))
             return None
 
-        mount_paths = ', '.join(device.mount_paths())
-        self.notify('mount')(device.device_file(), mount_paths)
+        mount_paths = ', '.join(device.mount_paths)
+        self.notify('mount')(device.device_file, mount_paths)
 
         return True
 
         The special value `None` means unknown/unreliable.
 
         """
-        if not device.is_handleable() or not device.is_crypto():
+        if not device.is_handleable or not device.is_crypto:
             self.log.debug('skipping unhandled device %s' % (device,))
             return False
-        if device.is_unlocked():
+        if device.is_unlocked:
             self.log.debug('skipping unlocked device %s' % (device,))
             return False
 
             device.unlock(password, [])
             holder_dev = udiskie.device.Device(
                     self.bus,
-                    device.luks_cleartext_holder())
-            holder_path = holder_dev.device_file()
+                    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
 
-        self.notify('unlock')(device.device_file())
+        self.notify('unlock')(device.device_file)
         return True
 
     def add_device(self, device):
         """Mount or unlock the device depending on its type."""
-        if not device.is_handleable():
+        if not device.is_handleable:
             self.log.debug('skipping unhandled device %s' % (device,))
             return False
-        if device.is_filesystem():
+        if device.is_filesystem:
             return self.mount_device(device)
-        elif device.is_crypto():
+        elif device.is_crypto:
             return self.unlock_device(device)
 
     def mount_present_devices(self):

udiskie/umount.py

 
     """
     logger = logging.getLogger('udiskie.umount.unmount_device')
-    if not device.is_handleable() or not device.is_filesystem():
+    if not device.is_handleable or not device.is_filesystem:
         logger.debug('skipping unhandled device %s' % (device,))
         return False
-    if not device.is_mounted():
+    if not device.is_mounted:
         logger.debug('skipping unmounted device %s' % (device,))
         return False
     try:
         logger.error('failed to unmount device %s: %s' % (device,
                                                             dbus_err))
         return None
-    notify('umount')(device.device_file())
+    notify('umount')(device.device_file)
     return True
 
 def lock_device(device, notify):
 
     """
     logger = logging.getLogger('udiskie.umount.lock_device')
-    if not device.is_handleable() or not device.is_crypto():
+    if not device.is_handleable or not device.is_crypto:
         logger.debug('skipping unhandled device %s' % (device,))
         return False
-    if not device.is_unlocked():
+    if not device.is_unlocked:
         logger.debug('skipping locked device %s' % (device,))
         return False
     try:
     except dbus.exceptions.DBusException, dbus_err:
         logger.error('failed to lock device %s: %s' % (device, dbus_err))
         return None
-    notify('lock')(device.device_file())
+    notify('lock')(device.device_file)
     return True
 
 def remove_device(device, notify):
     """Unmount or lock the device depending on device type."""
     logger = logging.getLogger('udiskie.umount.remove_device')
-    if not device.is_handleable():
+    if not device.is_handleable:
         logger.debug('skipping unhandled device %s' % (device,))
         return False
-    if device.is_filesystem():
+    if device.is_filesystem:
         return unmount_device(device, notify)
-    elif device.is_crypto():
+    elif device.is_crypto:
         return lock_device(device, notify)
 
 def lock_slave(device, notify):
 
     """
     logger = logging.getLogger('udiskie.umount.lock_slave')
-    if not device.is_luks_cleartext():
+    if not device.is_luks_cleartext:
         logger.debug('skipping non-luks-cleartext device %s' % (device,))
         return False
-    slave_path = device.luks_cleartext_slave()
+    slave_path = device.luks_cleartext_slave
     slave = udiskie.device.Device(device.bus, slave_path)
-    if slave.is_luks_cleartext_slave():
+    if slave.is_luks_cleartext_slave:
         return False
     return lock_device(slave, notify)
 
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.