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