Tasos Latsas avatar Tasos Latsas committed d34bd5e

make pynotify notifications optional

Comments (0)

Files changed (2)

 
 import dbus
 import gobject
-import gio
-import pynotify
 
 try:
     from xdg.BaseDirectory import xdg_config_home
 
 import udiskie.device
 import udiskie.match
+import udiskie.notify
 
 class DeviceState:
     def __init__(self, mounted, has_media):
 class AutoMounter:
     CONFIG_PATH = 'udiskie/filters.conf'
 
-    def __init__(self, bus=None, filter_file=None):
+    def __init__(self, bus=None, filter_file=None, notify=None):
         self.log = logging.getLogger('udiskie.mount.AutoMounter')
         self.last_device_state = {}
 
             filter_file = os.path.join(xdg_config_home, self.CONFIG_PATH)
         self.filters = udiskie.match.FilterMatcher((filter_file,))
 
+        if not notify:
+            self.notify = lambda *args: True
+        else:
+            self.notify = notify
 
         self.bus.add_signal_receiver(self.device_added,
                                      signal_name='DeviceAdded',
                         return
 
                     mount_paths = ', '.join(device.mount_paths())
-                    try:
-                        pynotify.Notification('Device mounted',
-                                              '%s mounted on %s' % (device.device_file(),
-                                                                    mount_paths),
-                                              'drive-removable-media').show()
-                    except gio.Error:
-                        pass
+                    self.notify(device.device_file(), mount_paths)
             finally:
                 self._store_device_state(device)
 
         log_level = logging.DEBUG
     logging.basicConfig(level=log_level, format='%(message)s')
 
-    pynotify.init('udiskie.mount')
+    if options.suppress_notify:
+        notify = None
+    else:
+        notify = udiskie.notify.Notify('udiskie.mount').mount
 
-    mounter = AutoMounter(bus=None, filter_file=options.filters)
+    mounter = AutoMounter(bus=None, filter_file=options.filters, notify=notify)
     mounter.mount_present_devices()
     return gobject.MainLoop().run()

udiskie/umount.py

 import os
 
 import dbus
-import gio
-import pynotify
 
 import udiskie.device
+import udiskie.notify
 
-def unmount_device(device):
+def unmount_device(device, notify):
     """Unmount a Device.
 
     Checks to make sure the device is unmountable and then unmounts."""
                                                               dbus_err))
             return
 
-        try:
-            pynotify.Notification('Device unmounted',
-                                  '%s unmounted' % (device.device_file(),),
-                                  'drive-removable-media').show()
-        except gio.Error:
-            pass
+        notify(device.device_file())
     else:
         logger.debug('skipping unhandled device %s' % (device,))
 
-def unmount(path):
+def unmount(path, notify):
     """Unmount a filesystem
 
     The filesystem must match the criteria for a filesystem mountable by
     for device in udiskie.device.get_all(bus):
         if path in device.mount_paths() or path == device.device_file():
             logger.debug('found device owning "%s": "%s"' % (path, device))
-            unmount_device(device)
+            unmount_device(device, notify)
 
-def unmount_all():
+def unmount_all(notify):
     """Unmount all filesystems handleable by udiskie."""
 
     bus = dbus.SystemBus()
     for device in udiskie.device.get_all(bus):
-        unmount_device(device)
+        unmount_device(device, notify)
 
 def cli(args):
     logger = logging.getLogger('udiskie.umount.cli')
         log_level = logging.DEBUG
     logging.basicConfig(level=log_level, format='%(message)s')
 
-    pynotify.init('udiskie.umount')
+    if options.suppress_notify:
+        notify = lambda *args: True
+    else:
+        notify = udiskie.notify.Notify('udiskie.umount').umount
 
     if options.all:
-        unmount_all()
+        unmount_all(notify)
     else:
         if len(args) == 0:
             logger.warn('No devices provided for unmount')
             return 1
 
         for path in args:
-            unmount(os.path.normpath(path))
+            unmount(os.path.normpath(path), notify)
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.