Christian Ebert avatar Christian Ebert committed 02501da

pybrowser: refactor with own Exception class

ui.app is now $BROWSER or None. This way we recognize and handle
textbrowsers correctly when they are set by the environment.

The webbrowser module will give a bogus $BROWSER more trust and turn
it into a generic instance. Therefore we catch this with
PybrowserError with the webbrowser.Error message.

Comments (0)

Files changed (2)

muttils/pybrowser.py

 import os, webbrowser
 from muttils import ui, urlregex, util
 
+class PybrowserError(util.DeadMan):
+    '''
+    Error class for the pybrowser module.
+    '''
+    def __init__(self, *args, **kw):
+        util.DeadMan.__init__(self, *args, **kw)
+        if not self.value:
+            self.value = 'could not locate runnable browser'
+
 class browser(object):
     '''
     Visits items with default or given browser.
 
     def appname(self):
         '''Extracts (text)browser name from /path/to/name(.exe).'''
-        return os.path.splitext(os.path.basename(self.ui.app))[0]
+        try:
+            return os.path.splitext(os.path.basename(self.ui.app))[0]
+        except AttributeError:
+            return ''
 
     def fixurl(self, url, cygpath):
         '''Adapts possibly short url to pass as browser argument.'''
             if not url.startswith('http://'):
                 url = util.absolutepath(url)
                 if not os.path.exists(url):
-                    raise util.DeadMan('%s: not found' % url)
+                    raise PybrowserError('%s: not found' % url)
                 if cygpath:
                     url = util.pipeline(['cygpath', '-w', url]).rstrip()
                 url = 'file://' + url
         system path?'''
         if not util.cygwin() or tb:
             return False
-        app = self.ui.app
-        if app is None:
-            try:
-                app = os.environ['BROWSER']
-            except KeyError:
-                hint = '$BROWSER environment variable required on cygwin'
-                raise util.DeadMan('cannot detect system browser', hint=hint)
-        return app.find('/cygdrive/') == 0 and app.find('/Cygwin/') < 0
+        try:
+            return (self.ui.app.find('/cygdrive/') == 0 and
+                    self.ui.app.find('/Cygwin/') < 0)
+        except AttributeError:
+            hint = '$BROWSER environment variable required on cygwin'
+            raise PybrowserError(hint=hint)
 
     def urlvisit(self):
         '''Visit url(s).'''
         textbrowsers = 'w3m', 'lynx', 'links', 'elinks'
-        app, tb, notty, screen = '', False, False, False
-        if self.ui.app is not None:
-            app = self.appname()
-            tb = app in textbrowsers
-            if tb:
-                notty = not util.termconnected()
-                screen = 'STY' in os.environ
+        notty, screen = False, False
+        app = self.appname()
+        tb = app in textbrowsers
+        if tb:
+            notty = not util.termconnected()
+            screen = 'STY' in os.environ
         cygpath = self.cygpath(tb)
         if not self.items:
             self.items = [self.ui.configitem('net', 'homepage')]
             try:
                 b = webbrowser.get(self.ui.app)
                 for url in self.items:
-                    b.open(url)
+                    if not b.open(url):
+                        # BROWSER=invalid gives valid
+                        # webbrowser.GenericBrowser instance
+                        # and returns False
+                        raise PybrowserError
             except webbrowser.Error, inst:
-                raise util.DeadMan(inst)
+                raise PybrowserError(inst)
 
 # $Id$
 
-import ConfigParser, os.path, sys
+import ConfigParser, os, sys
 from muttils import util
 
 class ui(object):
     mhiers = ''     # colon separated list of mail hierarchies
     specdirs = ''   # colon separated list of specified mail hierarchies
     mask = None     # file mask for mail hierarchies
-    app = None      # browser program
+    app = os.getenv('BROWSER')
     ftpdir = ''     # download directory for ftp
     getdir = ''     # download directory for wget
 
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.