Ka-Ping Yee | 0a8c29b | 2001-03-02 02:01:40 +0000 | [diff] [blame] | 1 | """Interfaces for launching and remotely controlling Web browsers.""" |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 6 | __all__ = ["Error", "open", "get", "register"] |
| 7 | |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 8 | class Error(Exception): |
| 9 | pass |
| 10 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 11 | _browsers = {} # Dictionary of available browser controllers |
| 12 | _tryorder = [] # Preference order of available browsers |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 13 | |
| 14 | def register(name, klass, instance=None): |
| 15 | """Register a browser connector and, optionally, connection.""" |
| 16 | _browsers[name.lower()] = [klass, instance] |
| 17 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 18 | def get(using=None): |
| 19 | """Return a browser launcher instance appropriate for the environment.""" |
Raymond Hettinger | 10ff706 | 2002-06-02 03:04:52 +0000 | [diff] [blame] | 20 | if using is not None: |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 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. |
Eric S. Raymond | f7eb4fa | 2001-03-31 01:50:52 +0000 | [diff] [blame] | 27 | return GenericBrowser(browser) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 28 | else: |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 29 | # User gave us a browser name. |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 30 | try: |
| 31 | command = _browsers[browser.lower()] |
| 32 | except KeyError: |
| 33 | command = _synthesize(browser) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 34 | if command[1] is None: |
| 35 | return command[0]() |
| 36 | else: |
| 37 | return command[1] |
| 38 | raise Error("could not locate runnable browser") |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 39 | |
| 40 | # Please note: the following definition hides a builtin function. |
| 41 | |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 42 | def open(url, new=0, autoraise=1): |
| 43 | get().open(url, new, autoraise) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 44 | |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 45 | def open_new(url): |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 46 | get().open(url, 1) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 47 | |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 48 | |
| 49 | def _synthesize(browser): |
| 50 | """Attempt to synthesize a controller base on existing controllers. |
| 51 | |
| 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 |
| 55 | browser in this way. |
| 56 | |
| 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]. |
| 59 | |
| 60 | """ |
| 61 | if not os.path.exists(browser): |
| 62 | return [None, None] |
| 63 | name = os.path.basename(browser) |
| 64 | try: |
| 65 | command = _browsers[name.lower()] |
| 66 | except KeyError: |
| 67 | return [None, None] |
| 68 | # now attempt to clone to fit the new name: |
| 69 | controller = command[1] |
| 70 | if controller and name.lower() == controller.basename: |
| 71 | import copy |
| 72 | controller = copy.copy(controller) |
| 73 | controller.name = browser |
| 74 | controller.basename = os.path.basename(browser) |
| 75 | register(browser, None, controller) |
| 76 | return [None, controller] |
Andrew M. Kuchling | 118aa53 | 2001-08-13 14:37:23 +0000 | [diff] [blame] | 77 | return [None, None] |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 78 | |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 79 | |
| 80 | def _iscommand(cmd): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 81 | """Return True if cmd can be found on the executable search path.""" |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 82 | path = os.environ.get("PATH") |
| 83 | if not path: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 84 | return False |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 85 | for d in path.split(os.pathsep): |
| 86 | exe = os.path.join(d, cmd) |
| 87 | if os.path.isfile(exe): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 88 | return True |
| 89 | return False |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | PROCESS_CREATION_DELAY = 4 |
| 93 | |
| 94 | |
| 95 | class GenericBrowser: |
| 96 | def __init__(self, cmd): |
| 97 | self.name, self.args = cmd.split(None, 1) |
| 98 | self.basename = os.path.basename(self.name) |
| 99 | |
| 100 | def open(self, url, new=0, autoraise=1): |
Fred Drake | 925f144 | 2002-01-07 15:29:01 +0000 | [diff] [blame] | 101 | assert "'" not in url |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 102 | command = "%s %s" % (self.name, self.args) |
| 103 | os.system(command % url) |
| 104 | |
| 105 | def open_new(self, url): |
| 106 | self.open(url) |
| 107 | |
| 108 | |
| 109 | class Netscape: |
| 110 | "Launcher class for Netscape browsers." |
| 111 | def __init__(self, name): |
| 112 | self.name = name |
| 113 | self.basename = os.path.basename(name) |
| 114 | |
| 115 | def _remote(self, action, autoraise): |
| 116 | raise_opt = ("-noraise", "-raise")[autoraise] |
| 117 | cmd = "%s %s -remote '%s' >/dev/null 2>&1" % (self.name, |
| 118 | raise_opt, |
| 119 | action) |
| 120 | rc = os.system(cmd) |
| 121 | if rc: |
| 122 | import time |
| 123 | os.system("%s &" % self.name) |
| 124 | time.sleep(PROCESS_CREATION_DELAY) |
| 125 | rc = os.system(cmd) |
| 126 | return not rc |
| 127 | |
| 128 | def open(self, url, new=0, autoraise=1): |
| 129 | if new: |
| 130 | self._remote("openURL(%s, new-window)"%url, autoraise) |
| 131 | else: |
| 132 | self._remote("openURL(%s)" % url, autoraise) |
| 133 | |
| 134 | def open_new(self, url): |
| 135 | self.open(url, 1) |
| 136 | |
| 137 | |
Neal Norwitz | 8dd28eb | 2002-10-10 22:49:29 +0000 | [diff] [blame] | 138 | class Galeon: |
| 139 | """Launcher class for Galeon browsers.""" |
| 140 | def __init__(self, name): |
| 141 | self.name = name |
| 142 | self.basename = os.path.basename(name) |
| 143 | |
| 144 | def _remote(self, action, autoraise): |
| 145 | raise_opt = ("--noraise", "")[autoraise] |
| 146 | cmd = "%s %s %s >/dev/null 2>&1" % (self.name, raise_opt, action) |
| 147 | rc = os.system(cmd) |
| 148 | if rc: |
| 149 | import time |
| 150 | os.system("%s >/dev/null 2>&1 &" % self.name) |
| 151 | time.sleep(PROCESS_CREATION_DELAY) |
| 152 | rc = os.system(cmd) |
| 153 | return not rc |
| 154 | |
| 155 | def open(self, url, new=0, autoraise=1): |
| 156 | if new: |
| 157 | self._remote("-w '%s'" % url, autoraise) |
| 158 | else: |
| 159 | self._remote("-n '%s'" % url, autoraise) |
| 160 | |
| 161 | def open_new(self, url): |
| 162 | self.open(url, 1) |
| 163 | |
| 164 | |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 165 | class Konqueror: |
| 166 | """Controller for the KDE File Manager (kfm, or Konqueror). |
| 167 | |
| 168 | See http://developer.kde.org/documentation/other/kfmclient.html |
| 169 | for more information on the Konqueror remote-control interface. |
| 170 | |
| 171 | """ |
| 172 | def __init__(self): |
| 173 | if _iscommand("konqueror"): |
| 174 | self.name = self.basename = "konqueror" |
| 175 | else: |
| 176 | self.name = self.basename = "kfm" |
| 177 | |
| 178 | def _remote(self, action): |
Neal Norwitz | 520cdf7 | 2002-10-11 22:04:22 +0000 | [diff] [blame] | 179 | cmd = "kfmclient %s >/dev/null 2>&1" % action |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 180 | rc = os.system(cmd) |
| 181 | if rc: |
| 182 | import time |
| 183 | if self.basename == "konqueror": |
| 184 | os.system(self.name + " --silent &") |
| 185 | else: |
| 186 | os.system(self.name + " -d &") |
| 187 | time.sleep(PROCESS_CREATION_DELAY) |
| 188 | rc = os.system(cmd) |
| 189 | return not rc |
| 190 | |
| 191 | def open(self, url, new=1, autoraise=1): |
| 192 | # XXX Currently I know no way to prevent KFM from |
| 193 | # opening a new win. |
Neal Norwitz | 520cdf7 | 2002-10-11 22:04:22 +0000 | [diff] [blame] | 194 | assert "'" not in url |
Fred Drake | 925f144 | 2002-01-07 15:29:01 +0000 | [diff] [blame] | 195 | self._remote("openURL '%s'" % url) |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 196 | |
| 197 | open_new = open |
| 198 | |
| 199 | |
| 200 | class Grail: |
| 201 | # There should be a way to maintain a connection to Grail, but the |
| 202 | # Grail remote control protocol doesn't really allow that at this |
| 203 | # point. It probably neverwill! |
| 204 | def _find_grail_rc(self): |
| 205 | import glob |
| 206 | import pwd |
| 207 | import socket |
| 208 | import tempfile |
| 209 | tempdir = os.path.join(tempfile.gettempdir(), |
| 210 | ".grail-unix") |
Fred Drake | 16623fe | 2001-10-13 16:00:52 +0000 | [diff] [blame] | 211 | user = pwd.getpwuid(os.getuid())[0] |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 212 | filename = os.path.join(tempdir, user + "-*") |
| 213 | maybes = glob.glob(filename) |
| 214 | if not maybes: |
| 215 | return None |
| 216 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 217 | for fn in maybes: |
| 218 | # need to PING each one until we find one that's live |
| 219 | try: |
| 220 | s.connect(fn) |
| 221 | except socket.error: |
| 222 | # no good; attempt to clean it out, but don't fail: |
| 223 | try: |
| 224 | os.unlink(fn) |
| 225 | except IOError: |
| 226 | pass |
| 227 | else: |
| 228 | return s |
| 229 | |
| 230 | def _remote(self, action): |
| 231 | s = self._find_grail_rc() |
| 232 | if not s: |
| 233 | return 0 |
| 234 | s.send(action) |
| 235 | s.close() |
| 236 | return 1 |
| 237 | |
| 238 | def open(self, url, new=0, autoraise=1): |
| 239 | if new: |
| 240 | self._remote("LOADNEW " + url) |
| 241 | else: |
| 242 | self._remote("LOAD " + url) |
| 243 | |
| 244 | def open_new(self, url): |
| 245 | self.open(url, 1) |
| 246 | |
| 247 | |
| 248 | class WindowsDefault: |
| 249 | def open(self, url, new=0, autoraise=1): |
| 250 | os.startfile(url) |
| 251 | |
| 252 | def open_new(self, url): |
| 253 | self.open(url) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 254 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 255 | # |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 256 | # Platform support for Unix |
| 257 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 258 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 259 | # This is the right test because all these Unix browsers require either |
| 260 | # a console terminal of an X display to run. Note that we cannot split |
| 261 | # the TERM and DISPLAY cases, because we might be running Python from inside |
| 262 | # an xterm. |
| 263 | if os.environ.get("TERM") or os.environ.get("DISPLAY"): |
Neal Norwitz | 8dd28eb | 2002-10-10 22:49:29 +0000 | [diff] [blame] | 264 | _tryorder = ["galeon", "mozilla", "netscape", "kfm", |
| 265 | "grail", "links", "lynx", "w3m",] |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 266 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 267 | # Easy cases first -- register console browsers if we have them. |
| 268 | if os.environ.get("TERM"): |
| 269 | # The Links browser <http://artax.karlin.mff.cuni.cz/~mikulas/links/> |
| 270 | if _iscommand("links"): |
Fred Drake | 925f144 | 2002-01-07 15:29:01 +0000 | [diff] [blame] | 271 | register("links", None, GenericBrowser("links '%s'")) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 272 | # The Lynx browser <http://lynx.browser.org/> |
| 273 | if _iscommand("lynx"): |
Fred Drake | 925f144 | 2002-01-07 15:29:01 +0000 | [diff] [blame] | 274 | register("lynx", None, GenericBrowser("lynx '%s'")) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 275 | # The w3m browser <http://ei5nazha.yz.yamagata-u.ac.jp/~aito/w3m/eng/> |
| 276 | if _iscommand("w3m"): |
Fred Drake | 925f144 | 2002-01-07 15:29:01 +0000 | [diff] [blame] | 277 | register("w3m", None, GenericBrowser("w3m '%s'")) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 278 | |
Ka-Ping Yee | 0a8c29b | 2001-03-02 02:01:40 +0000 | [diff] [blame] | 279 | # X browsers have more in the way of options |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 280 | if os.environ.get("DISPLAY"): |
| 281 | # First, the Netscape series |
Fred Drake | 925f144 | 2002-01-07 15:29:01 +0000 | [diff] [blame] | 282 | if _iscommand("mozilla"): |
| 283 | register("mozilla", None, Netscape("mozilla")) |
| 284 | if _iscommand("netscape"): |
| 285 | register("netscape", None, Netscape("netscape")) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 286 | |
| 287 | # Next, Mosaic -- old but still in use. |
| 288 | if _iscommand("mosaic"): |
Fred Drake | 925f144 | 2002-01-07 15:29:01 +0000 | [diff] [blame] | 289 | register("mosaic", None, GenericBrowser( |
| 290 | "mosaic '%s' >/dev/null &")) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 291 | |
Neal Norwitz | 8dd28eb | 2002-10-10 22:49:29 +0000 | [diff] [blame] | 292 | # Gnome's Galeon |
| 293 | if _iscommand("galeon"): |
| 294 | register("galeon", None, Galeon("galeon")) |
| 295 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 296 | # Konqueror/kfm, the KDE browser. |
Fred Drake | fc31f26 | 2001-03-26 15:06:15 +0000 | [diff] [blame] | 297 | if _iscommand("kfm") or _iscommand("konqueror"): |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 298 | register("kfm", Konqueror, Konqueror()) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 299 | |
| 300 | # Grail, the Python browser. |
| 301 | if _iscommand("grail"): |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 302 | register("grail", Grail, None) |
| 303 | |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 304 | |
| 305 | class InternetConfig: |
| 306 | def open(self, url, new=0, autoraise=1): |
| 307 | ic.launchurl(url) |
| 308 | |
| 309 | def open_new(self, url): |
| 310 | self.open(url) |
| 311 | |
| 312 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 313 | # |
| 314 | # Platform support for Windows |
| 315 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 316 | |
| 317 | if sys.platform[:3] == "win": |
Guido van Rossum | cb33165 | 2001-12-03 15:51:31 +0000 | [diff] [blame] | 318 | _tryorder = ["netscape", "windows-default"] |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 319 | register("windows-default", WindowsDefault) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 320 | |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 321 | # |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 322 | # Platform support for MacOS |
| 323 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 324 | |
| 325 | try: |
| 326 | import ic |
| 327 | except ImportError: |
| 328 | pass |
| 329 | else: |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 330 | # internet-config is the only supported controller on MacOS, |
| 331 | # so don't mess with the default! |
Guido van Rossum | cb33165 | 2001-12-03 15:51:31 +0000 | [diff] [blame] | 332 | _tryorder = ["internet-config"] |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 333 | register("internet-config", InternetConfig) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 334 | |
Martin v. Löwis | 3a89b2b | 2001-11-25 14:35:58 +0000 | [diff] [blame] | 335 | # |
| 336 | # Platform support for OS/2 |
| 337 | # |
| 338 | |
| 339 | if sys.platform[:3] == "os2" and _iscommand("netscape.exe"): |
Guido van Rossum | cb33165 | 2001-12-03 15:51:31 +0000 | [diff] [blame] | 340 | _tryorder = ["os2netscape"] |
Martin v. Löwis | 3a89b2b | 2001-11-25 14:35:58 +0000 | [diff] [blame] | 341 | register("os2netscape", None, |
| 342 | GenericBrowser("start netscape.exe %s")) |
| 343 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 344 | # OK, now that we know what the default preference orders for each |
| 345 | # platform are, allow user to override them with the BROWSER variable. |
| 346 | # |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 347 | if "BROWSER" in os.environ: |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 348 | # It's the user's responsibility to register handlers for any unknown |
| 349 | # browser referenced by this value, before calling open(). |
Guido van Rossum | 4b402f2 | 2001-12-04 17:43:22 +0000 | [diff] [blame] | 350 | _tryorder = os.environ["BROWSER"].split(os.pathsep) |
Skip Montanaro | cdab3bf | 2001-07-18 20:03:32 +0000 | [diff] [blame] | 351 | |
| 352 | for cmd in _tryorder: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 353 | if not cmd.lower() in _browsers: |
Skip Montanaro | cdab3bf | 2001-07-18 20:03:32 +0000 | [diff] [blame] | 354 | if _iscommand(cmd.lower()): |
Fred Drake | 925f144 | 2002-01-07 15:29:01 +0000 | [diff] [blame] | 355 | register(cmd.lower(), None, GenericBrowser( |
| 356 | "%s '%%s'" % cmd.lower())) |
Jack Jansen | ff0a7b8 | 2002-03-15 13:47:32 +0000 | [diff] [blame] | 357 | cmd = None # to make del work if _tryorder was empty |
Neal Norwitz | f963b45 | 2002-02-11 18:11:09 +0000 | [diff] [blame] | 358 | del cmd |
Skip Montanaro | cdab3bf | 2001-07-18 20:03:32 +0000 | [diff] [blame] | 359 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 360 | _tryorder = filter(lambda x: x.lower() in _browsers |
Skip Montanaro | cdab3bf | 2001-07-18 20:03:32 +0000 | [diff] [blame] | 361 | or x.find("%s") > -1, _tryorder) |
| 362 | # what to do if _tryorder is now empty? |