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)


 # $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
+    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()
         pybrowser.browser.__init__(self, parentui=self.ui,
         self.inp = args = 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:
+    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):
             if self.inp:
             self.items = [htmlfile]
-            if self.use_inotify:
-                import inotifyx
+            if self.keep:
-                    fd = inotifyx.init()
-                    wd = inotifyx.add_watch(fd, htmlfile, inotifyx.IN_CLOSE)
+                    self.ivisit()
+                except NameError:
-                    # 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:
-                time.sleep(self.keep)


     parser = optparse.OptionParser(usage='%prog [options]',
-    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
-        v = viewhtmlmsg.viewhtml(, options.keep, options.use_inotify,, args)
+        v = viewhtmlmsg.viewhtml(, options.keep,, args)
     except (util.DeadMan, IOError, KeyboardInterrupt), 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
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.