Commit e8f24430 authored by Georg Brandl's avatar Georg Brandl

Patch #754022: Greatly enhanced webbrowser.py.

parent 76390de8
...@@ -6,9 +6,8 @@ ...@@ -6,9 +6,8 @@
\moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org} \moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
The \module{webbrowser} module provides a very high-level interface to The \module{webbrowser} module provides a high-level interface to
allow displaying Web-based documents to users. The controller objects allow displaying Web-based documents to users. Under most
are easy to use and are platform-independent. Under most
circumstances, simply calling the \function{open()} function from this circumstances, simply calling the \function{open()} function from this
module will do the right thing. module will do the right thing.
...@@ -17,19 +16,26 @@ browsers will be used if graphical browsers are not available or an X11 ...@@ -17,19 +16,26 @@ browsers will be used if graphical browsers are not available or an X11
display isn't available. If text-mode browsers are used, the calling display isn't available. If text-mode browsers are used, the calling
process will block until the user exits the browser. process will block until the user exits the browser.
Under \UNIX, if the environment variable \envvar{BROWSER} exists, it If the environment variable \envvar{BROWSER} exists, it
is interpreted to override the platform default list of browsers, as a is interpreted to override the platform default list of browsers, as a
colon-separated list of browsers to try in order. When the value of os.pathsep-separated list of browsers to try in order. When the value of
a list part contains the string \code{\%s}, then it is interpreted as a list part contains the string \code{\%s}, then it is interpreted as
a literal browser command line to be used with the argument URL a literal browser command line to be used with the argument URL
substituted for the \code{\%s}; if the part does not contain substituted for the \code{\%s}; if the part does not contain
\code{\%s}, it is simply interpreted as the name of the browser to \code{\%s}, it is simply interpreted as the name of the browser to
launch. launch.
For non-\UNIX{} platforms, or when X11 browsers are available on For non-\UNIX{} platforms, or when a remote browser is available on
\UNIX, the controlling process will not wait for the user to finish \UNIX, the controlling process will not wait for the user to finish
with the browser, but allow the browser to maintain its own window on with the browser, but allow the remote browser to maintain its own
the display. windows on the display. If remote browsers are not available on \UNIX,
the controlling process will launch a new browser and wait.
The script \program{webbrowser} can be used as a command-line interface
for the module. It accepts an URL as the argument. It accepts the following
optional parameters: \programopt{-n} opens the URL in a new browser window,
if possible; \programopt{-t} opens the URL in a new browser page ("tab"). The
options are, naturally, mutually exclusive.
The following exception is defined: The following exception is defined:
...@@ -40,15 +46,24 @@ The following exception is defined: ...@@ -40,15 +46,24 @@ The following exception is defined:
The following functions are defined: The following functions are defined:
\begin{funcdesc}{open}{url\optional{, new=0}\optional{, autoraise=1}} \begin{funcdesc}{open}{url\optional{, new=0}\optional{, autoraise=1}}
Display \var{url} using the default browser. If \var{new} is true, Display \var{url} using the default browser. If \var{new} is 0, the
a new browser window is opened if possible. If \var{autoraise} is \var{url} is opened in the same browser window. If \var{new} is 1,
a new browser window is opened if possible. If \var{new} is 2,
a new browser page ("tab") is opened if possible. If \var{autoraise} is
true, the window is raised if possible (note that under many window true, the window is raised if possible (note that under many window
managers this will occur regardless of the setting of this variable). managers this will occur regardless of the setting of this variable).
\end{funcdesc} \end{funcdesc}
\begin{funcdesc}{open_new}{url} \begin{funcdesc}{open_new_win}{url}
Open \var{url} in a new window of the default browser, if possible, Open \var{url} in a new window of the default browser, if possible,
otherwise, open \var{url} in the only browser window. otherwise, open \var{url} in the only browser window. Alias
\function{open_new}.
\end{funcdesc}
\begin{funcdesc}{open_new_tab}{url}
Open \var{url} in a new page ("tab") of the default browser, if possible,
otherwise equivalent to \function{open_new_win}.
\end{funcdesc} \end{funcdesc}
\begin{funcdesc}{get}{\optional{name}} \begin{funcdesc}{get}{\optional{name}}
...@@ -76,16 +91,24 @@ corresponding instantiations for the controller classes, all defined ...@@ -76,16 +91,24 @@ corresponding instantiations for the controller classes, all defined
in this module. in this module.
\begin{tableiii}{l|l|c}{code}{Type Name}{Class Name}{Notes} \begin{tableiii}{l|l|c}{code}{Type Name}{Class Name}{Notes}
\lineiii{'mozilla'}{\class{Netscape('mozilla')}}{} \lineiii{'mozilla'}{\class{Mozilla('mozilla')}}{}
\lineiii{'netscape'}{\class{Netscape('netscape')}}{} \lineiii{'firefox'}{\class{Mozilla('mozilla')}}{}
\lineiii{'mosaic'}{\class{GenericBrowser('mosaic \%s \&')}}{} \lineiii{'netscape'}{\class{Mozilla('netscape')}}{}
\lineiii{'galeon'}{\class{Galeon('galeon')}}{}
\lineiii{'epiphany'}{\class{Galeon('epiphany')}}{}
\lineiii{'skipstone'}{\class{GenericBrowser('skipstone \%s \&')}}{}
\lineiii{'konqueror'}{\class{Konqueror()}}{(1)}
\lineiii{'kfm'}{\class{Konqueror()}}{(1)} \lineiii{'kfm'}{\class{Konqueror()}}{(1)}
\lineiii{'mosaic'}{\class{GenericBrowser('mosaic \%s \&')}}{}
\lineiii{'opera'}{\class{Opera()}}{}
\lineiii{'grail'}{\class{Grail()}}{} \lineiii{'grail'}{\class{Grail()}}{}
\lineiii{'links'}{\class{GenericBrowser('links \%s')}}{} \lineiii{'links'}{\class{GenericBrowser('links \%s')}}{}
\lineiii{'elinks'}{\class{Elinks('elinks')}}{}
\lineiii{'lynx'}{\class{GenericBrowser('lynx \%s')}}{} \lineiii{'lynx'}{\class{GenericBrowser('lynx \%s')}}{}
\lineiii{'w3m'}{\class{GenericBrowser('w3m \%s')}}{} \lineiii{'w3m'}{\class{GenericBrowser('w3m \%s')}}{}
\lineiii{'windows-default'}{\class{WindowsDefault}}{(2)} \lineiii{'windows-default'}{\class{WindowsDefault}}{(2)}
\lineiii{'internet-config'}{\class{InternetConfig}}{(3)} \lineiii{'internet-config'}{\class{InternetConfig}}{(3)}
\lineiii{'macosx'}{\class{MacOSX('default')}}{(4)}
\end{tableiii} \end{tableiii}
\noindent \noindent
...@@ -101,13 +124,15 @@ using the \program{konqueror} command with KDE 2 --- the ...@@ -101,13 +124,15 @@ using the \program{konqueror} command with KDE 2 --- the
implementation selects the best strategy for running Konqueror. implementation selects the best strategy for running Konqueror.
\item[(2)] \item[(2)]
Only on Windows platforms; requires the common Only on Windows platforms.
extension modules \module{win32api} and \module{win32con}.
\item[(3)] \item[(3)]
Only on MacOS platforms; requires the standard MacPython \module{ic} Only on MacOS platforms; requires the standard MacPython \module{ic}
module, described in the \citetitle[../mac/module-ic.html]{Macintosh module, described in the \citetitle[../mac/module-ic.html]{Macintosh
Library Modules} manual. Library Modules} manual.
\item[(4)]
Only on MacOS X platform.
\end{description} \end{description}
...@@ -117,12 +142,18 @@ Browser controllers provide two methods which parallel two of the ...@@ -117,12 +142,18 @@ Browser controllers provide two methods which parallel two of the
module-level convenience functions: module-level convenience functions:
\begin{funcdesc}{open}{url\optional{, new}} \begin{funcdesc}{open}{url\optional{, new}}
Display \var{url} using the browser handled by this controller. If Display \var{url} using the browser handled by this controller.
\var{new} is true, a new browser window is opened if possible. If \var{new} is 1, a new browser window is opened if possible.
If \var{new} is 2, a new browser page ("tab") is opened if possible.
\end{funcdesc} \end{funcdesc}
\begin{funcdesc}{open_new}{url} \begin{funcdesc}{open_new_win}{url}
Open \var{url} in a new window of the browser handled by this Open \var{url} in a new window of the browser handled by this
controller, if possible, otherwise, open \var{url} in the only controller, if possible, otherwise, open \var{url} in the only
browser window. browser window. Alias \function{open_new}.
\end{funcdesc}
\begin{funcdesc}{open_new_tab}{url}
Open \var{url} in a new page ("tab") of the browser handled by this
controller, if possible, otherwise equivalent to \function{open_new_win}.
\end{funcdesc} \end{funcdesc}
#! /usr/bin/env python
"""Interfaces for launching and remotely controlling Web browsers.""" """Interfaces for launching and remotely controlling Web browsers."""
import os import os
import sys import sys
import stat
__all__ = ["Error", "open", "get", "register"] __all__ = ["Error", "open", "open_new", "open_new_tab", "get", "register"]
class Error(Exception): class Error(Exception):
pass pass
...@@ -11,9 +13,13 @@ class Error(Exception): ...@@ -11,9 +13,13 @@ class Error(Exception):
_browsers = {} # Dictionary of available browser controllers _browsers = {} # Dictionary of available browser controllers
_tryorder = [] # Preference order of available browsers _tryorder = [] # Preference order of available browsers
def register(name, klass, instance=None): def register(name, klass, instance=None, update_tryorder=1):
"""Register a browser connector and, optionally, connection.""" """Register a browser connector and, optionally, connection."""
_browsers[name.lower()] = [klass, instance] _browsers[name.lower()] = [klass, instance]
if update_tryorder > 0:
_tryorder.append(name)
elif update_tryorder < 0:
_tryorder.insert(0, name)
def get(using=None): def get(using=None):
"""Return a browser launcher instance appropriate for the environment.""" """Return a browser launcher instance appropriate for the environment."""
...@@ -26,27 +32,36 @@ def get(using=None): ...@@ -26,27 +32,36 @@ def get(using=None):
# User gave us a command line, don't mess with it. # User gave us a command line, don't mess with it.
return GenericBrowser(browser) return GenericBrowser(browser)
else: else:
# User gave us a browser name. # User gave us a browser name or path.
try: try:
command = _browsers[browser.lower()] command = _browsers[browser.lower()]
except KeyError: except KeyError:
command = _synthesize(browser) command = _synthesize(browser)
if command[1] is None: if command[1] is not None:
return command[0]()
else:
return command[1] return command[1]
elif command[0] is not None:
return command[0]()
raise Error("could not locate runnable browser") raise Error("could not locate runnable browser")
# Please note: the following definition hides a builtin function. # Please note: the following definition hides a builtin function.
# It is recommended one does "import webbrowser" and uses webbrowser.open(url)
# instead of "from webbrowser import *".
def open(url, new=0, autoraise=1): def open(url, new=0, autoraise=1):
get().open(url, new, autoraise) for name in _tryorder:
browser = get(name)
if browser.open(url, new, autoraise):
return True
return False
def open_new(url): def open_new(url):
get().open(url, 1) return open(url, 1)
def open_new_tab(url):
return open(url, 2)
def _synthesize(browser):
def _synthesize(browser, update_tryorder=1):
"""Attempt to synthesize a controller base on existing controllers. """Attempt to synthesize a controller base on existing controllers.
This is useful to create a controller when a user specifies a path to This is useful to create a controller when a user specifies a path to
...@@ -58,9 +73,10 @@ def _synthesize(browser): ...@@ -58,9 +73,10 @@ def _synthesize(browser):
executable for the requested browser, return [None, None]. executable for the requested browser, return [None, None].
""" """
if not os.path.exists(browser): cmd = browser.split()[0]
if not _iscommand(cmd):
return [None, None] return [None, None]
name = os.path.basename(browser) name = os.path.basename(cmd)
try: try:
command = _browsers[name.lower()] command = _browsers[name.lower()]
except KeyError: except KeyError:
...@@ -72,132 +88,199 @@ def _synthesize(browser): ...@@ -72,132 +88,199 @@ def _synthesize(browser):
controller = copy.copy(controller) controller = copy.copy(controller)
controller.name = browser controller.name = browser
controller.basename = os.path.basename(browser) controller.basename = os.path.basename(browser)
register(browser, None, controller) register(browser, None, controller, update_tryorder)
return [None, controller] return [None, controller]
return [None, None] return [None, None]
if sys.platform[:3] == "win":
def _isexecutable(cmd):
cmd = cmd.lower()
if os.path.isfile(cmd) and (cmd.endswith(".exe") or
cmd.endswith(".bat")):
return True
for ext in ".exe", ".bat":
if os.path.isfile(cmd + ext):
return True
return False
else:
def _isexecutable(cmd):
if os.path.isfile(cmd):
mode = os.stat(cmd)[stat.ST_MODE]
if mode & stat.S_IXUSR or mode & stat.S_IXGRP or mode & stat.S_IXOTH:
return True
return False
def _iscommand(cmd): def _iscommand(cmd):
"""Return True if cmd can be found on the executable search path.""" """Return True if cmd is executable or can be found on the executable
search path."""
if _isexecutable(cmd):
return True
path = os.environ.get("PATH") path = os.environ.get("PATH")
if not path: if not path:
return False return False
for d in path.split(os.pathsep): for d in path.split(os.pathsep):
exe = os.path.join(d, cmd) exe = os.path.join(d, cmd)
if os.path.isfile(exe): if _isexecutable(exe):
return True return True
return False return False
PROCESS_CREATION_DELAY = 4 # General parent classes
class BaseBrowser(object):
"""Parent class for all browsers."""
def __init__(self, name=""):
self.name = name
def open_new(self, url):
return self.open(url, 1)
def open_new_tab(self, url):
return self.open(url, 2)
class GenericBrowser(BaseBrowser):
"""Class for all browsers started with a command
and without remote functionality."""
class GenericBrowser:
def __init__(self, cmd): def __init__(self, cmd):
self.name, self.args = cmd.split(None, 1) self.name, self.args = cmd.split(None, 1)
self.basename = os.path.basename(self.name)
def open(self, url, new=0, autoraise=1): def open(self, url, new=0, autoraise=1):
assert "'" not in url assert "'" not in url
command = "%s %s" % (self.name, self.args) command = "%s %s" % (self.name, self.args)
os.system(command % url) rc = os.system(command % url)
return not rc
def open_new(self, url):
self.open(url)
class UnixBrowser(BaseBrowser):
"""Parent class for all Unix browsers with remote functionality."""
class Netscape: raise_opts = None
"Launcher class for Netscape browsers."
def __init__(self, name): remote_cmd = ''
self.name = name remote_action = None
self.basename = os.path.basename(name) remote_action_newwin = None
remote_action_newtab = None
remote_background = False
def _remote(self, action, autoraise): def _remote(self, url, action, autoraise):
raise_opt = ("-noraise", "-raise")[autoraise] autoraise = int(bool(autoraise)) # always 0/1
cmd = "%s %s -remote '%s' >/dev/null 2>&1" % (self.name, raise_opt = self.raise_opts and self.raise_opts[autoraise] or ''
raise_opt, cmd = "%s %s %s '%s' >/dev/null 2>&1" % (self.name, raise_opt,
action) self.remote_cmd, action)
if remote_background:
cmd += ' &'
rc = os.system(cmd) rc = os.system(cmd)
if rc: if rc:
import time # bad return status, try again with simpler command
os.system("%s &" % self.name) rc = os.system("%s %s" % (self.name, url))
time.sleep(PROCESS_CREATION_DELAY)
rc = os.system(cmd)
return not rc return not rc
def open(self, url, new=0, autoraise=1): def open(self, url, new=0, autoraise=1):
if new: assert "'" not in url
self._remote("openURL(%s, new-window)"%url, autoraise) if new == 0:
action = self.remote_action
elif new == 1:
action = self.remote_action_newwin
elif new == 2:
if self.remote_action_newtab is None:
action = self.remote_action_newwin
else:
action = self.remote_action_newtab
else: else:
self._remote("openURL(%s)" % url, autoraise) raise Error("Bad 'new' parameter to open(); expected 0, 1, or 2, got %s" % new)
return self._remote(url, action % url, autoraise)
def open_new(self, url):
self.open(url, 1)
class Mozilla(UnixBrowser):
"""Launcher class for Mozilla/Netscape browsers."""
class Galeon: raise_opts = ("-noraise", "-raise")
"""Launcher class for Galeon browsers."""
def __init__(self, name):
self.name = name
self.basename = os.path.basename(name)
def _remote(self, action, autoraise): remote_cmd = '-remote'
raise_opt = ("--noraise", "")[autoraise] remote_action = "openURL(%s)"
cmd = "%s %s %s >/dev/null 2>&1" % (self.name, raise_opt, action) remote_action_newwin = "openURL(%s,new-window)"
rc = os.system(cmd) remote_action_newtab = "openURL(%s,new-tab)"
if rc:
import time
os.system("%s >/dev/null 2>&1 &" % self.name)
time.sleep(PROCESS_CREATION_DELAY)
rc = os.system(cmd)
return not rc
def open(self, url, new=0, autoraise=1): Netscape = Mozilla
if new:
self._remote("-w '%s'" % url, autoraise)
else:
self._remote("-n '%s'" % url, autoraise)
def open_new(self, url):
self.open(url, 1) class Galeon(UnixBrowser):
"""Launcher class for Galeon/Epiphany browsers."""
raise_opts = ("-noraise", "")
remote_action = "-n '%s'"
remote_action_newwin = "-w '%s'"
remote_background = True
class Konqueror: class Konqueror(BaseBrowser):
"""Controller for the KDE File Manager (kfm, or Konqueror). """Controller for the KDE File Manager (kfm, or Konqueror).
See http://developer.kde.org/documentation/other/kfmclient.html See http://developer.kde.org/documentation/other/kfmclient.html
for more information on the Konqueror remote-control interface. for more information on the Konqueror remote-control interface.
""" """
def __init__(self):
if _iscommand("konqueror"):
self.name = self.basename = "konqueror"
else:
self.name = self.basename = "kfm"
def _remote(self, action): def _remote(self, url, action):
# kfmclient is the new KDE way of opening URLs.
cmd = "kfmclient %s >/dev/null 2>&1" % action cmd = "kfmclient %s >/dev/null 2>&1" % action
rc = os.system(cmd) rc = os.system(cmd)
# Fall back to other variants.
if rc: if rc:
import time if _iscommand("konqueror"):
if self.basename == "konqueror": rc = os.system(self.name + " --silent '%s' &" % url)
os.system(self.name + " --silent &") elif _iscommand("kfm"):
else: rc = os.system(self.name + " -d '%s'" % url)
os.system(self.name + " -d &")
time.sleep(PROCESS_CREATION_DELAY)
rc = os.system(cmd)
return not rc return not rc
def open(self, url, new=1, autoraise=1): def open(self, url, new=0, autoraise=1):
# XXX Currently I know no way to prevent KFM from # XXX Currently I know no way to prevent KFM from
# opening a new win. # opening a new win.
assert "'" not in url assert "'" not in url
self._remote("openURL '%s'" % url) if new == 2:
action = "newTab '%s'" % url
else:
action = "openURL '%s'" % url
ok = self._remote(url, action)
return ok
class Opera(UnixBrowser):
"Launcher class for Opera browser."
raise_opts = ("", "-raise")
remote_cmd = '-remote'
remote_action = "openURL(%s)"
remote_action_newwin = "openURL(%s,new-window)"
remote_action_newtab = "openURL(%s,new-page)"
open_new = open
class Elinks(UnixBrowser):
"Launcher class for Elinks browsers."
class Grail: remote_cmd = '-remote'
remote_action = "openURL(%s)"
remote_action_newwin = "openURL(%s,new-window)"
remote_action_newtab = "openURL(%s,new-tab)"
def _remote(self, url, action, autoraise):
# elinks doesn't like its stdout to be redirected -
# it uses redirected stdout as a signal to do -dump
cmd = "%s %s '%s' 2>/dev/null" % (self.name,
self.remote_cmd, action)
rc = os.system(cmd)
if rc:
rc = os.system("%s %s" % (self.name, url))
return not rc
class Grail(BaseBrowser):
# There should be a way to maintain a connection to Grail, but the # There should be a way to maintain a connection to Grail, but the
# Grail remote control protocol doesn't really allow that at this # Grail remote control protocol doesn't really allow that at this
# point. It probably neverwill! # point. It probably neverwill!
...@@ -237,93 +320,97 @@ class Grail: ...@@ -237,93 +320,97 @@ class Grail:
def open(self, url, new=0, autoraise=1): def open(self, url, new=0, autoraise=1):
if new: if new:
self._remote("LOADNEW " + url) ok = self._remote("LOADNEW " + url)
else: else:
self._remote("LOAD " + url) ok = self._remote("LOAD " + url)
return ok
def open_new(self, url):
self.open(url, 1)
class WindowsDefault:
def open(self, url, new=0, autoraise=1):
os.startfile(url)
def open_new(self, url):
self.open(url)
# #
# Platform support for Unix # Platform support for Unix
# #
# This is the right test because all these Unix browsers require either # These are the right tests because all these Unix browsers require either
# a console terminal of an X display to run. Note that we cannot split # a console terminal or an X display to run.
# the TERM and DISPLAY cases, because we might be running Python from inside
# an xterm.
if os.environ.get("TERM") or os.environ.get("DISPLAY"):
_tryorder = ["links", "lynx", "w3m"]
# Easy cases first -- register console browsers if we have them.
if os.environ.get("TERM"):
# The Links browser <http://artax.karlin.mff.cuni.cz/~mikulas/links/>
if _iscommand("links"):
register("links", None, GenericBrowser("links '%s'"))
# The Lynx browser <http://lynx.browser.org/>
if _iscommand("lynx"):
register("lynx", None, GenericBrowser("lynx '%s'"))
# The w3m browser <http://ei5nazha.yz.yamagata-u.ac.jp/~aito/w3m/eng/>
if _iscommand("w3m"):
register("w3m", None, GenericBrowser("w3m '%s'"))
# X browsers have more in the way of options # Prefer X browsers if present
if os.environ.get("DISPLAY"): if os.environ.get("DISPLAY"):
_tryorder = ["galeon", "skipstone",
"mozilla-firefox", "mozilla-firebird", "mozilla", "netscape",
"kfm", "grail"] + _tryorder
# First, the Netscape series # First, the Mozilla/Netscape browsers
for browser in ("mozilla-firefox", "mozilla-firebird", for browser in ("mozilla-firefox", "firefox",
"mozilla-firebird", "firebird",
"mozilla", "netscape"): "mozilla", "netscape"):
if _iscommand(browser): if _iscommand(browser):
register(browser, None, Netscape(browser)) register(browser, None, Mozilla(browser))
# Next, Mosaic -- old but still in use. # The default Gnome browser
if _iscommand("mosaic"): if _iscommand("gconftool-2"):
register("mosaic", None, GenericBrowser( # get the web browser string from gconftool
"mosaic '%s' >/dev/null &")) gc = 'gconftool-2 -g /desktop/gnome/url-handlers/http/command'
out = os.popen(gc)
commd = out.read().strip()
retncode = out.close()
# Gnome's Galeon # if successful, register it
if _iscommand("galeon"): if retncode == None and len(commd) != 0:
register("galeon", None, Galeon("galeon")) register("gnome", None, GenericBrowser(
commd + " '%s' >/dev/null &"))
# Skipstone, another Gtk/Mozilla based browser
if _iscommand("skipstone"):
register("skipstone", None, GenericBrowser(
"skipstone '%s' >/dev/null &"))
# Konqueror/kfm, the KDE browser. # Konqueror/kfm, the KDE browser.
if _iscommand("kfm") or _iscommand("konqueror"): if _iscommand("kfm") or _iscommand("konqueror"):
register("kfm", Konqueror, Konqueror()) register("kfm", Konqueror, Konqueror())
# Grail, the Python browser. # Gnome's Galeon and Epiphany
if _iscommand("grail"): for browser in ("galeon", "epiphany"):
register("grail", Grail, None) if _iscommand(browser):
register(browser, None, Galeon(browser))
# Skipstone, another Gtk/Mozilla based browser
if _iscommand("skipstone"):
register("skipstone", None, GenericBrowser("skipstone '%s' &"))
class InternetConfig: # Opera, quite popular
def open(self, url, new=0, autoraise=1): if _iscommand("opera"):
ic.launchurl(url) register("opera", None, Opera("opera"))
def open_new(self, url): # Next, Mosaic -- old but still in use.
self.open(url) if _iscommand("mosaic"):
register("mosaic", None, GenericBrowser("mosaic '%s' &"))
# Grail, the Python browser. Does anybody still use it?
if _iscommand("grail"):
register("grail", Grail, None)
# Also try console browsers
if os.environ.get("TERM"):
# The Links/elinks browsers <http://artax.karlin.mff.cuni.cz/~mikulas/links/>
if _iscommand("links"):
register("links", None, GenericBrowser("links '%s'"))
if _iscommand("elinks"):
register("elinks", None, Elinks("elinks"))
# The Lynx browser <http://lynx.isc.org/>, <http://lynx.browser.org/>
if _iscommand("lynx"):
register("lynx", None, GenericBrowser("lynx '%s'"))
# The w3m browser <http://w3m.sourceforge.net/>
if _iscommand("w3m"):
register("w3m", None, GenericBrowser("w3m '%s'"))
# #
# Platform support for Windows # Platform support for Windows
# #
if sys.platform[:3] == "win": if sys.platform[:3] == "win":
_tryorder = ["netscape", "windows-default"] class WindowsDefault(BaseBrowser):
def open(self, url, new=0, autoraise=1):
os.startfile(url)
return True # Oh, my...
_tryorder = []
_browsers = {}
# Prefer mozilla/netscape/opera if present
for browser in ("firefox", "firebird", "mozilla", "netscape", "opera"):
if _iscommand(browser):
register(browser, None, GenericBrowser(browser + ' %s'))
register("windows-default", WindowsDefault) register("windows-default", WindowsDefault)
# #
...@@ -335,36 +422,112 @@ try: ...@@ -335,36 +422,112 @@ try:
except ImportError: except ImportError:
pass pass
else: else:
# internet-config is the only supported controller on MacOS, class InternetConfig(BaseBrowser):
# so don't mess with the default! def open(self, url, new=0, autoraise=1):
_tryorder = ["internet-config"] ic.launchurl(url)
register("internet-config", InternetConfig) return True # Any way to get status?
register("internet-config", InternetConfig, update_tryorder=-1)
if sys.platform == 'darwin':
# Adapted from patch submitted to SourceForge by Steven J. Burr
class MacOSX(BaseBrowser):
"""Launcher class for Aqua browsers on Mac OS X
Optionally specify a browser name on instantiation. Note that this
will not work for Aqua browsers if the user has moved the application
package after installation.
If no browser is specified, the default browser, as specified in the
Internet System Preferences panel, will be used.
"""
def __init__(self, name):
self.name = name
def open(self, url, new=0, autoraise=1):
assert "'" not in url
# new must be 0 or 1
new = int(bool(new))
if self.name == "default":
# User called open, open_new or get without a browser parameter
script = _safequote('open location "%s"', url) # opens in default browser
else:
# User called get and chose a browser
if self.name == "OmniWeb":
toWindow = ""
else:
# Include toWindow parameter of OpenURL command for browsers
# that support it. 0 == new window; -1 == existing
toWindow = "toWindow %d" % (new - 1)
cmd = _safequote('OpenURL "%s"', url)
script = '''tell application "%s"
activate
%s %s
end tell''' % (self.name, cmd, toWindow)
# Open pipe to AppleScript through osascript command
osapipe = os.popen("osascript", "w")
if osapipe is None:
return False
# Write script to osascript's stdin
osapipe.write(script)
rc = osapipe.close()
return not rc
# Don't clear _tryorder or _browsers since OS X can use above Unix support
# (but we prefer using the OS X specific stuff)
register("MacOSX", None, MacOSX('default'), -1)
# #
# Platform support for OS/2 # Platform support for OS/2
# #
if sys.platform[:3] == "os2" and _iscommand("netscape.exe"): if sys.platform[:3] == "os2" and _iscommand("netscape"):
_tryorder = ["os2netscape"] _tryorder = []
_browsers = {}
register("os2netscape", None, register("os2netscape", None,
GenericBrowser("start netscape.exe %s")) GenericBrowser("start netscape %s"), -1)
# OK, now that we know what the default preference orders for each # OK, now that we know what the default preference orders for each
# platform are, allow user to override them with the BROWSER variable. # platform are, allow user to override them with the BROWSER variable.
#
if "BROWSER" in os.environ: if "BROWSER" in os.environ:
# It's the user's responsibility to register handlers for any unknown _userchoices = os.environ["BROWSER"].split(os.pathsep)
# browser referenced by this value, before calling open(). _userchoices.reverse()
_tryorder = os.environ["BROWSER"].split(os.pathsep)
# Treat choices in same way as if passed into get() but do register
for cmd in _tryorder: # and prepend to _tryorder
if not cmd.lower() in _browsers: for cmdline in _userchoices:
if _iscommand(cmd.lower()): if cmdline != '':
register(cmd.lower(), None, GenericBrowser( _synthesize(cmdline, -1)
"%s '%%s'" % cmd.lower())) cmdline = None # to make del work if _userchoices was empty
cmd = None # to make del work if _tryorder was empty del cmdline
del cmd del _userchoices
_tryorder = filter(lambda x: x.lower() in _browsers
or x.find("%s") > -1, _tryorder)
# what to do if _tryorder is now empty? # what to do if _tryorder is now empty?
def main():
import getopt
usage = """Usage: %s [-n | -t] url
-n: open new window
-t: open new tab""" % sys.argv[0]
try:
opts, args = getopt.getopt(sys.argv[1:], 'ntd')
except getopt.error, msg:
print >>sys.stderr, msg
print >>sys.stderr, usage
sys.exit(1)
new_win = 0
for o, a in opts:
if o == '-n': new_win = 1
elif o == '-t': new_win = 2
if len(args) <> 1:
print >>sys.stderr, usage
sys.exit(1)
url = args[0]
open(url, new_win)
if __name__ == "__main__":
main()
...@@ -254,6 +254,8 @@ Extension Modules ...@@ -254,6 +254,8 @@ Extension Modules
Library Library
------- -------
- Patch #754022: Greatly enhanced webbrowser.py (by Oleg Broytmann).
- Bug #729103: pydoc.py: Fix docother() method to accept additional - Bug #729103: pydoc.py: Fix docother() method to accept additional
"parent" argument. "parent" argument.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment