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