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