Thomas Gläßle avatar Thomas Gläßle committed a7fbbc3

Separate automount

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.notify
+import udiskie.mount
+import udiskie.device
+
+
+class AutoMounter(udiskie.mount.Mounter):
+
+    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.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):
+    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.mount_present_devices()
+        return gobject.MainLoop().run()
+
+
 
 import logging
 import os
-
 import dbus
-import gobject
 
 try:
     from xdg.BaseDirectory import xdg_config_home
             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 option_parser():
     import optparse
     parser = optparse.OptionParser()
     return parser
 
 def cli(args):
-    options, args = option_parser().parse_args(args)
+    parser = option_parser()
+    options, posargs = parser.parse_args(args)
     logging.basicConfig(level=options.log_level, format='%(message)s')
 
     if options.suppress_notify:
 
     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 = Mounter(
-                bus=None, filter_file=options.filters,
-                notify=notify, prompt=prompt)
         mounter.mount_present_devices()
 
     # only mount the desired devices
-    elif len(args) > 0:
-        mounter = Mounter(
-                bus=None, filter_file=options.filters,
-                notify=notify, prompt=prompt)
-        for path in args:
+    elif len(posargs) > 0:
+        for path in posargs:
             device = udiskie.device.get_device(mounter.bus, path)
             if device:
                 mounter.add_device(device)
 
-    # run as a daemon
+    # print command line options
     else:
-        mounter = AutoMounter(
-                bus=None, filter_file=options.filters,
-                notify=notify, prompt=prompt)
-        mounter.mount_present_devices()
-        return gobject.MainLoop().run()
+        parser.print_usage()
 

udiskie/umount.py

 
 def cli(args):
     logger = logging.getLogger('udiskie.umount.cli')
-    (options, args) = option_parser().parse_args(args)
+    (options, posargs) = option_parser().parse_args(args)
     logging.basicConfig(level=options.log_level, format='%(message)s')
 
     if options.suppress_notify:
     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.