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