Commits

Christian Ebert committed 04e472c

pybrowser: refactor to make most use of the webbrowser module

Use webbrowser.get(optarg).[base]name attributes to comply with the
module's browser detection mechanism.

Remove util.progname(), revert default of ui.app.

Comments (0)

Files changed (3)

     Visits items with default or given browser.
     '''
     weburl_re = None          # url protocol scheme regex
+    appname = ''
 
     def __init__(self, parentui=None, items=None, app=None, evalurl=False):
         self.ui = parentui or ui.ui()
         self.items = items             # urls
         if app is not None:
             self.ui.app = app
-        self.appname = util.progname(self.ui.app)
-        self.cygwin = util.cygwin()
+        try:
+            self.ui.app = webbrowser.get(self.ui.app)
+        except webbrowser.Error, inst:
+            raise PybrowserError(inst)
+        try:
+            # lynx.old -> lynx, lynx.exe -> lynx
+            self.appname = os.path.splitext(self.ui.app.basename)[0]
+        except AttributeError:
+            pass
         if evalurl: # check remote url protocol scheme
             self.ui.proto = 'web'
             u = urlregex.urlregex(self.ui, uniq=False)
     def cygpath(self, tb):
         '''Do we have to call cygpath to transform local path to windows file
         system path?'''
-        if not self.cygwin or tb:
+        if not util.cygwin() or tb:
             return False
-        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)
+        return (self.ui.app.name.find('/cygdrive/') == 0 and
+                self.ui.app.name.find('/Cygwin/') < 0)
 
     def urlvisit(self):
         '''Visit url(s).'''
         if tb:
             notty = not util.termconnected()
             screen = 'STY' in os.environ
-            if self.cygwin:
-                self.ui.app = os.path.splitext(self.ui.app)[0]
         cygpath = self.cygpath(tb)
         if not self.items:
             self.items = [self.ui.configitem('net', 'homepage')]
         # but has to be connected if called into another screen instance
         if screen or self.appname in textbrowsers[1:] and notty:
             for url in self.items:
-                util.systemcall([self.ui.app, url], notty, screen)
+                util.systemcall([self.ui.app.name, url], notty, screen)
         else:
-            try:
-                b = webbrowser.get(self.ui.app)
-                for url in self.items:
-                    if not b.open(url):
-                        # BROWSER=invalid gives valid
-                        # webbrowser.GenericBrowser instance
-                        # and returns False
-                        raise PybrowserError
-            except webbrowser.Error, inst:
-                raise PybrowserError(inst)
+            for url in self.items:
+                if not self.ui.app.open(url):
+                    # BROWSER=invalid gives valid
+                    # webbrowser.GenericBrowser instance
+                    # and returns False
+                    raise PybrowserError
 
 # $Id$
 
-import ConfigParser, os, sys
+import ConfigParser, os.path, 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 = os.getenv('BROWSER')
+    app = None      # browser program
     ftpdir = ''     # download directory for ftp
     getdir = ''     # download directory for wget
 
     '''Returns true when we are on cygwin.'''
     return sys.platform == 'cygwin'
 
-def progname(prog):
-    '''Extracts program name from /path/to/name(.exe).
-    Returns empty string when prog is None.'''
-    if prog is None:
-        return ''
-    return os.path.splitext(os.path.basename(prog))[0]
-
 def systemcall(cs, notty=None, screen=None):
     '''Calls command sequence cs in manner suiting
     terminal connectivity.'''
     # programs that can be launched without terminal connection
     termprogs = 'w3m', 'wget'
-    prog = progname(cs[0])
+    prog = cs[0]
     if notty is None:
         # check if connected to terminal
         notty = prog not in termprogs and not termconnected()