1 """Interfaces for launching and remotely controlling Web browsers."""
6 __all__ = [
"Error",
"open",
"get",
"register"]
15 """Register a browser connector and, optionally, connection."""
16 _browsers[name.lower()] = [klass, instance]
19 """Return a browser launcher instance appropriate for the environment."""
21 alternatives = [using]
23 alternatives = _tryorder
24 for browser
in alternatives:
25 if browser.find(
'%s') > -1:
31 command = _browsers[browser.lower()]
33 command = _synthesize(browser)
34 if command[1]
is None:
38 raise Error(
"could not locate runnable browser")
42 def open(url, new=0, autoraise=1):
49 def _synthesize(browser):
50 """Attempt to synthesize a controller base on existing controllers.
52 This is useful to create a controller when a user specifies a path to
53 an entry in the BROWSER environment variable -- we can copy a general
54 controller to operate using a specific installation of the desired
57 If we can't create a controller in this way, or if there is no
58 executable for the requested browser, return [None, None].
61 if not os.path.exists(browser):
63 name = os.path.basename(browser)
65 command = _browsers[name.lower()]
69 controller = command[1]
70 if controller
and name.lower() == controller.basename:
73 controller.name = browser
74 controller.basename = os.path.basename(browser)
76 return [
None, controller]
81 """Return true if cmd can be found on the executable search path."""
82 path = os.environ.get(
"PATH")
85 for d
in path.split(os.pathsep):
86 exe = os.path.join(d, cmd)
87 if os.path.isfile(exe):
92 PROCESS_CREATION_DELAY = 4
97 self.name, self.
args = cmd.split(
None, 1)
100 def open(self, url, new=0, autoraise=1):
101 assert "'" not in url
102 command =
"%s %s" % (self.name, self.
args)
103 os.system(command % url)
110 "Launcher class for Netscape browsers."
115 def _remote(self, action, autoraise):
116 raise_opt = (
"-noraise",
"-raise")[autoraise]
117 cmd =
"%s %s -remote '%s' >/dev/null 2>&1" % (self.
name,
123 os.system(
"%s &" % self.
name)
124 time.sleep(PROCESS_CREATION_DELAY)
128 def open(self, url, new=0, autoraise=1):
130 self.
_remote(
"openURL(%s, new-window)"%url, autoraise)
132 self.
_remote(
"openURL(%s)" % url, autoraise)
139 """Controller for the KDE File Manager (kfm, or Konqueror).
141 See http://developer.kde.org/documentation/other/kfmclient.html
142 for more information on the Konqueror remote-control interface.
146 if _iscommand(
"konqueror"):
151 def _remote(self, action):
152 assert "'" not in action
153 cmd =
"kfmclient '%s' >/dev/null 2>&1" % action
158 os.system(self.
name +
" --silent &")
160 os.system(self.
name +
" -d &")
161 time.sleep(PROCESS_CREATION_DELAY)
165 def open(self, url, new=1, autoraise=1):
168 self.
_remote(
"openURL '%s'" % url)
177 def _find_grail_rc(self):
184 user = pwd.getpwuid(os.getuid())[0]
185 filename = os.path.join(tempdir, user +
"-*")
203 def _remote(self, action):
211 def open(self, url, new=0, autoraise=1):
222 def open(self, url, new=0, autoraise=1):
236 if os.environ.get(
"TERM")
or os.environ.get(
"DISPLAY"):
237 _tryorder = [
"mozilla",
"netscape",
"kfm",
"grail",
"links",
"lynx",
"w3m"]
240 if os.environ.get(
"TERM"):
242 if _iscommand(
"links"):
245 if _iscommand(
"lynx"):
248 if _iscommand(
"w3m"):
252 if os.environ.get(
"DISPLAY"):
254 if _iscommand(
"mozilla"):
256 if _iscommand(
"netscape"):
260 if _iscommand(
"mosaic"):
262 "mosaic '%s' >/dev/null &"))
265 if _iscommand(
"kfm")
or _iscommand(
"konqueror"):
269 if _iscommand(
"grail"):
274 def open(self, url, new=0, autoraise=1):
285 if sys.platform[:3] ==
"win":
286 _tryorder = [
"netscape",
"windows-default"]
287 register(
"windows-default", WindowsDefault)
300 _tryorder = [
"internet-config"]
301 register(
"internet-config", InternetConfig)
307 if sys.platform[:3] ==
"os2" and _iscommand(
"netscape.exe"):
308 _tryorder = [
"os2netscape"]
315 if os.environ.has_key(
"BROWSER"):
318 _tryorder = os.environ[
"BROWSER"].
split(os.pathsep)
320 for cmd
in _tryorder:
321 if not _browsers.has_key(cmd.lower()):
322 if _iscommand(cmd.lower()):
324 "%s '%%s'" % cmd.lower()))
326 _tryorder =
filter(
lambda x: _browsers.has_key(x.lower())
327 or x.find(
"%s") > -1, _tryorder)