Thomas Gläßle avatar Thomas Gläßle committed 36d1698

Separate optparse.OptionParser creation

In preparation to use the separate utilities for automounting,
notification and basic mounting.

Comments (0)

Files changed (2)

 warnings.filterwarnings("ignore", ".*g_object_unref.*", Warning)
 
 import logging
-import optparse
 import os
 
 import dbus
 
         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):
+    options, args = option_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)
 
-    # only mount the desired devices
-    if options.all or len(args) > 0:
+    # mount all present devices
+    if options.all:
         mounter = Mounter(
                 bus=None, filter_file=options.filters,
                 notify=notify, prompt=prompt)
+        mounter.mount_present_devices()
 
-        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)
+    # 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:
+            device = udiskie.device.get_device(mounter.bus, path)
+            if device:
+                mounter.add_device(device)
 
     # run as a daemon
     else:

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, args) = option_parser().parse_args(args)
+    logging.basicConfig(level=options.log_level, format='%(message)s')
 
     if options.suppress_notify:
         notify = lambda ctx: lambda *args: True
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.