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