1. Christian Ebert
  2. muttils

Commits

cwarden  committed 7506c68

add option to use inotify (using inotifyx library)

Add option to use inotify (using inotifyx library) to wait for browser
to load page before deleting temporary files.

inotify_add_watch returns "no space left on device" when
/proc/sys/fs/inotify/max_user_watches is too low. Show a helpful
error when this occurs.

  • Participants
  • Parent commits 0bc4d72
  • Branches default

Comments (0)

Files changed (2)

File muttils/viewhtmlmsg.py

View file
  • Ignore whitespace
 from muttils import pybrowser, ui, util
 
 class viewhtml(pybrowser.browser):
-    def __init__(self, safe, keep, app, args):
+    def __init__(self, safe, keep, use_inotify, app, args):
         self.ui = ui.ui()
         self.ui.updateconfig()
         pybrowser.browser.__init__(self, parentui=self.ui,
         self.inp = args
         self.safe = safe or self.ui.configbool('html', 'safe')
         self.keep = keep
-        if self.keep is None:
+        self.use_inotify = use_inotify or self.ui.configbool('html', 'inotify')
+        if self.keep is None and not use_inotify:
             self.keep = self.ui.configint('html', 'keep', 3)
 
     def cleanup(self, tmpdir):
-        if self.keep:
+        if self.keep or self.use_inotify:
             shutil.rmtree(tmpdir)
 
     def view(self):
             fp.write(html)
             fp.close()
             self.items = [htmlfile]
-            self.urlvisit()
-            if self.keep:
+            if self.use_inotify:
+                import inotifyx
+                try:
+                    fd = inotifyx.init()
+                    wd = inotifyx.add_watch(fd, htmlfile, inotifyx.IN_CLOSE)
+                    self.urlvisit()
+                    # timeout after keep seconds, or wait forever if keep is None
+                    if self.keep:
+                        event = inotifyx.get_events(fd, self.keep)
+                    else:
+                        events = inotifyx.get_events(fd)
+                    inotifyx.rm_watch(fd, wd)
+                except IOError:
+                    raise util.DeadMan('Failed to enable inotify.  Verify that /proc/sys/fs/inotify/max_user_watches is high enough.')
+                finally:
+                    os.close(fd)
+            elif self.keep:
+                self.urlvisit()
                 time.sleep(self.keep)
         finally:
             self.cleanup(htmldir)

File muttils/viewhtmlmsgcommand.py

View file
  • Ignore whitespace
     parser = optparse.OptionParser(usage='%prog [options]',
                                    description=__doc__,
                                    version=util.fullversion(proginfo))
-    parser.set_defaults(safe=False, keep=None, app=None)
+    parser.set_defaults(safe=False, keep=None, use_inotify=False, app=None)
     parser.add_option('-s', '--safe', action='store_true',
                       help='view html w/o loading remote files')
     parser.add_option('-k', '--keep', type='int',
                       help='remove temporary files after KEEP seconds '
                            '(0 for keeping files)')
+    parser.add_option('-i', '--inotify', action='store_true', dest="use_inotify",
+                      help='use inotify to wait for browser')
     parser.add_option('-b', '--browser', dest='app',
                       help='prefer browser APP over $BROWSER environment')
 
     del parser
 
     try:
-        v = viewhtmlmsg.viewhtml(options.safe, options.keep, options.app, args)
+        v = viewhtmlmsg.viewhtml(options.safe, options.keep, options.use_inotify, options.app, args)
         v.view()
     except (util.DeadMan, IOError, KeyboardInterrupt), inst:
         sys.exit(inst)