Commits

Christian Ebert committed 42b3807

viewhtmlmsg: inotify when available w/o option, reinstate keep=0

Ditch try-except-finally block for compatibility with python 2.4:
- python is capable to clean up after open errors
- file descriptor was not properly referenced

Comments (0)

Files changed (2)

muttils/viewhtmlmsg.py

 # $Id$
 
 import email, email.Errors, email.Iterators, email.Utils
-import os.path, re, shutil, sys, tempfile, time, urllib
+import os.path, re, shutil, sys, tempfile, urllib
 from muttils import pybrowser, ui, util
 
+try:
+    import inotifyx
+except ImportError:
+    import time
+
+
 class viewhtml(pybrowser.browser):
-    def __init__(self, safe, keep, use_inotify, app, args):
+    def __init__(self, safe, keep, 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
-        self.use_inotify = use_inotify or self.ui.configbool('html', 'inotify')
-        if self.keep is None and not use_inotify:
+        if self.keep is None:
             self.keep = self.ui.configint('html', 'keep', 3)
 
     def cleanup(self, tmpdir):
-        if self.keep or self.use_inotify:
+        if self.keep:
             shutil.rmtree(tmpdir)
 
+    def ivisit(self):
+        try:
+            fd = inotifyx.init()
+            wd = inotifyx.add_watch(fd, self.items[0], inotifyx.IN_CLOSE)
+            self.urlvisit()
+            inotifyx.get_events(fd, self.keep)
+            inotifyx.rm_watch(fd, wd)
+            os.close(fd)
+        except IOError:
+            hint = ('consider increasing '
+                    '/proc/sys/fs/inotify/max_user_watches')
+            raise util.DeadMan('failed to enable inotify', hint=hint)
+
     def view(self):
         try:
             if self.inp:
             fp.write(html)
             fp.close()
             self.items = [htmlfile]
-            if self.use_inotify:
-                import inotifyx
+            if self.keep:
                 try:
-                    fd = inotifyx.init()
-                    wd = inotifyx.add_watch(fd, htmlfile, inotifyx.IN_CLOSE)
+                    self.ivisit()
+                except NameError:
                     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:
+                    time.sleep(self.keep)
+            else:
                 self.urlvisit()
-                time.sleep(self.keep)
         finally:
             self.cleanup(htmldir)

muttils/viewhtmlmsgcommand.py

     parser = optparse.OptionParser(usage='%prog [options]',
                                    description=__doc__,
                                    version=util.fullversion(proginfo))
-    parser.set_defaults(safe=False, keep=None, use_inotify=False, app=None)
+    parser.set_defaults(safe=False, keep=None, 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.use_inotify, options.app, args)
+        v = viewhtmlmsg.viewhtml(options.safe, options.keep, options.app, args)
         v.view()
     except (util.DeadMan, IOError, KeyboardInterrupt), inst:
         sys.exit(inst)
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.