Thomas Gläßle avatar Thomas Gläßle committed 1ba5013

Load filters calling controlling code

Should use dependency injection where practical.

Comments (0)

Files changed (2)

 """
 Udiskie CLI logic.
 """
-__all__ = ['mount_options', 'mount', 'umount_options', 'umount']
+__all__ = [
+    'load_filter',
+    'mount_program_options', 'mount',
+    'umount_program_options', 'umount']
 
 import warnings
 warnings.filterwarnings("ignore", ".*could not open display.*", Warning)
 import logging
 import dbus
 
+import udiskie.match
 import udiskie.mount
 import udiskie.umount
 import udiskie.device
 import udiskie.daemon
 
 
-def mount_options():
+CONFIG_PATH = 'udiskie/filters.conf'
+
+def load_filter(filter_file=None):
+    """Load mount option filters."""
+    try:
+        from xdg.BaseDirectory import xdg_config_home
+    except ImportError:
+        xdg_config_home = os.path.expanduser('~/.config')
+    if not filter_file:
+        filter_file = os.path.join(xdg_config_home, CONFIG_PATH)
+    return udiskie.match.FilterMatcher((filter_file,))
+
+
+def mount_program_options():
     """
     Return the mount option parser for the mount command.
     """
     """
     Execute the mount/daemon command.
     """
-    parser = mount_options()
+    parser = mount_program_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
 
     # create a mounter
     prompt = udiskie.prompt.password(options.password_prompt)
-    mounter = udiskie.mount.Mounter(
-            bus=bus, filter_file=options.filters, prompt=prompt)
+    filter = load_filter(options.filters)
+    mounter = udiskie.mount.Mounter(bus=bus, filter=filter, prompt=prompt)
 
     # run udiskie daemon if needed
     if run_daemon:
         parser.print_usage()
 
 
-
-def umount_options():
+def umount_program_options():
     """
     Return the command line option parser for the umount command.
     """
                       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)
+    (options, posargs) = umount_program_options().parse_args(args)
     logging.basicConfig(level=options.log_level, format='%(message)s')
 
     if options.all:
 __all__ = ['Mounter']
 
 import logging
-import os
 import dbus
 
-try:
-    from xdg.BaseDirectory import xdg_config_home
-except ImportError:
-    xdg_config_home = os.path.expanduser('~/.config')
-
 import udiskie.device
-import udiskie.match
 
 class Mounter:
-    CONFIG_PATH = 'udiskie/filters.conf'
 
-    def __init__(self, bus, filter_file=None, prompt=None):
+    def __init__(self, bus, filter=None, prompt=None):
         self.log = logging.getLogger('udiskie.mount.Mounter')
-        self.bus = bus
+        self.bus = bus or dbus.SystemBus()
         self.prompt = prompt
-
-        if not filter_file:
-            filter_file = os.path.join(xdg_config_home, self.CONFIG_PATH)
-        self.filters = udiskie.match.FilterMatcher((filter_file,))
+        self.filter = filter
 
     def mount_device(self, device):
         """
             return False
 
         fstype = str(device.id_type)
-        options = self.filters.get_mount_options(device)
+        options = self.filter.get_mount_options(device) if self.filter else []
 
         S = 'attempting to mount device %s (%s:%s)'
         self.log.info(S % (device, fstype, options))
             return None
 
         mount_paths = ', '.join(device.mount_paths)
-
         return True
 
     def unlock_device(self, 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.