Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Ka-Ping Yee | 0a8c29b | 2001-03-02 02:01:40 +0000 | [diff] [blame] | 2 | """Interfaces for launching and remotely controlling Web browsers.""" |
Georg Brandl | ee25030 | 2007-07-01 08:11:35 +0000 | [diff] [blame] | 3 | # Maintained by Georg Brandl. |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 4 | |
| 5 | import os |
Georg Brandl | 972ca50 | 2007-03-21 11:51:25 +0000 | [diff] [blame] | 6 | import shlex |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 7 | import sys |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 8 | import stat |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 9 | import subprocess |
| 10 | import time |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 11 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 12 | __all__ = ["Error", "open", "open_new", "open_new_tab", "get", "register"] |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 13 | |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 14 | class Error(Exception): |
| 15 | pass |
| 16 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 17 | _browsers = {} # Dictionary of available browser controllers |
| 18 | _tryorder = [] # Preference order of available browsers |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 19 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 20 | def register(name, klass, instance=None, update_tryorder=1): |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 21 | """Register a browser connector and, optionally, connection.""" |
| 22 | _browsers[name.lower()] = [klass, instance] |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 23 | if update_tryorder > 0: |
| 24 | _tryorder.append(name) |
| 25 | elif update_tryorder < 0: |
| 26 | _tryorder.insert(0, name) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 27 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 28 | def get(using=None): |
| 29 | """Return a browser launcher instance appropriate for the environment.""" |
Raymond Hettinger | 10ff706 | 2002-06-02 03:04:52 +0000 | [diff] [blame] | 30 | if using is not None: |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 31 | alternatives = [using] |
| 32 | else: |
| 33 | alternatives = _tryorder |
| 34 | for browser in alternatives: |
Raymond Hettinger | bac788a | 2004-05-04 09:21:43 +0000 | [diff] [blame] | 35 | if '%s' in browser: |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 36 | # User gave us a command line, split it into name and args |
Georg Brandl | 972ca50 | 2007-03-21 11:51:25 +0000 | [diff] [blame] | 37 | browser = shlex.split(browser) |
| 38 | if browser[-1] == '&': |
| 39 | return BackgroundBrowser(browser[:-1]) |
| 40 | else: |
| 41 | return GenericBrowser(browser) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 42 | else: |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 43 | # User gave us a browser name or path. |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 44 | try: |
| 45 | command = _browsers[browser.lower()] |
| 46 | except KeyError: |
| 47 | command = _synthesize(browser) |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 48 | if command[1] is not None: |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 49 | return command[1] |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 50 | elif command[0] is not None: |
| 51 | return command[0]() |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 52 | raise Error("could not locate runnable browser") |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 53 | |
| 54 | # Please note: the following definition hides a builtin function. |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 55 | # It is recommended one does "import webbrowser" and uses webbrowser.open(url) |
| 56 | # instead of "from webbrowser import *". |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 57 | |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 58 | def open(url, new=0, autoraise=1): |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 59 | for name in _tryorder: |
| 60 | browser = get(name) |
| 61 | if browser.open(url, new, autoraise): |
| 62 | return True |
| 63 | return False |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 64 | |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 65 | def open_new(url): |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 66 | return open(url, 1) |
| 67 | |
| 68 | def open_new_tab(url): |
| 69 | return open(url, 2) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 70 | |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 71 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 72 | def _synthesize(browser, update_tryorder=1): |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 73 | """Attempt to synthesize a controller base on existing controllers. |
| 74 | |
| 75 | This is useful to create a controller when a user specifies a path to |
| 76 | an entry in the BROWSER environment variable -- we can copy a general |
| 77 | controller to operate using a specific installation of the desired |
| 78 | browser in this way. |
| 79 | |
| 80 | If we can't create a controller in this way, or if there is no |
| 81 | executable for the requested browser, return [None, None]. |
| 82 | |
| 83 | """ |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 84 | cmd = browser.split()[0] |
| 85 | if not _iscommand(cmd): |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 86 | return [None, None] |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 87 | name = os.path.basename(cmd) |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 88 | try: |
| 89 | command = _browsers[name.lower()] |
| 90 | except KeyError: |
| 91 | return [None, None] |
| 92 | # now attempt to clone to fit the new name: |
| 93 | controller = command[1] |
| 94 | if controller and name.lower() == controller.basename: |
| 95 | import copy |
| 96 | controller = copy.copy(controller) |
| 97 | controller.name = browser |
| 98 | controller.basename = os.path.basename(browser) |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 99 | register(browser, None, controller, update_tryorder) |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 100 | return [None, controller] |
Andrew M. Kuchling | 118aa53 | 2001-08-13 14:37:23 +0000 | [diff] [blame] | 101 | return [None, None] |
Fred Drake | f4e5bd9 | 2001-04-12 22:07:27 +0000 | [diff] [blame] | 102 | |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 103 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 104 | if sys.platform[:3] == "win": |
| 105 | def _isexecutable(cmd): |
| 106 | cmd = cmd.lower() |
Georg Brandl | b2afe85 | 2006-06-09 20:43:48 +0000 | [diff] [blame] | 107 | if os.path.isfile(cmd) and cmd.endswith((".exe", ".bat")): |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 108 | return True |
| 109 | for ext in ".exe", ".bat": |
| 110 | if os.path.isfile(cmd + ext): |
| 111 | return True |
| 112 | return False |
| 113 | else: |
| 114 | def _isexecutable(cmd): |
| 115 | if os.path.isfile(cmd): |
| 116 | mode = os.stat(cmd)[stat.ST_MODE] |
| 117 | if mode & stat.S_IXUSR or mode & stat.S_IXGRP or mode & stat.S_IXOTH: |
| 118 | return True |
| 119 | return False |
| 120 | |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 121 | def _iscommand(cmd): |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 122 | """Return True if cmd is executable or can be found on the executable |
| 123 | search path.""" |
| 124 | if _isexecutable(cmd): |
| 125 | return True |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 126 | path = os.environ.get("PATH") |
| 127 | if not path: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 128 | return False |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 129 | for d in path.split(os.pathsep): |
| 130 | exe = os.path.join(d, cmd) |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 131 | if _isexecutable(exe): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 132 | return True |
| 133 | return False |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 134 | |
| 135 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 136 | # General parent classes |
| 137 | |
| 138 | class BaseBrowser(object): |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 139 | """Parent class for all browsers. Do not use directly.""" |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 140 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 141 | args = ['%s'] |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 142 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 143 | def __init__(self, name=""): |
| 144 | self.name = name |
Georg Brandl | b980113 | 2005-10-08 20:47:38 +0000 | [diff] [blame] | 145 | self.basename = name |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 146 | |
Neal Norwitz | 196f733 | 2005-10-04 03:17:49 +0000 | [diff] [blame] | 147 | def open(self, url, new=0, autoraise=1): |
| 148 | raise NotImplementedError |
| 149 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 150 | def open_new(self, url): |
| 151 | return self.open(url, 1) |
| 152 | |
| 153 | def open_new_tab(self, url): |
| 154 | return self.open(url, 2) |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 155 | |
| 156 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 157 | class GenericBrowser(BaseBrowser): |
| 158 | """Class for all browsers started with a command |
| 159 | and without remote functionality.""" |
| 160 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 161 | def __init__(self, name): |
| 162 | if isinstance(name, basestring): |
| 163 | self.name = name |
Georg Brandl | ee25030 | 2007-07-01 08:11:35 +0000 | [diff] [blame] | 164 | self.args = ["%s"] |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 165 | else: |
| 166 | # name should be a list with arguments |
| 167 | self.name = name[0] |
| 168 | self.args = name[1:] |
Georg Brandl | b980113 | 2005-10-08 20:47:38 +0000 | [diff] [blame] | 169 | self.basename = os.path.basename(self.name) |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 170 | |
| 171 | def open(self, url, new=0, autoraise=1): |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 172 | cmdline = [self.name] + [arg.replace("%s", url) |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 173 | for arg in self.args] |
| 174 | try: |
Georg Brandl | 2c94bf7 | 2006-09-24 10:36:01 +0000 | [diff] [blame] | 175 | if sys.platform[:3] == 'win': |
| 176 | p = subprocess.Popen(cmdline) |
| 177 | else: |
| 178 | p = subprocess.Popen(cmdline, close_fds=True) |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 179 | return not p.wait() |
| 180 | except OSError: |
| 181 | return False |
| 182 | |
| 183 | |
| 184 | class BackgroundBrowser(GenericBrowser): |
| 185 | """Class for all browsers which are to be started in the |
| 186 | background.""" |
| 187 | |
| 188 | def open(self, url, new=0, autoraise=1): |
| 189 | cmdline = [self.name] + [arg.replace("%s", url) |
| 190 | for arg in self.args] |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 191 | try: |
Georg Brandl | 2c94bf7 | 2006-09-24 10:36:01 +0000 | [diff] [blame] | 192 | if sys.platform[:3] == 'win': |
| 193 | p = subprocess.Popen(cmdline) |
| 194 | else: |
| 195 | setsid = getattr(os, 'setsid', None) |
| 196 | if not setsid: |
| 197 | setsid = getattr(os, 'setpgrp', None) |
| 198 | p = subprocess.Popen(cmdline, close_fds=True, preexec_fn=setsid) |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 199 | return (p.poll() is None) |
| 200 | except OSError: |
| 201 | return False |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 202 | |
| 203 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 204 | class UnixBrowser(BaseBrowser): |
| 205 | """Parent class for all Unix browsers with remote functionality.""" |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 206 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 207 | raise_opts = None |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 208 | remote_args = ['%action', '%s'] |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 209 | remote_action = None |
| 210 | remote_action_newwin = None |
| 211 | remote_action_newtab = None |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 212 | background = False |
| 213 | redirect_stdout = True |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 214 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 215 | def _invoke(self, args, remote, autoraise): |
| 216 | raise_opt = [] |
| 217 | if remote and self.raise_opts: |
| 218 | # use autoraise argument only for remote invocation |
| 219 | autoraise = int(bool(autoraise)) |
| 220 | opt = self.raise_opts[autoraise] |
| 221 | if opt: raise_opt = [opt] |
| 222 | |
| 223 | cmdline = [self.name] + raise_opt + args |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 224 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 225 | if remote or self.background: |
| 226 | inout = file(os.devnull, "r+") |
| 227 | else: |
| 228 | # for TTY browsers, we need stdin/out |
| 229 | inout = None |
| 230 | # if possible, put browser in separate process group, so |
| 231 | # keyboard interrupts don't affect browser as well as Python |
| 232 | setsid = getattr(os, 'setsid', None) |
| 233 | if not setsid: |
| 234 | setsid = getattr(os, 'setpgrp', None) |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 235 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 236 | p = subprocess.Popen(cmdline, close_fds=True, stdin=inout, |
| 237 | stdout=(self.redirect_stdout and inout or None), |
| 238 | stderr=inout, preexec_fn=setsid) |
| 239 | if remote: |
| 240 | # wait five secons. If the subprocess is not finished, the |
| 241 | # remote invocation has (hopefully) started a new instance. |
| 242 | time.sleep(1) |
| 243 | rc = p.poll() |
| 244 | if rc is None: |
| 245 | time.sleep(4) |
| 246 | rc = p.poll() |
| 247 | if rc is None: |
| 248 | return True |
| 249 | # if remote call failed, open() will try direct invocation |
| 250 | return not rc |
| 251 | elif self.background: |
| 252 | if p.poll() is None: |
| 253 | return True |
| 254 | else: |
| 255 | return False |
| 256 | else: |
| 257 | return not p.wait() |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 258 | |
| 259 | def open(self, url, new=0, autoraise=1): |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 260 | if new == 0: |
| 261 | action = self.remote_action |
| 262 | elif new == 1: |
| 263 | action = self.remote_action_newwin |
| 264 | elif new == 2: |
| 265 | if self.remote_action_newtab is None: |
| 266 | action = self.remote_action_newwin |
| 267 | else: |
| 268 | action = self.remote_action_newtab |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 269 | else: |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 270 | raise Error("Bad 'new' parameter to open(); " + |
| 271 | "expected 0, 1, or 2, got %s" % new) |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 272 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 273 | args = [arg.replace("%s", url).replace("%action", action) |
| 274 | for arg in self.remote_args] |
| 275 | success = self._invoke(args, True, autoraise) |
| 276 | if not success: |
| 277 | # remote invocation failed, try straight way |
| 278 | args = [arg.replace("%s", url) for arg in self.args] |
| 279 | return self._invoke(args, False, False) |
| 280 | else: |
| 281 | return True |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 282 | |
| 283 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 284 | class Mozilla(UnixBrowser): |
| 285 | """Launcher class for Mozilla/Netscape browsers.""" |
Neal Norwitz | 8dd28eb | 2002-10-10 22:49:29 +0000 | [diff] [blame] | 286 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 287 | raise_opts = ["-noraise", "-raise"] |
Neal Norwitz | 8dd28eb | 2002-10-10 22:49:29 +0000 | [diff] [blame] | 288 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 289 | remote_args = ['-remote', 'openURL(%s%action)'] |
| 290 | remote_action = "" |
| 291 | remote_action_newwin = ",new-window" |
| 292 | remote_action_newtab = ",new-tab" |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 293 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 294 | background = True |
Neal Norwitz | 8dd28eb | 2002-10-10 22:49:29 +0000 | [diff] [blame] | 295 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 296 | Netscape = Mozilla |
Neal Norwitz | 8dd28eb | 2002-10-10 22:49:29 +0000 | [diff] [blame] | 297 | |
| 298 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 299 | class Galeon(UnixBrowser): |
| 300 | """Launcher class for Galeon/Epiphany browsers.""" |
| 301 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 302 | raise_opts = ["-noraise", ""] |
| 303 | remote_args = ['%action', '%s'] |
| 304 | remote_action = "-n" |
| 305 | remote_action_newwin = "-w" |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 306 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 307 | background = True |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 308 | |
| 309 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 310 | class Opera(UnixBrowser): |
| 311 | "Launcher class for Opera browser." |
| 312 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 313 | raise_opts = ["", "-raise"] |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 314 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 315 | remote_args = ['-remote', 'openURL(%s%action)'] |
| 316 | remote_action = "" |
| 317 | remote_action_newwin = ",new-window" |
| 318 | remote_action_newtab = ",new-page" |
| 319 | background = True |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 320 | |
| 321 | |
| 322 | class Elinks(UnixBrowser): |
| 323 | "Launcher class for Elinks browsers." |
| 324 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 325 | remote_args = ['-remote', 'openURL(%s%action)'] |
| 326 | remote_action = "" |
| 327 | remote_action_newwin = ",new-window" |
| 328 | remote_action_newtab = ",new-tab" |
| 329 | background = False |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 330 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 331 | # elinks doesn't like its stdout to be redirected - |
| 332 | # it uses redirected stdout as a signal to do -dump |
| 333 | redirect_stdout = False |
| 334 | |
| 335 | |
| 336 | class Konqueror(BaseBrowser): |
| 337 | """Controller for the KDE File Manager (kfm, or Konqueror). |
| 338 | |
| 339 | See the output of ``kfmclient --commands`` |
| 340 | for more information on the Konqueror remote-control interface. |
| 341 | """ |
| 342 | |
| 343 | def open(self, url, new=0, autoraise=1): |
| 344 | # XXX Currently I know no way to prevent KFM from opening a new win. |
| 345 | if new == 2: |
| 346 | action = "newTab" |
| 347 | else: |
| 348 | action = "openURL" |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 349 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 350 | devnull = file(os.devnull, "r+") |
| 351 | # if possible, put browser in separate process group, so |
| 352 | # keyboard interrupts don't affect browser as well as Python |
| 353 | setsid = getattr(os, 'setsid', None) |
| 354 | if not setsid: |
| 355 | setsid = getattr(os, 'setpgrp', None) |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 356 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 357 | try: |
| 358 | p = subprocess.Popen(["kfmclient", action, url], |
| 359 | close_fds=True, stdin=devnull, |
| 360 | stdout=devnull, stderr=devnull) |
| 361 | except OSError: |
| 362 | # fall through to next variant |
| 363 | pass |
| 364 | else: |
| 365 | p.wait() |
| 366 | # kfmclient's return code unfortunately has no meaning as it seems |
| 367 | return True |
| 368 | |
| 369 | try: |
| 370 | p = subprocess.Popen(["konqueror", "--silent", url], |
| 371 | close_fds=True, stdin=devnull, |
| 372 | stdout=devnull, stderr=devnull, |
| 373 | preexec_fn=setsid) |
| 374 | except OSError: |
| 375 | # fall through to next variant |
| 376 | pass |
| 377 | else: |
| 378 | if p.poll() is None: |
| 379 | # Should be running now. |
| 380 | return True |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 381 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 382 | try: |
| 383 | p = subprocess.Popen(["kfm", "-d", url], |
| 384 | close_fds=True, stdin=devnull, |
| 385 | stdout=devnull, stderr=devnull, |
| 386 | preexec_fn=setsid) |
| 387 | except OSError: |
| 388 | return False |
| 389 | else: |
| 390 | return (p.poll() is None) |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 391 | |
| 392 | |
| 393 | class Grail(BaseBrowser): |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 394 | # There should be a way to maintain a connection to Grail, but the |
| 395 | # Grail remote control protocol doesn't really allow that at this |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 396 | # point. It probably never will! |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 397 | def _find_grail_rc(self): |
| 398 | import glob |
| 399 | import pwd |
| 400 | import socket |
| 401 | import tempfile |
| 402 | tempdir = os.path.join(tempfile.gettempdir(), |
| 403 | ".grail-unix") |
Fred Drake | 16623fe | 2001-10-13 16:00:52 +0000 | [diff] [blame] | 404 | user = pwd.getpwuid(os.getuid())[0] |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 405 | filename = os.path.join(tempdir, user + "-*") |
| 406 | maybes = glob.glob(filename) |
| 407 | if not maybes: |
| 408 | return None |
| 409 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 410 | for fn in maybes: |
| 411 | # need to PING each one until we find one that's live |
| 412 | try: |
| 413 | s.connect(fn) |
| 414 | except socket.error: |
| 415 | # no good; attempt to clean it out, but don't fail: |
| 416 | try: |
| 417 | os.unlink(fn) |
| 418 | except IOError: |
| 419 | pass |
| 420 | else: |
| 421 | return s |
| 422 | |
| 423 | def _remote(self, action): |
| 424 | s = self._find_grail_rc() |
| 425 | if not s: |
| 426 | return 0 |
| 427 | s.send(action) |
| 428 | s.close() |
| 429 | return 1 |
| 430 | |
| 431 | def open(self, url, new=0, autoraise=1): |
| 432 | if new: |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 433 | ok = self._remote("LOADNEW " + url) |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 434 | else: |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 435 | ok = self._remote("LOAD " + url) |
| 436 | return ok |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 437 | |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 438 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 439 | # |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 440 | # Platform support for Unix |
| 441 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 442 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 443 | # These are the right tests because all these Unix browsers require either |
| 444 | # a console terminal or an X display to run. |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 445 | |
Neal Norwitz | 196f733 | 2005-10-04 03:17:49 +0000 | [diff] [blame] | 446 | def register_X_browsers(): |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 447 | |
Georg Brandl | 8f06d02 | 2007-03-16 07:55:09 +0000 | [diff] [blame] | 448 | # The default GNOME browser |
| 449 | if "GNOME_DESKTOP_SESSION_ID" in os.environ and _iscommand("gnome-open"): |
| 450 | register("gnome-open", None, BackgroundBrowser("gnome-open")) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 451 | |
Georg Brandl | 8f06d02 | 2007-03-16 07:55:09 +0000 | [diff] [blame] | 452 | # The default KDE browser |
| 453 | if "KDE_FULL_SESSION" in os.environ and _iscommand("kfmclient"): |
| 454 | register("kfmclient", Konqueror, Konqueror("kfmclient")) |
| 455 | |
| 456 | # The Mozilla/Netscape browsers |
Georg Brandl | 4a5a918 | 2005-11-22 19:18:01 +0000 | [diff] [blame] | 457 | for browser in ("mozilla-firefox", "firefox", |
| 458 | "mozilla-firebird", "firebird", |
Georg Brandl | 314acac | 2006-04-28 16:31:17 +0000 | [diff] [blame] | 459 | "seamonkey", "mozilla", "netscape"): |
Georg Brandl | 4a5a918 | 2005-11-22 19:18:01 +0000 | [diff] [blame] | 460 | if _iscommand(browser): |
| 461 | register(browser, None, Mozilla(browser)) |
| 462 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 463 | # Konqueror/kfm, the KDE browser. |
Georg Brandl | b980113 | 2005-10-08 20:47:38 +0000 | [diff] [blame] | 464 | if _iscommand("kfm"): |
| 465 | register("kfm", Konqueror, Konqueror("kfm")) |
| 466 | elif _iscommand("konqueror"): |
| 467 | register("konqueror", Konqueror, Konqueror("konqueror")) |
Neal Norwitz | 8dd28eb | 2002-10-10 22:49:29 +0000 | [diff] [blame] | 468 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 469 | # Gnome's Galeon and Epiphany |
| 470 | for browser in ("galeon", "epiphany"): |
| 471 | if _iscommand(browser): |
| 472 | register(browser, None, Galeon(browser)) |
Gustavo Niemeyer | 1456fde | 2002-11-25 17:25:04 +0000 | [diff] [blame] | 473 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 474 | # Skipstone, another Gtk/Mozilla based browser |
| 475 | if _iscommand("skipstone"): |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 476 | register("skipstone", None, BackgroundBrowser("skipstone")) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 477 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 478 | # Opera, quite popular |
| 479 | if _iscommand("opera"): |
| 480 | register("opera", None, Opera("opera")) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 481 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 482 | # Next, Mosaic -- old but still in use. |
| 483 | if _iscommand("mosaic"): |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 484 | register("mosaic", None, BackgroundBrowser("mosaic")) |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 485 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 486 | # Grail, the Python browser. Does anybody still use it? |
| 487 | if _iscommand("grail"): |
| 488 | register("grail", Grail, None) |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 489 | |
Neal Norwitz | 196f733 | 2005-10-04 03:17:49 +0000 | [diff] [blame] | 490 | # Prefer X browsers if present |
| 491 | if os.environ.get("DISPLAY"): |
| 492 | register_X_browsers() |
| 493 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 494 | # Also try console browsers |
| 495 | if os.environ.get("TERM"): |
| 496 | # The Links/elinks browsers <http://artax.karlin.mff.cuni.cz/~mikulas/links/> |
| 497 | if _iscommand("links"): |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 498 | register("links", None, GenericBrowser("links")) |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 499 | if _iscommand("elinks"): |
| 500 | register("elinks", None, Elinks("elinks")) |
| 501 | # The Lynx browser <http://lynx.isc.org/>, <http://lynx.browser.org/> |
| 502 | if _iscommand("lynx"): |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 503 | register("lynx", None, GenericBrowser("lynx")) |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 504 | # The w3m browser <http://w3m.sourceforge.net/> |
| 505 | if _iscommand("w3m"): |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 506 | register("w3m", None, GenericBrowser("w3m")) |
Fred Drake | 3f8f164 | 2001-07-19 03:46:26 +0000 | [diff] [blame] | 507 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 508 | # |
| 509 | # Platform support for Windows |
| 510 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 511 | |
| 512 | if sys.platform[:3] == "win": |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 513 | class WindowsDefault(BaseBrowser): |
| 514 | def open(self, url, new=0, autoraise=1): |
Georg Brandl | 8f06d02 | 2007-03-16 07:55:09 +0000 | [diff] [blame] | 515 | try: |
| 516 | os.startfile(url) |
| 517 | except WindowsError: |
| 518 | # [Error 22] No application is associated with the specified |
| 519 | # file for this operation: '<URL>' |
| 520 | return False |
| 521 | else: |
| 522 | return True |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 523 | |
| 524 | _tryorder = [] |
| 525 | _browsers = {} |
Georg Brandl | 8f06d02 | 2007-03-16 07:55:09 +0000 | [diff] [blame] | 526 | |
| 527 | # First try to use the default Windows browser |
| 528 | register("windows-default", WindowsDefault) |
| 529 | |
| 530 | # Detect some common Windows browsers, fallback to IE |
| 531 | iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"), |
| 532 | "Internet Explorer\\IEXPLORE.EXE") |
Georg Brandl | 7377ad2 | 2006-05-03 17:46:13 +0000 | [diff] [blame] | 533 | for browser in ("firefox", "firebird", "seamonkey", "mozilla", |
Georg Brandl | 8f06d02 | 2007-03-16 07:55:09 +0000 | [diff] [blame] | 534 | "netscape", "opera", iexplore): |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 535 | if _iscommand(browser): |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 536 | register(browser, None, BackgroundBrowser(browser)) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 537 | |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 538 | # |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 539 | # Platform support for MacOS |
| 540 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 541 | |
| 542 | try: |
| 543 | import ic |
| 544 | except ImportError: |
| 545 | pass |
| 546 | else: |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 547 | class InternetConfig(BaseBrowser): |
| 548 | def open(self, url, new=0, autoraise=1): |
| 549 | ic.launchurl(url) |
| 550 | return True # Any way to get status? |
| 551 | |
| 552 | register("internet-config", InternetConfig, update_tryorder=-1) |
| 553 | |
| 554 | if sys.platform == 'darwin': |
| 555 | # Adapted from patch submitted to SourceForge by Steven J. Burr |
| 556 | class MacOSX(BaseBrowser): |
| 557 | """Launcher class for Aqua browsers on Mac OS X |
| 558 | |
| 559 | Optionally specify a browser name on instantiation. Note that this |
| 560 | will not work for Aqua browsers if the user has moved the application |
| 561 | package after installation. |
| 562 | |
| 563 | If no browser is specified, the default browser, as specified in the |
| 564 | Internet System Preferences panel, will be used. |
| 565 | """ |
| 566 | def __init__(self, name): |
| 567 | self.name = name |
| 568 | |
| 569 | def open(self, url, new=0, autoraise=1): |
| 570 | assert "'" not in url |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 571 | # hack for local urls |
| 572 | if not ':' in url: |
| 573 | url = 'file:'+url |
Tim Peters | 887c080 | 2006-01-20 23:40:56 +0000 | [diff] [blame] | 574 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 575 | # new must be 0 or 1 |
| 576 | new = int(bool(new)) |
| 577 | if self.name == "default": |
| 578 | # User called open, open_new or get without a browser parameter |
Georg Brandl | 1cb179e | 2005-11-09 21:42:48 +0000 | [diff] [blame] | 579 | script = 'open location "%s"' % url.replace('"', '%22') # opens in default browser |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 580 | else: |
| 581 | # User called get and chose a browser |
| 582 | if self.name == "OmniWeb": |
| 583 | toWindow = "" |
| 584 | else: |
| 585 | # Include toWindow parameter of OpenURL command for browsers |
| 586 | # that support it. 0 == new window; -1 == existing |
| 587 | toWindow = "toWindow %d" % (new - 1) |
Georg Brandl | 1cb179e | 2005-11-09 21:42:48 +0000 | [diff] [blame] | 588 | cmd = 'OpenURL "%s"' % url.replace('"', '%22') |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 589 | script = '''tell application "%s" |
| 590 | activate |
| 591 | %s %s |
| 592 | end tell''' % (self.name, cmd, toWindow) |
| 593 | # Open pipe to AppleScript through osascript command |
| 594 | osapipe = os.popen("osascript", "w") |
| 595 | if osapipe is None: |
| 596 | return False |
| 597 | # Write script to osascript's stdin |
| 598 | osapipe.write(script) |
| 599 | rc = osapipe.close() |
| 600 | return not rc |
| 601 | |
| 602 | # Don't clear _tryorder or _browsers since OS X can use above Unix support |
| 603 | # (but we prefer using the OS X specific stuff) |
| 604 | register("MacOSX", None, MacOSX('default'), -1) |
| 605 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 606 | |
Martin v. Löwis | 3a89b2b | 2001-11-25 14:35:58 +0000 | [diff] [blame] | 607 | # |
| 608 | # Platform support for OS/2 |
| 609 | # |
| 610 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 611 | if sys.platform[:3] == "os2" and _iscommand("netscape"): |
| 612 | _tryorder = [] |
| 613 | _browsers = {} |
Martin v. Löwis | 3a89b2b | 2001-11-25 14:35:58 +0000 | [diff] [blame] | 614 | register("os2netscape", None, |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 615 | GenericBrowser(["start", "netscape", "%s"]), -1) |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 616 | |
Martin v. Löwis | 3a89b2b | 2001-11-25 14:35:58 +0000 | [diff] [blame] | 617 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 618 | # OK, now that we know what the default preference orders for each |
| 619 | # platform are, allow user to override them with the BROWSER variable. |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 620 | if "BROWSER" in os.environ: |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 621 | _userchoices = os.environ["BROWSER"].split(os.pathsep) |
| 622 | _userchoices.reverse() |
Skip Montanaro | cdab3bf | 2001-07-18 20:03:32 +0000 | [diff] [blame] | 623 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 624 | # Treat choices in same way as if passed into get() but do register |
| 625 | # and prepend to _tryorder |
| 626 | for cmdline in _userchoices: |
| 627 | if cmdline != '': |
| 628 | _synthesize(cmdline, -1) |
| 629 | cmdline = None # to make del work if _userchoices was empty |
| 630 | del cmdline |
| 631 | del _userchoices |
Skip Montanaro | cdab3bf | 2001-07-18 20:03:32 +0000 | [diff] [blame] | 632 | |
Skip Montanaro | cdab3bf | 2001-07-18 20:03:32 +0000 | [diff] [blame] | 633 | # what to do if _tryorder is now empty? |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 634 | |
| 635 | |
| 636 | def main(): |
| 637 | import getopt |
| 638 | usage = """Usage: %s [-n | -t] url |
| 639 | -n: open new window |
| 640 | -t: open new tab""" % sys.argv[0] |
| 641 | try: |
| 642 | opts, args = getopt.getopt(sys.argv[1:], 'ntd') |
| 643 | except getopt.error, msg: |
| 644 | print >>sys.stderr, msg |
| 645 | print >>sys.stderr, usage |
| 646 | sys.exit(1) |
| 647 | new_win = 0 |
| 648 | for o, a in opts: |
| 649 | if o == '-n': new_win = 1 |
| 650 | elif o == '-t': new_win = 2 |
| 651 | if len(args) <> 1: |
| 652 | print >>sys.stderr, usage |
| 653 | sys.exit(1) |
| 654 | |
| 655 | url = args[0] |
| 656 | open(url, new_win) |
| 657 | |
Georg Brandl | 23929f2 | 2006-01-20 21:03:35 +0000 | [diff] [blame] | 658 | print "\a" |
| 659 | |
Georg Brandl | e8f2443 | 2005-10-03 14:16:44 +0000 | [diff] [blame] | 660 | if __name__ == "__main__": |
| 661 | main() |