Thomas Gläßle avatar Thomas Gläßle committed ea63d10

Exhibit device properties as object properties in internal API

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

     def __str__(self):
         return self.device_path
 
+    # properties
+    @property
     def partition_slave(self):
         """Get the partition slave."""
         return self.property.PartitionSlave
 
+    @property
     def is_partition_table(self):
         """Check if the device is a partition table."""
         return self.property.DeviceIsPartitionTable
 
+    @property
     def is_systeminternal(self):
         """Check if the device is internal."""
         return self.property.DeviceIsSystemInternal
 
+    @property
     def is_handleable(self):
         """
         Should this device be handled by udiskie?
         filesystem or the device is a LUKS encrypted volume.
 
         """
-        return (self.is_filesystem() or self.is_crypto()) and not self.is_systeminternal()
+        return (self.is_filesystem or self.is_crypto) and not self.is_systeminternal
 
+    @property
     def is_mounted(self):
         """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.property.DeviceMountPaths
         return [os.path.normpath(path) for path in raw_paths]
 
+    @property
     def device_file(self):
         return os.path.normpath(self.property.DeviceFile)
 
+    @property
     def is_filesystem(self):
-        return self.property.IdUsage == 'filesystem'
+        return self.id_usage == 'filesystem'
 
+    @property
     def is_crypto(self):
-        return self.property.IdUsage == 'crypto'
+        return self.id_usage == 'crypto'
 
+    @property
     def is_luks(self):
         return self.property.DeviceIsLuks
 
+    @property
     def is_luks_cleartext(self):
         """Check whether this is a luks cleartext device."""
         return self.property.DeviceIsLuksCleartext
 
+    @property
     def luks_cleartext_slave(self):
         """Get luks crypto device."""
         return self.property.LuksCleartextSlave
 
+    @property
     def luks_cleartext_holder(self):
         """Get unlocked luks cleartext device."""
         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.property.DeviceIsMediaAvailable
 
+    @property
     def id_type(self):
         return self.property.IdType
 
+    @property
+    def id_usage(self):
+        return self.property.IdUsage
+
+    @property
     def id_uuid(self):
         return self.property.IdUuid
 
+    # methods
     def mount(self, filesystem, options):
         self.method.FilesystemMount(filesystem, 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.