1 """Remote-control interfaces to some browsers."""
7 PROCESS_CREATION_DELAY
= 4
10 class Error(Exception):
16 def register(name
, klass
, instance
=None):
17 """Register a browser connector and, optionally, connection."""
18 _browsers
[name
.lower()] = [klass
, instance
]
22 """Retrieve a connection to a browser by type name, or the default
24 name
= name
or DEFAULT_BROWSER
26 L
= _browsers
[name
.lower()]
28 raise ValueError, "unknown browser type: " + `name`
34 # Please note: the following definition hides a builtin function.
45 """Return true if cmd can be found on the executable search path."""
46 path
= os
.environ
.get("PATH")
49 for d
in path
.split(os
.pathsep
):
50 exe
= os
.path
.join(d
, cmd
)
51 if os
.path
.isfile(exe
):
56 class CommandLineBrowser
:
58 if os
.environ
.get("DISPLAY"):
60 ("netscape", "netscape %s >/dev/null &"),
61 ("mosaic", "mosaic %s >/dev/null &"),
68 def open(self
, url
, new
=0):
69 for exe
, cmd
in self
._browsers
:
73 raise Error("could not locate runnable browser")
75 def open_new(self
, url
):
78 register("command-line", CommandLineBrowser
)
84 def _remote(self
, action
):
85 raise_opt
= ("-noraise", "-raise")[self
.autoRaise
]
86 cmd
= "netscape %s -remote '%s' >/dev/null 2>&1" % (raise_opt
, action
)
90 os
.system("netscape -no-about-splash &")
91 time
.sleep(PROCESS_CREATION_DELAY
)
95 def open(self
, url
, new
=0):
99 self
._remote
("openURL(%s)" % url
)
101 def open_new(self
, url
):
102 self
._remote
("openURL(%s, new-window)" % url
)
104 register("netscape", Netscape
)
108 """Controller for the KDE File Manager (kfm, or Konquerer).
110 See http://developer.kde.org/documentation/other/kfmclient.html
111 for more information on the Konquerer remote-control interface.
114 def _remote(self
, action
):
115 cmd
= "kfmclient %s >/dev/null 2>&1" % action
119 os
.system("kfm -d &")
120 time
.sleep(PROCESS_CREATION_DELAY
)
124 def open(self
, url
, new
=1):
125 # XXX currently I know no way to prevent KFM from opening a new win.
128 def open_new(self
, url
):
129 self
._remote
("openURL %s" % url
)
131 register("kfm", Konquerer
)
135 # There should be a way to maintain a connection to Grail, but the
136 # Grail remote control protocol doesn't really allow that at this
137 # point. It probably never will!
139 def _find_grail_rc(self
):
144 tempdir
= os
.path
.join(tempfile
.gettempdir(), ".grail-unix")
145 user
= pwd
.getpwuid(_os
.getuid())[0]
146 filename
= os
.path
.join(tempdir
, user
+ "-*")
147 maybes
= glob
.glob(filename
)
150 s
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_STREAM
)
152 # need to PING each one until we find one that's live
156 # no good; attempt to clean it out, but don't fail:
164 def _remote(self
, action
):
165 s
= self
._find
_grail
_rc
()
172 def open(self
, url
, new
=0):
176 self
._remote
("LOAD " + url
)
178 def open_new(self
, url
):
179 self
._remote
("LOADNEW " + url
)
181 register("grail", Grail
)
184 class WindowsDefault
:
185 def open(self
, url
, new
=0):
186 import win32api
, win32con
187 win32api
.ShellExecute(0, "open", url
, None, ".",
188 win32con
.SW_SHOWNORMAL
)
190 def open_new(self
, url
):
194 DEFAULT_BROWSER
= "command-line"
196 if sys
.platform
[:3] == "win":
198 register("windows-default", WindowsDefault
)
199 DEFAULT_BROWSER
= "windows-default"
200 elif os
.environ
.get("DISPLAY"):
201 if os
.environ
.get("KDEDIR"):
202 DEFAULT_BROWSER
= "kfm"
203 elif _iscommand("netscape"):
204 DEFAULT_BROWSER
= "netscape"
206 # If the $BROWSER environment variable is set and true, let that be
207 # the name of the browser to use:
209 DEFAULT_BROWSER
= os
.environ
.get("BROWSER") or DEFAULT_BROWSER
212 # Now try to support the MacOS world. This is the only supported
213 # controller on that platform, so don't mess with the default!
220 class InternetConfig
:
221 def open(self
, url
, new
=0):
224 def open_new(self
, url
):
228 register("internet-config", InternetConfig
)
229 DEFAULT_BROWSER
= "internet-config"