1 """Remote-control interfaces to some browsers."""
8 DEFAULT_CONFIG_FILE
= "~/.browser.ini"
9 PROCESS_CREATION_DELAY
= 4
11 DEFAULT_BROWSER
= "netscape"
18 name
= get_default_browser()
20 name
= string
.lower(name
)
27 def get_default_browser(file=None):
29 files
= [DEFAULT_CONFIG_FILE
]
31 files
= [file, DEFAULT_CONFIG_FILE
]
33 file = os
.path
.expandvars(os
.path
.expanduser(file))
34 if file and os
.path
.isfile(file):
36 cf
= ConfigParser
.ConfigParser()
39 return string
.lower(cf
.get("Browser", "name"))
40 except ConfigParser
.Error
:
42 return DEFAULT_BROWSER
45 _default_browser
= None
48 global _default_browser
49 if _default_browser
is None:
50 _default_browser
= get()
51 return _default_browser
55 _get_browser().open(url
, new
)
59 _get_browser().open_new(url
)
62 def register(name
, klass
):
63 _browsers
[string
.lower(name
)] = [klass
, None]
69 def _remote(self
, action
):
70 raise_opt
= ("-noraise", "-raise")[self
.autoRaise
]
71 cmd
= "netscape %s -remote '%s' >/dev/null 2>&1" % (raise_opt
, action
)
75 os
.system("netscape -no-about-splash &")
76 time
.sleep(PROCESS_CREATION_DELAY
)
80 def open(self
, url
, new
=0):
84 self
._remote
("openURL(%s)" % url
)
86 def open_new(self
, url
):
87 self
._remote
("openURL(%s, new-window)" % url
)
89 register("netscape", Netscape
)
93 # There should be a way to maintain a connection to Grail, but the
94 # Grail remote control protocol doesn't really allow that at this
95 # point. It probably never will!
97 def _find_grail_rc(self
):
102 tempdir
= os
.path
.join(tempfile
.gettempdir(), ".grail-unix")
103 user
= pwd
.getpwuid(_os
.getuid())[0]
104 filename
= os
.path
.join(tempdir
, user
+ "-*")
105 maybes
= glob
.glob(filename
)
108 s
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_STREAM
)
110 # need to PING each one until we find one that's live
114 # no good; attempt to clean it out, but don't fail:
122 def _remote(self
, action
):
123 s
= self
._find
_grail
_rc
()
130 def open(self
, url
, new
=0):
134 self
._remote
("LOAD " + url
)
136 def open_new(self
, url
):
137 self
._remote
("LOADNEW " + url
)
139 register("grail", Grail
)
142 class WindowsDefault
:
143 def open(self
, url
, new
=0):
144 import win32api
, win32con
146 win32api
.ShellExecute(0, "open", url
, None, ".",
147 win32con
.SW_SHOWNORMAL
)
149 traceback
.print_exc()
152 def open_new(self
, url
):
155 if sys
.platform
[:3] == "win":
156 register("windows-default", WindowsDefault
)
157 DEFAULT_BROWSER
= "windows-default"