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