blob: 36dfdc7e145948aeaa587d83a4bbd37aff7ba1c9 [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
Fred Drakec70b4482000-07-09 16:45:56 +00006class Error(Exception):
7 pass
8
Eric S. Raymondf7f18512001-01-23 13:16:32 +00009_browsers = {} # Dictionary of available browser controllers
10_tryorder = [] # Preference order of available browsers
Fred Drakec70b4482000-07-09 16:45:56 +000011
12def register(name, klass, instance=None):
13 """Register a browser connector and, optionally, connection."""
14 _browsers[name.lower()] = [klass, instance]
15
Eric S. Raymondf7f18512001-01-23 13:16:32 +000016def get(using=None):
17 """Return a browser launcher instance appropriate for the environment."""
18 if using:
19 alternatives = [using]
20 else:
21 alternatives = _tryorder
22 for browser in alternatives:
23 if browser.find('%s') > -1:
24 # User gave us a command line, don't mess with it.
25 return browser
26 else:
27 # User gave us a browser name.
28 command = _browsers[browser.lower()]
29 if command[1] is None:
30 return command[0]()
31 else:
32 return command[1]
33 raise Error("could not locate runnable browser")
Fred Drakec70b4482000-07-09 16:45:56 +000034
35# Please note: the following definition hides a builtin function.
36
37def open(url, new=0):
38 get().open(url, new)
39
Eric S. Raymondf7f18512001-01-23 13:16:32 +000040def open_new(url): # Marked deprecated. May be removed in 2.1.
41 get().open(url, 1)
Fred Drakec70b4482000-07-09 16:45:56 +000042
Eric S. Raymondf7f18512001-01-23 13:16:32 +000043#
44# Everything after this point initializes _browsers and _tryorder,
45# then disappears. Some class definitions and instances remain
46# live through these globals, but only the minimum set needed to
47# support the user's platform.
48#
Fred Drakec70b4482000-07-09 16:45:56 +000049
Eric S. Raymondf7f18512001-01-23 13:16:32 +000050#
51# Platform support for Unix
52#
Fred Drakec70b4482000-07-09 16:45:56 +000053
Eric S. Raymondf7f18512001-01-23 13:16:32 +000054# This is the right test because all these Unix browsers require either
55# a console terminal of an X display to run. Note that we cannot split
56# the TERM and DISPLAY cases, because we might be running Python from inside
57# an xterm.
58if os.environ.get("TERM") or os.environ.get("DISPLAY"):
59 PROCESS_CREATION_DELAY = 4
60 global tryorder
61 _tryorder = ("mozilla","netscape","kfm","grail","links","lynx","w3m")
Fred Drakec70b4482000-07-09 16:45:56 +000062
Eric S. Raymondf7f18512001-01-23 13:16:32 +000063 def _iscommand(cmd):
64 """Return true if cmd can be found on the executable search path."""
65 path = os.environ.get("PATH")
66 if not path:
Fred Drakec70b4482000-07-09 16:45:56 +000067 return 0
Eric S. Raymondf7f18512001-01-23 13:16:32 +000068 for d in path.split(os.pathsep):
69 exe = os.path.join(d, cmd)
70 if os.path.isfile(exe):
71 return 1
72 return 0
Fred Drakec70b4482000-07-09 16:45:56 +000073
Eric S. Raymondf7f18512001-01-23 13:16:32 +000074 class GenericBrowser:
75 def __init__(self, cmd):
76 self.command = cmd
Fred Drakec70b4482000-07-09 16:45:56 +000077
Eric S. Raymondf7f18512001-01-23 13:16:32 +000078 def open(self, url, new=0):
79 os.system(self.command % url)
Fred Drakec70b4482000-07-09 16:45:56 +000080
Eric S. Raymondf7f18512001-01-23 13:16:32 +000081 def open_new(self, url): # Deprecated. May be removed in 2.1.
82 self.open(url)
Fred Drakec70b4482000-07-09 16:45:56 +000083
Eric S. Raymondf7f18512001-01-23 13:16:32 +000084 # Easy cases first -- register console browsers if we have them.
85 if os.environ.get("TERM"):
86 # The Links browser <http://artax.karlin.mff.cuni.cz/~mikulas/links/>
87 if _iscommand("links"):
88 register("links", None, GenericBrowser("links %s"))
89 # The Lynx browser <http://lynx.browser.org/>
90 if _iscommand("lynx"):
91 register("lynx", None, GenericBrowser("lynx %s"))
92 # The w3m browser <http://ei5nazha.yz.yamagata-u.ac.jp/~aito/w3m/eng/>
93 if _iscommand("w3m"):
94 register("w3m", None, GenericBrowser("w3m %s"))
Fred Drakec70b4482000-07-09 16:45:56 +000095
Eric S. Raymondf7f18512001-01-23 13:16:32 +000096 # X browsers have mre in the way of options
97 if os.environ.get("DISPLAY"):
98 # First, the Netscape series
99 if _iscommand("netscape") or _iscommand("mozilla"):
100 class Netscape:
101 "Launcher class for Netscape browsers."
102 autoRaise = 1
Fred Drakec70b4482000-07-09 16:45:56 +0000103
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000104 def __init__(self, name):
105 self.name = name
Fred Drakec70b4482000-07-09 16:45:56 +0000106
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000107 def _remote(self, action):
108 raise_opt = ("-noraise", "-raise")[self.autoRaise]
109 cmd = "%s %s -remote '%s' >/dev/null 2>&1" % (self.name, raise_opt, action)
110 rc = os.system(cmd)
111 if rc:
112 import time
113 os.system("%s -no-about-splash &" % self.name)
114 time.sleep(PROCESS_CREATION_DELAY)
115 rc = os.system(cmd)
116 return not rc
Fred Drakec70b4482000-07-09 16:45:56 +0000117
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000118 def open(self, url, new=0):
119 if new:
120 self._remote("openURL(%s, new-window)" % url)
121 else:
122 self._remote("openURL(%s)" % url)
123
124 # Deprecated. May be removed in 2.1.
125 def open_new(self, url):
126 self.open(url, 1)
127
128 if _iscommand("mozilla"):
129 register("mozilla", None, Netscape("mozilla"))
130 if _iscommand("netscape"):
131 register("netscape", None, Netscape("netscape"))
132
133 # Next, Mosaic -- old but still in use.
134 if _iscommand("mosaic"):
135 register("mosaic", None, GenericBrowser("mosaic %s >/dev/null &"))
136
137 # Konqueror/kfm, the KDE browser.
138 if _iscommand("kfm"):
139 class Konqueror:
140 """Controller for the KDE File Manager (kfm, or Konqueror).
141
142 See http://developer.kde.org/documentation/other/kfmclient.html
143 for more information on the Konqueror remote-control interface.
144
145 """
146 def _remote(self, action):
147 cmd = "kfmclient %s >/dev/null 2>&1" % action
148 rc = os.system(cmd)
149 if rc:
150 import time
151 os.system("kfm -d &")
152 time.sleep(PROCESS_CREATION_DELAY)
153 rc = os.system(cmd)
154 return not rc
155
156 def open(self, url, new=1):
157 # XXX Currently I know no way to prevent KFM from opening a new win.
158 self._remote("openURL %s" % url)
159
160 # Deprecated. May be removed in 2.1.
161 open_new = open
162
163
164 register("kfm", Konqueror, None)
165
166 # Grail, the Python browser.
167 if _iscommand("grail"):
168 class Grail:
169 # There should be a way to maintain a connection to
170 # Grail, but the Grail remote control protocol doesn't
171 # really allow that at this point. It probably neverwill!
172 def _find_grail_rc(self):
173 import glob
174 import pwd
175 import socket
176 import tempfile
177 tempdir = os.path.join(tempfile.gettempdir(), ".grail-unix")
178 user = pwd.getpwuid(_os.getuid())[0]
179 filename = os.path.join(tempdir, user + "-*")
180 maybes = glob.glob(filename)
181 if not maybes:
182 return None
183 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
184 for fn in maybes:
185 # need to PING each one until we find one that's live
186 try:
187 s.connect(fn)
188 except socket.error:
189 # no good; attempt to clean it out, but don't fail:
190 try:
191 os.unlink(fn)
192 except IOError:
193 pass
194 else:
195 return s
196
197 def _remote(self, action):
198 s = self._find_grail_rc()
199 if not s:
200 return 0
201 s.send(action)
202 s.close()
203 return 1
204
205 def open(self, url, new=0):
206 if new:
207 self._remote("LOADNEW " + url)
208 else:
209 self._remote("LOAD " + url)
210
211 # Deprecated. May be removed in 2.1.
212 def open_new(self, url):
213 self.open(url, 1)
214
215 register("grail", Grail, None)
216
217#
218# Platform support for Windows
219#
Fred Drakec70b4482000-07-09 16:45:56 +0000220
221if sys.platform[:3] == "win":
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000222 global _tryorder
223 _tryorder = ("netscape", "windows-default")
224
225 class WindowsDefault:
226 def open(self, url, new=0):
227 os.startfile(url)
228
229 def open_new(self, url): # Deprecated. May be removed in 2.1.
230 self.open(url)
231
Fred Drakec70b4482000-07-09 16:45:56 +0000232 register("windows-default", WindowsDefault)
Fred Drakec70b4482000-07-09 16:45:56 +0000233
Fred Drakec70b4482000-07-09 16:45:56 +0000234#
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000235# Platform support for MacOS
236#
Fred Drakec70b4482000-07-09 16:45:56 +0000237
238try:
239 import ic
240except ImportError:
241 pass
242else:
243 class InternetConfig:
244 def open(self, url, new=0):
Guido van Rossum2595a832000-11-13 20:30:57 +0000245 ic.launchurl(url)
Fred Drakec70b4482000-07-09 16:45:56 +0000246
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000247 def open_new(self, url): # Deprecated. May be removed in 2.1.
Fred Drakec70b4482000-07-09 16:45:56 +0000248 self.open(url)
249
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000250 # internet-config is the only supported controller on MacOS,
251 # so don't mess with the default!
252 _tryorder = ("internet-config")
Fred Drakec70b4482000-07-09 16:45:56 +0000253 register("internet-config", InternetConfig)
Eric S. Raymondf7f18512001-01-23 13:16:32 +0000254
255# OK, now that we know what the default preference orders for each
256# platform are, allow user to override them with the BROWSER variable.
257#
258if os.environ.has_key("BROWSER"):
259 # It's the user's responsibility to register handlers for any unknown
260 # browser referenced by this value, before calling open().
261 _tryorder = os.environ["BROWSER"].split(":")
262else:
263 # Optimization: filter out alternatives that aren't available, so we can
264 # avoid has_key() tests at runtime. (This may also allow some unused
265 # classes and class-instance storage to be garbage-collected.)
266 _tryorder = filter(lambda x: _browsers.has_key(x.lower()) or x.find("%s")>-1,\
267 _tryorder)
268
269# end