Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 1 | """Remote-control interfaces to common browsers.""" |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 6 | class Error(Exception): |
| 7 | pass |
| 8 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 9 | _browsers = {} # Dictionary of available browser controllers |
| 10 | _tryorder = [] # Preference order of available browsers |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 11 | |
| 12 | def register(name, klass, instance=None): |
| 13 | """Register a browser connector and, optionally, connection.""" |
| 14 | _browsers[name.lower()] = [klass, instance] |
| 15 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 16 | def get(using=None): |
| 17 | """Return a browser launcher instance appropriate for the environment.""" |
| 18 | if using: |
| 19 | alternatives = [using] |
| 20 | else: |
| 21 | alternatives = _tryorder |
| 22 | for browser in alternatives: |
| 23 | if browser.find('%s') > -1: |
| 24 | # User gave us a command line, don't mess with it. |
| 25 | return browser |
| 26 | else: |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 27 | # User gave us a browser name. |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 28 | command = _browsers[browser.lower()] |
| 29 | if command[1] is None: |
| 30 | return command[0]() |
| 31 | else: |
| 32 | return command[1] |
| 33 | raise Error("could not locate runnable browser") |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 34 | |
| 35 | # Please note: the following definition hides a builtin function. |
| 36 | |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 37 | def open(url, new=0, autoraise=1): |
| 38 | get().open(url, new, autoraise) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 39 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 40 | def open_new(url): # Marked deprecated. May be removed in 2.1. |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 41 | get().open(url, 1) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 42 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 43 | # |
| 44 | # Everything after this point initializes _browsers and _tryorder, |
| 45 | # then disappears. Some class definitions and instances remain |
| 46 | # live through these globals, but only the minimum set needed to |
| 47 | # support the user's platform. |
| 48 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 49 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 50 | # |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 51 | # Platform support for Unix |
| 52 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 53 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 54 | # This is the right test because all these Unix browsers require either |
| 55 | # a console terminal of an X display to run. Note that we cannot split |
| 56 | # the TERM and DISPLAY cases, because we might be running Python from inside |
| 57 | # an xterm. |
| 58 | if os.environ.get("TERM") or os.environ.get("DISPLAY"): |
| 59 | PROCESS_CREATION_DELAY = 4 |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 60 | _tryorder = ("mozilla","netscape","kfm","grail","links","lynx","w3m") |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 61 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 62 | def _iscommand(cmd): |
| 63 | """Return true if cmd can be found on the executable search path.""" |
| 64 | path = os.environ.get("PATH") |
| 65 | if not path: |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 66 | return 0 |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 67 | for d in path.split(os.pathsep): |
| 68 | exe = os.path.join(d, cmd) |
| 69 | if os.path.isfile(exe): |
| 70 | return 1 |
| 71 | return 0 |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 72 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 73 | class GenericBrowser: |
| 74 | def __init__(self, cmd): |
| 75 | self.command = cmd |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 76 | |
Eric S. Raymond | cfa4096 | 2001-01-23 15:49:34 +0000 | [diff] [blame] | 77 | def open(self, url, new=0, autoraise=1): |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 78 | os.system(self.command % url) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 79 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 80 | def open_new(self, url): # Deprecated. May be removed in 2.1. |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 81 | self.open(url) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 82 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 83 | # Easy cases first -- register console browsers if we have them. |
| 84 | if os.environ.get("TERM"): |
| 85 | # The Links browser <http://artax.karlin.mff.cuni.cz/~mikulas/links/> |
| 86 | if _iscommand("links"): |
| 87 | register("links", None, GenericBrowser("links %s")) |
| 88 | # The Lynx browser <http://lynx.browser.org/> |
| 89 | if _iscommand("lynx"): |
| 90 | register("lynx", None, GenericBrowser("lynx %s")) |
| 91 | # The w3m browser <http://ei5nazha.yz.yamagata-u.ac.jp/~aito/w3m/eng/> |
| 92 | if _iscommand("w3m"): |
| 93 | register("w3m", None, GenericBrowser("w3m %s")) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 94 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 95 | # X browsers have mre in the way of options |
| 96 | if os.environ.get("DISPLAY"): |
| 97 | # First, the Netscape series |
| 98 | if _iscommand("netscape") or _iscommand("mozilla"): |
| 99 | class Netscape: |
| 100 | "Launcher class for Netscape browsers." |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 101 | def __init__(self, name): |
| 102 | self.name = name |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 103 | |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 104 | def _remote(self, action, autoraise): |
| 105 | raise_opt = ("-noraise", "-raise")[autoraise] |
Jeremy Hylton | 8016a4b | 2001-02-27 18:44:14 +0000 | [diff] [blame] | 106 | cmd = "%s %s -remote '%s' >/dev/null 2>&1" % (self.name, |
| 107 | raise_opt, |
| 108 | action) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 109 | rc = os.system(cmd) |
| 110 | if rc: |
| 111 | import time |
| 112 | os.system("%s -no-about-splash &" % self.name) |
| 113 | time.sleep(PROCESS_CREATION_DELAY) |
| 114 | rc = os.system(cmd) |
| 115 | return not rc |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 116 | |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 117 | def open(self, url, new=0, autoraise=1): |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 118 | if new: |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 119 | self._remote("openURL(%s, new-window)"%url, autoraise) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 120 | else: |
Eric S. Raymond | f79cb2d | 2001-01-23 13:49:44 +0000 | [diff] [blame] | 121 | self._remote("openURL(%s)" % url, autoraise) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 122 | |
| 123 | # Deprecated. May be removed in 2.1. |
| 124 | def open_new(self, url): |
| 125 | self.open(url, 1) |
| 126 | |
| 127 | if _iscommand("mozilla"): |
| 128 | register("mozilla", None, Netscape("mozilla")) |
| 129 | if _iscommand("netscape"): |
| 130 | register("netscape", None, Netscape("netscape")) |
| 131 | |
| 132 | # Next, Mosaic -- old but still in use. |
| 133 | if _iscommand("mosaic"): |
| 134 | register("mosaic", None, GenericBrowser("mosaic %s >/dev/null &")) |
| 135 | |
| 136 | # Konqueror/kfm, the KDE browser. |
| 137 | if _iscommand("kfm"): |
| 138 | class Konqueror: |
| 139 | """Controller for the KDE File Manager (kfm, or Konqueror). |
| 140 | |
| 141 | See http://developer.kde.org/documentation/other/kfmclient.html |
| 142 | for more information on the Konqueror remote-control interface. |
| 143 | |
| 144 | """ |
| 145 | def _remote(self, action): |
| 146 | cmd = "kfmclient %s >/dev/null 2>&1" % action |
| 147 | rc = os.system(cmd) |
| 148 | if rc: |
| 149 | import time |
| 150 | os.system("kfm -d &") |
| 151 | time.sleep(PROCESS_CREATION_DELAY) |
| 152 | rc = os.system(cmd) |
| 153 | return not rc |
| 154 | |
Eric S. Raymond | cfa4096 | 2001-01-23 15:49:34 +0000 | [diff] [blame] | 155 | def open(self, url, new=1, autoraise=1): |
Jeremy Hylton | 8016a4b | 2001-02-27 18:44:14 +0000 | [diff] [blame] | 156 | # XXX Currently I know no way to prevent KFM from |
| 157 | # opening a new win. |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 158 | self._remote("openURL %s" % url) |
| 159 | |
| 160 | # Deprecated. May be removed in 2.1. |
| 161 | open_new = open |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 162 | |
| 163 | register("kfm", Konqueror, None) |
| 164 | |
| 165 | # Grail, the Python browser. |
| 166 | if _iscommand("grail"): |
| 167 | class Grail: |
| 168 | # There should be a way to maintain a connection to |
| 169 | # Grail, but the Grail remote control protocol doesn't |
| 170 | # really allow that at this point. It probably neverwill! |
| 171 | def _find_grail_rc(self): |
| 172 | import glob |
| 173 | import pwd |
| 174 | import socket |
| 175 | import tempfile |
Jeremy Hylton | 8016a4b | 2001-02-27 18:44:14 +0000 | [diff] [blame] | 176 | tempdir = os.path.join(tempfile.gettempdir(), |
| 177 | ".grail-unix") |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 178 | user = pwd.getpwuid(_os.getuid())[0] |
| 179 | filename = os.path.join(tempdir, user + "-*") |
| 180 | maybes = glob.glob(filename) |
| 181 | if not maybes: |
| 182 | return None |
| 183 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 184 | for fn in maybes: |
| 185 | # need to PING each one until we find one that's live |
| 186 | try: |
| 187 | s.connect(fn) |
| 188 | except socket.error: |
| 189 | # no good; attempt to clean it out, but don't fail: |
| 190 | try: |
| 191 | os.unlink(fn) |
| 192 | except IOError: |
| 193 | pass |
| 194 | else: |
| 195 | return s |
| 196 | |
| 197 | def _remote(self, action): |
| 198 | s = self._find_grail_rc() |
| 199 | if not s: |
| 200 | return 0 |
| 201 | s.send(action) |
| 202 | s.close() |
| 203 | return 1 |
| 204 | |
Eric S. Raymond | cfa4096 | 2001-01-23 15:49:34 +0000 | [diff] [blame] | 205 | def open(self, url, new=0, autoraise=1): |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 206 | if new: |
| 207 | self._remote("LOADNEW " + url) |
| 208 | else: |
| 209 | self._remote("LOAD " + url) |
| 210 | |
| 211 | # Deprecated. May be removed in 2.1. |
| 212 | def open_new(self, url): |
| 213 | self.open(url, 1) |
| 214 | |
| 215 | register("grail", Grail, None) |
| 216 | |
| 217 | # |
| 218 | # Platform support for Windows |
| 219 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 220 | |
| 221 | if sys.platform[:3] == "win": |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 222 | _tryorder = ("netscape", "windows-default") |
| 223 | |
| 224 | class WindowsDefault: |
Eric S. Raymond | cfa4096 | 2001-01-23 15:49:34 +0000 | [diff] [blame] | 225 | def open(self, url, new=0, autoraise=1): |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 226 | os.startfile(url) |
| 227 | |
| 228 | def open_new(self, url): # Deprecated. May be removed in 2.1. |
| 229 | self.open(url) |
| 230 | |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 231 | register("windows-default", WindowsDefault) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 232 | |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 233 | # |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 234 | # Platform support for MacOS |
| 235 | # |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 236 | |
| 237 | try: |
| 238 | import ic |
| 239 | except ImportError: |
| 240 | pass |
| 241 | else: |
| 242 | class InternetConfig: |
Eric S. Raymond | cfa4096 | 2001-01-23 15:49:34 +0000 | [diff] [blame] | 243 | def open(self, url, new=0, autoraise=1): |
Guido van Rossum | 2595a83 | 2000-11-13 20:30:57 +0000 | [diff] [blame] | 244 | ic.launchurl(url) |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 245 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 246 | def open_new(self, url): # Deprecated. May be removed in 2.1. |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 247 | self.open(url) |
| 248 | |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 249 | # internet-config is the only supported controller on MacOS, |
| 250 | # so don't mess with the default! |
| 251 | _tryorder = ("internet-config") |
Fred Drake | c70b448 | 2000-07-09 16:45:56 +0000 | [diff] [blame] | 252 | register("internet-config", InternetConfig) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 253 | |
| 254 | # OK, now that we know what the default preference orders for each |
| 255 | # platform are, allow user to override them with the BROWSER variable. |
| 256 | # |
| 257 | if os.environ.has_key("BROWSER"): |
| 258 | # It's the user's responsibility to register handlers for any unknown |
| 259 | # browser referenced by this value, before calling open(). |
| 260 | _tryorder = os.environ["BROWSER"].split(":") |
| 261 | else: |
| 262 | # Optimization: filter out alternatives that aren't available, so we can |
| 263 | # avoid has_key() tests at runtime. (This may also allow some unused |
| 264 | # classes and class-instance storage to be garbage-collected.) |
Jeremy Hylton | 8016a4b | 2001-02-27 18:44:14 +0000 | [diff] [blame] | 265 | _tryorder = filter(lambda x: _browsers.has_key(x.lower()) |
| 266 | or x.find("%s") > -1, _tryorder) |
Eric S. Raymond | f7f1851 | 2001-01-23 13:16:32 +0000 | [diff] [blame] | 267 | |
| 268 | # end |