blob: 6a60c24590e5065ac39337ee67e9fc9e246c3fb0 [file] [log] [blame]
Eric S. Raymondf7f18512001-01-23 13:16:32 +00001"""Remote-control interfaces to common browsers."""
Fred Drakec70b4482000-07-09 16:45:56 +00002
3import os
4import sys
5
Skip Montanaro40fc1602001-03-01 04:27:19 +00006__all__ = ["Error", "open", "get", "register"]
7
Fred Drakec70b4482000-07-09 16:45:56 +00008class Error(Exception):
9 pass
10
Tim Peters658cba62001-02-09 20:06:00 +000011_browsers = {} # Dictionary of available browser controllers
12_tryorder = [] # Preference order of available browsers
Fred Drakec70b4482000-07-09 16:45:56 +000013
14def register(name, klass, instance=None):
15 """Register a browser connector and, optionally, connection."""
16 _browsers[name.lower()] = [klass, instance]
17
Eric S. Raymondf7f18512001-01-23 13:16:32 +000018def get(using=None):
19 """Return a browser launcher instance appropriate for the environment."""
20 if using:
21 alternatives = [using]
22 else:
23 alternatives = _tryorder
24 for browser in alternatives:
25 if browser.find('%s') > -1:
26 # User gave us a command line, don't mess with it.
27 return browser
28 else:
Tim Peters658cba62001-02-09 20:06:00 +000029 # User gave us a browser name.
Eric S. Raymondf7f18512001-01-23 13:16:32 +000030 command = _browsers[browser.lower()]
31 if command[1] is None:
32 return command[0]()
33 else:
34 return command[1]
35 raise Error("could not locate runnable browser")
Fred Drakec70b4482000-07-09 16:45:56 +000036
37# Please note: the following definition hides a builtin function.
38
Eric S. Raymondf79cb2d2001-01-23 13:49:44 +000039def open(url, new=0, autoraise=1):
40 get().open(url, new, autoraise)
Fred Drakec70b4482000-07-09 16:45:56 +000041
Tim Peters658cba62001-02-09 20:06:00 +000042def open_new(url): # Marked deprecated. May be removed in 2.1.
Eric S. Raymondf7f18512001-01-23 13:16:32 +000043 get().open(url, 1)
Fred Drakec70b4482000-07-09 16:45:56 +000044
Eric S. Raymondf7f18512001-01-23 13:16:32 +000045#
46# Everything after this point initializes _browsers and _tryorder,
47# then disappears. Some class definitions and instances remain
48# live through these globals, but only the minimum set needed to
49# support the user's platform.
50#
Fred Drakec70b4482000-07-09 16:45:56 +000051
Tim Peters658cba62001-02-09 20:06:00 +000052#
Eric S. Raymondf7f18512001-01-23 13:16:32 +000053# Platform support for Unix
54#
Fred Drakec70b4482000-07-09 16:45:56 +000055
Eric S. Raymondf7f18512001-01-23 13:16:32 +000056# This is the right test because all these Unix browsers require either
57# a console terminal of an X display to run. Note that we cannot split
58# the TERM and DISPLAY cases, because we might be running Python from inside
59# an xterm.
60if os.environ.get("TERM") or os.environ.get("DISPLAY"):
61 PROCESS_CREATION_DELAY = 4
Eric S. Raymondf7f18512001-01-23 13:16:32 +000062 _tryorder = ("mozilla","netscape","kfm","grail","links","lynx","w3m")
Fred Drakec70b4482000-07-09 16:45:56 +000063
Eric S. Raymondf7f18512001-01-23 13:16:32 +000064 def _iscommand(cmd):
65 """Return true if cmd can be found on the executable search path."""
66 path = os.environ.get("PATH")
67 if not path:
Fred Drakec70b4482000-07-09 16:45:56 +000068 return 0
Eric S. Raymondf7f18512001-01-23 13:16:32 +000069 for d in path.split(os.pathsep):
70 exe = os.path.join(d, cmd)
71 if os.path.isfile(exe):
72 return 1
73 return 0
Fred Drakec70b4482000-07-09 16:45:56 +000074
Eric S. Raymondf7f18512001-01-23 13:16:32 +000075 class GenericBrowser:
76 def __init__(self, cmd):
77 self.command = cmd
Fred Drakec70b4482000-07-09 16:45:56 +000078
Eric S. Raymondcfa40962001-01-23 15:49:34 +000079 def open(self, url, new=0, autoraise=1):
Eric S. Raymondf7f18512001-01-23 13:16:32 +000080 os.system(self.command % url)
Fred Drakec70b4482000-07-09 16:45:56 +000081
Tim Peters658cba62001-02-09 20:06:00 +000082 def open_new(self, url): # Deprecated. May be removed in 2.1.
Eric S. Raymondf7f18512001-01-23 13:16:32 +000083 self.open(url)
Fred Drakec70b4482000-07-09 16:45:56 +000084
Eric S. Raymondf7f18512001-01-23 13:16:32 +000085 # Easy cases first -- register console browsers if we have them.
86 if os.environ.get("TERM"):
87 # The Links browser <http://artax.karlin.mff.cuni.cz/~mikulas/links/>
88 if _iscommand("links"):
89 register("links", None, GenericBrowser("links %s"))
90 # The Lynx browser <http://lynx.browser.org/>
91 if _iscommand("lynx"):
92 register("lynx", None, GenericBrowser("lynx %s"))
93 # The w3m browser <http://ei5nazha.yz.yamagata-u.ac.jp/~aito/w3m/eng/>
94 if _iscommand("w3m"):
95 register("w3m", None, GenericBrowser("w3m %s"))
Fred Drakec70b4482000-07-09 16:45:56 +000096
Eric S. Raymondf7f18512001-01-23 13:16:32 +000097 # X browsers have mre in the way of options
98 if os.environ.get("DISPLAY"):
99 # First, the Netscape series
100 if _iscommand("netscape") or _iscommand("mozilla"):
101 class Netscape:
102 "Launcher class for Netscape browsers."
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000103 def __init__(self, name):
104 self.name = name
Fred Drakec70b4482000-07-09 16:45:56 +0000105
Eric S. Raymondf79cb2d2001-01-23 13:49:44 +0000106 def _remote(self, action, autoraise):
107 raise_opt = ("-noraise", "-raise")[autoraise]
Jeremy Hylton8016a4b2001-02-27 18:44:14 +0000108 cmd = "%s %s -remote '%s' >/dev/null 2>&1" % (self.name,
109 raise_opt,
110 action)
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000111 rc = os.system(cmd)
112 if rc:
113 import time
114 os.system("%s -no-about-splash &" % self.name)
115 time.sleep(PROCESS_CREATION_DELAY)
116 rc = os.system(cmd)
117 return not rc
Fred Drakec70b4482000-07-09 16:45:56 +0000118
Eric S. Raymondf79cb2d2001-01-23 13:49:44 +0000119 def open(self, url, new=0, autoraise=1):
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000120 if new:
Eric S. Raymondf79cb2d2001-01-23 13:49:44 +0000121 self._remote("openURL(%s, new-window)"%url, autoraise)
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000122 else:
Eric S. Raymondf79cb2d2001-01-23 13:49:44 +0000123 self._remote("openURL(%s)" % url, autoraise)
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000124
125 # Deprecated. May be removed in 2.1.
126 def open_new(self, url):
127 self.open(url, 1)
128
129 if _iscommand("mozilla"):
130 register("mozilla", None, Netscape("mozilla"))
131 if _iscommand("netscape"):
132 register("netscape", None, Netscape("netscape"))
133
134 # Next, Mosaic -- old but still in use.
135 if _iscommand("mosaic"):
136 register("mosaic", None, GenericBrowser("mosaic %s >/dev/null &"))
137
138 # Konqueror/kfm, the KDE browser.
139 if _iscommand("kfm"):
140 class Konqueror:
141 """Controller for the KDE File Manager (kfm, or Konqueror).
142
143 See http://developer.kde.org/documentation/other/kfmclient.html
144 for more information on the Konqueror remote-control interface.
145
146 """
147 def _remote(self, action):
148 cmd = "kfmclient %s >/dev/null 2>&1" % action
149 rc = os.system(cmd)
150 if rc:
151 import time
152 os.system("kfm -d &")
153 time.sleep(PROCESS_CREATION_DELAY)
154 rc = os.system(cmd)
155 return not rc
156
Eric S. Raymondcfa40962001-01-23 15:49:34 +0000157 def open(self, url, new=1, autoraise=1):
Jeremy Hylton8016a4b2001-02-27 18:44:14 +0000158 # XXX Currently I know no way to prevent KFM from
Tim Peters85ba6732001-02-28 08:26:44 +0000159 # opening a new win.
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000160 self._remote("openURL %s" % url)
161
162 # Deprecated. May be removed in 2.1.
163 open_new = open
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000164
165 register("kfm", Konqueror, None)
166
167 # Grail, the Python browser.
168 if _iscommand("grail"):
169 class Grail:
170 # There should be a way to maintain a connection to
171 # Grail, but the Grail remote control protocol doesn't
172 # really allow that at this point. It probably neverwill!
173 def _find_grail_rc(self):
174 import glob
175 import pwd
176 import socket
177 import tempfile
Jeremy Hylton8016a4b2001-02-27 18:44:14 +0000178 tempdir = os.path.join(tempfile.gettempdir(),
179 ".grail-unix")
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000180 user = pwd.getpwuid(_os.getuid())[0]
181 filename = os.path.join(tempdir, user + "-*")
182 maybes = glob.glob(filename)
183 if not maybes:
184 return None
185 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
186 for fn in maybes:
187 # need to PING each one until we find one that's live
188 try:
189 s.connect(fn)
190 except socket.error:
191 # no good; attempt to clean it out, but don't fail:
192 try:
193 os.unlink(fn)
194 except IOError:
195 pass
196 else:
197 return s
198
199 def _remote(self, action):
200 s = self._find_grail_rc()
201 if not s:
202 return 0
203 s.send(action)
204 s.close()
205 return 1
206
Eric S. Raymondcfa40962001-01-23 15:49:34 +0000207 def open(self, url, new=0, autoraise=1):
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000208 if new:
209 self._remote("LOADNEW " + url)
210 else:
211 self._remote("LOAD " + url)
212
213 # Deprecated. May be removed in 2.1.
214 def open_new(self, url):
215 self.open(url, 1)
216
217 register("grail", Grail, None)
218
219#
220# Platform support for Windows
221#
Fred Drakec70b4482000-07-09 16:45:56 +0000222
223if sys.platform[:3] == "win":
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000224 _tryorder = ("netscape", "windows-default")
225
226 class WindowsDefault:
Eric S. Raymondcfa40962001-01-23 15:49:34 +0000227 def open(self, url, new=0, autoraise=1):
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000228 os.startfile(url)
229
230 def open_new(self, url): # Deprecated. May be removed in 2.1.
231 self.open(url)
232
Fred Drakec70b4482000-07-09 16:45:56 +0000233 register("windows-default", WindowsDefault)
Fred Drakec70b4482000-07-09 16:45:56 +0000234
Fred Drakec70b4482000-07-09 16:45:56 +0000235#
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000236# Platform support for MacOS
237#
Fred Drakec70b4482000-07-09 16:45:56 +0000238
239try:
240 import ic
241except ImportError:
242 pass
243else:
244 class InternetConfig:
Eric S. Raymondcfa40962001-01-23 15:49:34 +0000245 def open(self, url, new=0, autoraise=1):
Guido van Rossum2595a832000-11-13 20:30:57 +0000246 ic.launchurl(url)
Fred Drakec70b4482000-07-09 16:45:56 +0000247
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000248 def open_new(self, url): # Deprecated. May be removed in 2.1.
Fred Drakec70b4482000-07-09 16:45:56 +0000249 self.open(url)
250
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000251 # internet-config is the only supported controller on MacOS,
252 # so don't mess with the default!
253 _tryorder = ("internet-config")
Fred Drakec70b4482000-07-09 16:45:56 +0000254 register("internet-config", InternetConfig)
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000255
256# OK, now that we know what the default preference orders for each
257# platform are, allow user to override them with the BROWSER variable.
258#
259if os.environ.has_key("BROWSER"):
260 # It's the user's responsibility to register handlers for any unknown
261 # browser referenced by this value, before calling open().
262 _tryorder = os.environ["BROWSER"].split(":")
263else:
264 # Optimization: filter out alternatives that aren't available, so we can
265 # avoid has_key() tests at runtime. (This may also allow some unused
266 # classes and class-instance storage to be garbage-collected.)
Jeremy Hylton8016a4b2001-02-27 18:44:14 +0000267 _tryorder = filter(lambda x: _browsers.has_key(x.lower())
268 or x.find("%s") > -1, _tryorder)
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000269
270# end