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