Thomas Gläßle avatar Thomas Gläßle committed ffef904 Merge

Merge branch 'separate-automount' into separate-mvc-components

Comments (0)

Files changed (4)

 
 if __name__ == '__main__':
     import sys
-    import udiskie.mount
+    import udiskie.automount
 
-    sys.exit(udiskie.mount.cli(sys.argv[1:]))
+    sys.exit(udiskie.automount.cli(sys.argv[1:]))

udiskie/automount.py

+import logging
+import gobject
+
+import udiskie
+import udiskie.device
+import udiskie.mount
+import udiskie.notify
+
+class DeviceState:
+    def __init__(self, mounted, has_media):
+        self.mounted = mounted
+        self.has_media = has_media
+
+class AutoMounter:
+
+    def __init__(self, bus=None, filter_file=None, notify=None, prompt=None):
+        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',
+                                     bus_name='org.freedesktop.UDisks')
+        self.bus.add_signal_receiver(self.device_removed,
+                                     signal_name='DeviceRemoved',
+                                     bus_name='org.freedesktop.UDisks')
+        self.bus.add_signal_receiver(self.device_changed,
+                                     signal_name='DeviceChanged',
+                                     bus_name='org.freedesktop.UDisks')
+
+
+    def device_added(self, device):
+        self.log.debug('device added: %s' % (device,))
+        udiskie_device = udiskie.device.Device(self.bus, 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,))
+        self._remove_device_state(udiskie.device.Device(self.bus, device))
+
+    def device_changed(self, device):
+        self.log.debug('device changed: %s' % (device,))
+
+        udiskie_device = udiskie.device.Device(self.bus, device)
+        last_state = self._get_device_state(udiskie_device)
+
+        if not last_state:
+            # First time we saw the device, try to mount it.
+            self.mounter.add_device(udiskie_device)
+        else:
+            media_added = False
+            if udiskie_device.has_media() and not last_state.has_media:
+                media_added = True
+
+            if media_added and not last_state.mounted:
+                # Wasn't mounted before, but it has new media now.
+                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
+    options, posargs = udiskie.mount.option_parser().parse_args(args)
+
+    # invoked as a mount tool
+    if options.all or len(posargs) > 0:
+        return udiskie.mount.cli(args)
+
+    # run as a daemon
+    else:
+        logging.basicConfig(level=options.log_level, format='%(message)s')
+
+        if options.suppress_notify:
+            notify = None
+        else:
+            notify = udiskie.notify.Notify('udiskie.mount')
+
+        import udiskie.prompt
+        prompt = udiskie.prompt.password(options.password_prompt)
+
+        mounter = AutoMounter(
+                bus=None, filter_file=options.filters,
+                notify=notify, prompt=prompt)
+        mounter.mounter.mount_present_devices()
+        return gobject.MainLoop().run()
+
+
 warnings.filterwarnings("ignore", ".*g_object_unref.*", Warning)
 
 import logging
-import optparse
 import os
-
 import dbus
-import gobject
 
 try:
     from xdg.BaseDirectory import xdg_config_home
 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):
             self.add_device(device)
 
 
-class AutoMounter(Mounter):
-
-    def __init__(self, bus=None, filter_file=None, notify=None, prompt=None):
-        Mounter.__init__(self, bus, filter_file, notify, prompt)
-        self.log = logging.getLogger('udiskie.mount.AutoMounter')
-
-        self.bus.add_signal_receiver(self.device_added,
-                                     signal_name='DeviceAdded',
-                                     bus_name='org.freedesktop.UDisks')
-        self.bus.add_signal_receiver(self.device_removed,
-                                     signal_name='DeviceRemoved',
-                                     bus_name='org.freedesktop.UDisks')
-        self.bus.add_signal_receiver(self.device_changed,
-                                     signal_name='DeviceChanged',
-                                     bus_name='org.freedesktop.UDisks')
-
-
-    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)
-
-    def device_removed(self, device):
-        self.log.debug('device removed: %s' % (device,))
-        self._remove_device_state(udiskie.device.Device(self.bus, device))
-
-    def device_changed(self, device):
-        self.log.debug('device changed: %s' % (device,))
-
-        udiskie_device = udiskie.device.Device(self.bus, device)
-        last_state = self._get_device_state(udiskie_device)
-
-        if not last_state:
-            # First time we saw the device, try to mount it.
-            self.add_device(udiskie_device)
-        else:
-            media_added = False
-            if udiskie_device.has_media() and not last_state.has_media:
-                media_added = True
-
-            if media_added and not last_state.mounted:
-                # Wasn't mounted before, but it has new media now.
-                self.add_device(udiskie_device)
-
-        self._store_device_state(udiskie_device)
-
-
-def cli(args):
+def option_parser():
+    import optparse
     parser = optparse.OptionParser()
     parser.add_option('-a', '--all', action='store_true',
                       dest='all', default=False,
                       help='mount all present devices')
-    parser.add_option('-v', '--verbose', action='store_true',
-                      dest='verbose', default=False,
-                      help='verbose output')
+    parser.add_option('-v', '--verbose', action='store_const',
+                      dest='log_level', default=logging.INFO,
+                      const=logging.DEBUG, help='verbose output')
     parser.add_option('-f', '--filters', action='store',
                       dest='filters', default=None,
                       metavar='FILE', help='filter FILE')
     parser.add_option('-P', '--password-prompt', action='store',
                       dest='password_prompt', default='zenity',
                       metavar='MODULE', help="replace password prompt")
-    (options, args) = parser.parse_args(args)
+    return parser
 
-    log_level = logging.INFO
-    if options.verbose:
-        log_level = logging.DEBUG
-    logging.basicConfig(level=log_level, format='%(message)s')
+def cli(args):
+    parser = option_parser()
+    options, posargs = parser.parse_args(args)
+    logging.basicConfig(level=options.log_level, format='%(message)s')
 
     if options.suppress_notify:
         notify = None
 
     prompt = udiskie.prompt.password(options.password_prompt)
 
+    mounter = Mounter(
+            bus=None, filter_file=options.filters,
+            notify=notify, prompt=prompt)
+
+    # mount all present devices
+    if options.all:
+        mounter.mount_present_devices()
+
     # only mount the desired devices
-    if options.all or len(args) > 0:
-        mounter = Mounter(
-                bus=None, filter_file=options.filters,
-                notify=notify, prompt=prompt)
+    elif len(posargs) > 0:
+        for path in posargs:
+            device = udiskie.device.get_device(mounter.bus, path)
+            if device:
+                mounter.add_device(device)
 
-        if options.all:
-            mounter.mount_present_devices()
-        else:
-            for path in args:
-                device = udiskie.device.get_device(mounter.bus, path)
-                if device:
-                    mounter.add_device(device)
+    # print command line options
+    else:
+        parser.print_usage()
 
-    # run as a daemon
-    else:
-        mounter = AutoMounter(
-                bus=None, filter_file=options.filters,
-                notify=notify, prompt=prompt)
-        mounter.mount_present_devices()
-        return gobject.MainLoop().run()
-

udiskie/umount.py

 warnings.filterwarnings("ignore", ".*g_object_unref.*", Warning)
 
 import logging
-import optparse
 import os
 
 import dbus
             unmounted.append(device)
     return unmounted
 
-def cli(args):
-    logger = logging.getLogger('udiskie.umount.cli')
+def option_parser():
+    import optparse
     parser = optparse.OptionParser()
     parser.add_option('-a', '--all', action='store_true',
                       dest='all', default=False,
                       help='all devices')
-    parser.add_option('-v', '--verbose', action='store_true',
-                      dest='verbose', default=False,
-                      help='verbose output')
+    parser.add_option('-v', '--verbose', action='store_const',
+                      dest='log_level', default=logging.INFO,
+                      const=logging.DEBUG, help='verbose output')
     parser.add_option('-s', '--suppress', action='store_true',
                       dest='suppress_notify', default=False,
                       help='suppress popup notifications')
-    (options, args) = parser.parse_args(args)
+    return parser
 
-    log_level = logging.INFO
-    if options.verbose:
-        log_level = logging.DEBUG
-    logging.basicConfig(level=log_level, format='%(message)s')
+def cli(args):
+    logger = logging.getLogger('udiskie.umount.cli')
+    (options, posargs) = option_parser().parse_args(args)
+    logging.basicConfig(level=options.log_level, format='%(message)s')
 
     if options.suppress_notify:
         notify = lambda ctx: lambda *args: True
     if options.all:
         unmounted = unmount_all(notify)
     else:
-        if len(args) == 0:
+        if len(posargs) == 0:
             logger.warn('No devices provided for unmount')
             return 1
 
         unmounted = []
-        for path in args:
+        for path in posargs:
             device = unmount(os.path.normpath(path), notify)
             if device:
                 unmounted.append(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.