Thomas Gläßle avatar Thomas Gläßle committed 7b3f90b Merge

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

Comments (0)

Files changed (7)

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

bin/udiskie-mount

+#! /usr/bin/env python
+
+if __name__ == '__main__':
+    import sys
+    import udiskie.cli
+
+    sys.exit(udiskie.cli.mount(sys.argv[1:], allow_daemon=False))
+

bin/udiskie-umount

 
 if __name__ == '__main__':
     import sys
-    import udiskie.umount
+    import udiskie.cli
 
-    sys.exit(udiskie.umount.cli(sys.argv[1:]))
+    sys.exit(udiskie.cli.umount(sys.argv[1:]))
+"""
+Udiskie CLI logic.
+"""
+__all__ = ['mount_options', 'mount', 'umount_options', 'umount']
+
+import warnings
+warnings.filterwarnings("ignore", ".*could not open display.*", Warning)
+warnings.filterwarnings("ignore", ".*g_object_unref.*", Warning)
+
+import os
+import logging
+import dbus
+
+import udiskie.mount
+import udiskie.umount
+import udiskie.device
+import udiskie.prompt
+import udiskie.notify
+import udiskie.automount
+import udiskie.daemon
+
+
+def mount_options():
+    """
+    Return the mount option parser for the mount command.
+    """
+    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_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('-s', '--suppress', action='store_true',
+                      dest='suppress_notify', default=False,
+                      help='suppress popup notifications')
+    parser.add_option('-P', '--password-prompt', action='store',
+                      dest='password_prompt', default='zenity',
+                      metavar='MODULE', help="replace password prompt")
+    return parser
+
+
+def mount(args, allow_daemon=False):
+    """
+    Execute the mount/daemon command.
+    """
+    parser = mount_options()
+    options, posargs = parser.parse_args(args)
+    logging.basicConfig(level=options.log_level, format='%(message)s')
+    run_daemon = allow_daemon and not options.all and len(posargs) == 0
+
+    # establish connection to system bus
+    if run_daemon:
+        from dbus.mainloop.glib import DBusGMainLoop
+        DBusGMainLoop(set_as_default=True)
+    bus = dbus.SystemBus()
+
+    # create a mounter
+    prompt = udiskie.prompt.password(options.password_prompt)
+    mounter = udiskie.mount.Mounter(
+            bus=bus, filter_file=options.filters, prompt=prompt)
+
+    # run udiskie daemon if needed
+    if run_daemon:
+        daemon = udiskie.daemon.Daemon(bus)
+    if run_daemon and not options.suppress_notify:
+        notify = udiskie.notify.Notify('udiskie.mount')
+        notify.connect(daemon)
+    if run_daemon:
+        automount = udiskie.automount.AutoMounter(mounter)
+        automount.connect(daemon)
+
+    # mount all present devices
+    if options.all:
+        mounter.mount_present_devices()
+
+    # only mount the desired devices
+    elif len(posargs) > 0:
+        for path in posargs:
+            device = udiskie.device.get_device(mounter.bus, path)
+            if device:
+                mounter.add_device(device)
+
+    # run in daemon mode
+    elif run_daemon:
+        mounter.mount_present_devices()
+        return daemon.run()
+
+    # print command line options
+    else:
+        parser.print_usage()
+
+
+
+def umount_options():
+    """
+    Return the command line option parser for the umount command.
+    """
+    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_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')
+    return parser
+
+def umount(args):
+    """
+    Execute the umount command.
+    """
+    logger = logging.getLogger('udiskie.umount.cli')
+    (options, posargs) = umount_options().parse_args(args)
+    logging.basicConfig(level=options.log_level, format='%(message)s')
+
+    if options.all:
+        unmounted = udiskie.umount.unmount_all()
+    else:
+        if len(posargs) == 0:
+            logger.warn('No devices provided for unmount')
+            return 1
+
+        unmounted = []
+        for path in posargs:
+            device = udiskie.umount.unmount(os.path.normpath(path))
+            if device:
+                unmounted.append(device)
+
+    # automatically lock unused luks slaves of unmounted devices
+    for device in unmounted:
+        udiskie.umount.lock_slave(device)
+

udiskie/common.py

 """
 Common utilities.
 """
-__all__ = ['Properties', 'system_bus']
+__all__ = ['Properties']
 import dbus
 
 
         """Retrieve the property via the dbus proxy."""
         return self.__proxy.Get(self.__interface, property)
 
-def system_bus():
-    """
-    Connect to system bus.
-
-    When called for the first time, dbus will be initialized to use a glib
-    mainloop.
-
-    """
-    if not system_bus.initialized:
-        from dbus.mainloop.glib import DBusGMainLoop
-        DBusGMainLoop(set_as_default=True)
-        system_bus.initialized = True
-    return dbus.SystemBus()
-system_bus.initialized = False
-
 """
 Udiskie mount utilities.
 """
-__all__ = ['Mounter', 'option_parser', 'cli']
-
-import warnings
-warnings.filterwarnings("ignore", ".*could not open display.*", Warning)
-warnings.filterwarnings("ignore", ".*g_object_unref.*", Warning)
+__all__ = ['Mounter']
 
 import logging
 import os
 
 import udiskie.device
 import udiskie.match
-import udiskie.prompt
-import udiskie.notify
-import udiskie.automount
-import udiskie.daemon
-
-from udiskie.common import system_bus
-
 
 class Mounter:
     CONFIG_PATH = 'udiskie/filters.conf'
             self.add_device(device)
 
 
-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_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('-s', '--suppress', action='store_true',
-                      dest='suppress_notify', default=False,
-                      help='suppress popup notifications')
-    parser.add_option('-P', '--password-prompt', action='store',
-                      dest='password_prompt', default='zenity',
-                      metavar='MODULE', help="replace password prompt")
-    return parser
-
-def cli(args, allow_daemon=False):
-    parser = option_parser()
-    options, posargs = parser.parse_args(args)
-    logging.basicConfig(level=options.log_level, format='%(message)s')
-
-    # establish connection to system bus
-    bus = system_bus()
-
-    # create a mounter
-    prompt = udiskie.prompt.password(options.password_prompt)
-    mounter = Mounter(bus=bus, filter_file=options.filters, prompt=prompt)
-
-    # run udiskie daemon if needed
-    run_daemon = allow_daemon and not options.all and len(posargs) == 0
-    if run_daemon:
-        daemon = udiskie.daemon.Daemon(bus)
-
-    if run_daemon and not options.suppress_notify:
-        notify = udiskie.notify.Notify('udiskie.mount')
-        notify.connect(daemon)
-
-    if run_daemon:
-        automount = udiskie.automount.AutoMounter(mounter)
-        automount.connect(daemon)
-
-    # mount all present devices
-    if options.all:
-        mounter.mount_present_devices()
-
-    # only mount the desired devices
-    elif len(posargs) > 0:
-        for path in posargs:
-            device = udiskie.device.get_device(mounter.bus, path)
-            if device:
-                mounter.add_device(device)
-
-    # run in daemon mode
-    elif run_daemon:
-        mounter.mount_present_devices()
-        return daemon.run()
-
-    # print command line options
-    else:
-        parser.print_usage()
-

udiskie/umount.py

 """
 __all__ = [
     'unmount_device', 'lock_device', 'remove_device', 'lock_slave',
-    'unmount', 'unmount_all', 'option_parser', 'cli']
-
-import warnings
-warnings.filterwarnings("ignore", ".*could not open display.*", Warning)
-warnings.filterwarnings("ignore", ".*g_object_unref.*", Warning)
+    'unmount', 'unmount_all']
 
 import logging
-import os
-
 import dbus
 
 import udiskie.device
             unmounted.append(device)
     return unmounted
 
-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_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')
-    return parser
-
-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.all:
-        unmounted = unmount_all()
-    else:
-        if len(posargs) == 0:
-            logger.warn('No devices provided for unmount')
-            return 1
-
-        unmounted = []
-        for path in posargs:
-            device = unmount(os.path.normpath(path))
-            if device:
-                unmounted.append(device)
-
-    # automatically lock unused luks slaves of unmounted devices
-    for device in unmounted:
-        lock_slave(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.