Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f8f164

Browse files
committed
Remove some false comments.
Reorganize so the initialization sequences does not bite us in the foot. (There is no good reason to discard classes that clients may want to subclass.)
1 parent f5fec3c commit 3f8f164

1 file changed

Lines changed: 156 additions & 159 deletions

File tree

Lib/webbrowser.py

Lines changed: 156 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get(using=None):
4242
def open(url, new=0, autoraise=1):
4343
get().open(url, new, autoraise)
4444

45-
def open_new(url): # Marked deprecated. May be removed in 2.1.
45+
def open_new(url):
4646
get().open(url, 1)
4747

4848

@@ -76,12 +76,152 @@ def _synthesize(browser):
7676
return [None, controller]
7777
ret
7878

79-
#
80-
# Everything after this point initializes _browsers and _tryorder,
81-
# then disappears. Some class definitions and instances remain
82-
# live through these globals, but only the minimum set needed to
83-
# support the user's platform.
84-
#
79+
80+
def _iscommand(cmd):
81+
"""Return true if cmd can be found on the executable search path."""
82+
path = os.environ.get("PATH")
83+
if not path:
84+
return 0
85+
for d in path.split(os.pathsep):
86+
exe = os.path.join(d, cmd)
87+
if os.path.isfile(exe):
88+
return 1
89+
return 0
90+
91+
92+
PROCESS_CREATION_DELAY = 4
93+
94+
95+
class GenericBrowser:
96+
def __init__(self, cmd):
97+
self.name, self.args = cmd.split(None, 1)
98+
self.basename = os.path.basename(self.name)
99+
100+
def open(self, url, new=0, autoraise=1):
101+
command = "%s %s" % (self.name, self.args)
102+
os.system(command % url)
103+
104+
def open_new(self, url):
105+
self.open(url)
106+
107+
108+
class Netscape:
109+
"Launcher class for Netscape browsers."
110+
def __init__(self, name):
111+
self.name = name
112+
self.basename = os.path.basename(name)
113+
114+
def _remote(self, action, autoraise):
115+
raise_opt = ("-noraise", "-raise")[autoraise]
116+
cmd = "%s %s -remote '%s' >/dev/null 2>&1" % (self.name,
117+
raise_opt,
118+
action)
119+
rc = os.system(cmd)
120+
if rc:
121+
import time
122+
os.system("%s &" % self.name)
123+
time.sleep(PROCESS_CREATION_DELAY)
124+
rc = os.system(cmd)
125+
return not rc
126+
127+
def open(self, url, new=0, autoraise=1):
128+
if new:
129+
self._remote("openURL(%s, new-window)"%url, autoraise)
130+
else:
131+
self._remote("openURL(%s)" % url, autoraise)
132+
133+
def open_new(self, url):
134+
self.open(url, 1)
135+
136+
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 __init__(self):
145+
if _iscommand("konqueror"):
146+
self.name = self.basename = "konqueror"
147+
else:
148+
self.name = self.basename = "kfm"
149+
150+
def _remote(self, action):
151+
cmd = "kfmclient %s >/dev/null 2>&1" % action
152+
rc = os.system(cmd)
153+
if rc:
154+
import time
155+
if self.basename == "konqueror":
156+
os.system(self.name + " --silent &")
157+
else:
158+
os.system(self.name + " -d &")
159+
time.sleep(PROCESS_CREATION_DELAY)
160+
rc = os.system(cmd)
161+
return not rc
162+
163+
def open(self, url, new=1, autoraise=1):
164+
# XXX Currently I know no way to prevent KFM from
165+
# opening a new win.
166+
self._remote("openURL %s" % url)
167+
168+
open_new = open
169+
170+
171+
class Grail:
172+
# There should be a way to maintain a connection to Grail, but the
173+
# Grail remote control protocol doesn't really allow that at this
174+
# point. It probably neverwill!
175+
def _find_grail_rc(self):
176+
import glob
177+
import pwd
178+
import socket
179+
import tempfile
180+
tempdir = os.path.join(tempfile.gettempdir(),
181+
".grail-unix")
182+
user = pwd.getpwuid(_os.getuid())[0]
183+
filename = os.path.join(tempdir, user + "-*")
184+
maybes = glob.glob(filename)
185+
if not maybes:
186+
return None
187+
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
188+
for fn in maybes:
189+
# need to PING each one until we find one that's live
190+
try:
191+
s.connect(fn)
192+
except socket.error:
193+
# no good; attempt to clean it out, but don't fail:
194+
try:
195+
os.unlink(fn)
196+
except IOError:
197+
pass
198+
else:
199+
return s
200+
201+
def _remote(self, action):
202+
s = self._find_grail_rc()
203+
if not s:
204+
return 0
205+
s.send(action)
206+
s.close()
207+
return 1
208+
209+
def open(self, url, new=0, autoraise=1):
210+
if new:
211+
self._remote("LOADNEW " + url)
212+
else:
213+
self._remote("LOAD " + url)
214+
215+
def open_new(self, url):
216+
self.open(url, 1)
217+
218+
219+
class WindowsDefault:
220+
def open(self, url, new=0, autoraise=1):
221+
os.startfile(url)
222+
223+
def open_new(self, url):
224+
self.open(url)
85225

86226
#
87227
# Platform support for Unix
@@ -92,32 +232,8 @@ def _synthesize(browser):
92232
# the TERM and DISPLAY cases, because we might be running Python from inside
93233
# an xterm.
94234
if os.environ.get("TERM") or os.environ.get("DISPLAY"):
95-
PROCESS_CREATION_DELAY = 4
96235
_tryorder = ("mozilla","netscape","kfm","grail","links","lynx","w3m")
97236

98-
def _iscommand(cmd):
99-
"""Return true if cmd can be found on the executable search path."""
100-
path = os.environ.get("PATH")
101-
if not path:
102-
return 0
103-
for d in path.split(os.pathsep):
104-
exe = os.path.join(d, cmd)
105-
if os.path.isfile(exe):
106-
return 1
107-
return 0
108-
109-
class GenericBrowser:
110-
def __init__(self, cmd):
111-
self.name, self.args = cmd.split(None, 1)
112-
self.basename = os.path.basename(self.name)
113-
114-
def open(self, url, new=0, autoraise=1):
115-
command = "%s %s" % (self.name, self.args)
116-
os.system(command % url)
117-
118-
def open_new(self, url): # Deprecated. May be removed in 2.1.
119-
self.open(url)
120-
121237
# Easy cases first -- register console browsers if we have them.
122238
if os.environ.get("TERM"):
123239
# The Links browser <http://artax.karlin.mff.cuni.cz/~mikulas/links/>
@@ -134,35 +250,6 @@ def open_new(self, url): # Deprecated. May be removed in 2.1.
134250
if os.environ.get("DISPLAY"):
135251
# First, the Netscape series
136252
if _iscommand("netscape") or _iscommand("mozilla"):
137-
class Netscape:
138-
"Launcher class for Netscape browsers."
139-
def __init__(self, name):
140-
self.name = name
141-
self.basename = os.path.basename(name)
142-
143-
def _remote(self, action, autoraise):
144-
raise_opt = ("-noraise", "-raise")[autoraise]
145-
cmd = "%s %s -remote '%s' >/dev/null 2>&1" % (self.name,
146-
raise_opt,
147-
action)
148-
rc = os.system(cmd)
149-
if rc:
150-
import time
151-
os.system("%s &" % self.name)
152-
time.sleep(PROCESS_CREATION_DELAY)
153-
rc = os.system(cmd)
154-
return not rc
155-
156-
def open(self, url, new=0, autoraise=1):
157-
if new:
158-
self._remote("openURL(%s, new-window)"%url, autoraise)
159-
else:
160-
self._remote("openURL(%s)" % url, autoraise)
161-
162-
# Deprecated. May be removed in 2.1.
163-
def open_new(self, url):
164-
self.open(url, 1)
165-
166253
if _iscommand("mozilla"):
167254
register("mozilla", None, Netscape("mozilla"))
168255
if _iscommand("netscape"):
@@ -174,108 +261,27 @@ def open_new(self, url):
174261

175262
# Konqueror/kfm, the KDE browser.
176263
if _iscommand("kfm") or _iscommand("konqueror"):
177-
class Konqueror:
178-
"""Controller for the KDE File Manager (kfm, or Konqueror).
179-
180-
See http://developer.kde.org/documentation/other/kfmclient.html
181-
for more information on the Konqueror remote-control interface.
182-
183-
"""
184-
def __init__(self):
185-
if _iscommand("konqueror"):
186-
self.name = self.basename = "konqueror"
187-
else:
188-
self.name = self.basename = "kfm"
189-
190-
def _remote(self, action):
191-
cmd = "kfmclient %s >/dev/null 2>&1" % action
192-
rc = os.system(cmd)
193-
if rc:
194-
import time
195-
if self.basename == "konqueror":
196-
os.system(self.name + " --silent &")
197-
else:
198-
os.system(self.name + " -d &")
199-
time.sleep(PROCESS_CREATION_DELAY)
200-
rc = os.system(cmd)
201-
return not rc
202-
203-
def open(self, url, new=1, autoraise=1):
204-
# XXX Currently I know no way to prevent KFM from
205-
# opening a new win.
206-
self._remote("openURL %s" % url)
207-
208-
# Deprecated. May be removed in 2.1.
209-
open_new = open
210-
211264
register("kfm", Konqueror, Konqueror())
212265

213266
# Grail, the Python browser.
214267
if _iscommand("grail"):
215-
class Grail:
216-
# There should be a way to maintain a connection to
217-
# Grail, but the Grail remote control protocol doesn't
218-
# really allow that at this point. It probably neverwill!
219-
def _find_grail_rc(self):
220-
import glob
221-
import pwd
222-
import socket
223-
import tempfile
224-
tempdir = os.path.join(tempfile.gettempdir(),
225-
".grail-unix")
226-
user = pwd.getpwuid(_os.getuid())[0]
227-
filename = os.path.join(tempdir, user + "-*")
228-
maybes = glob.glob(filename)
229-
if not maybes:
230-
return None
231-
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
232-
for fn in maybes:
233-
# need to PING each one until we find one that's live
234-
try:
235-
s.connect(fn)
236-
except socket.error:
237-
# no good; attempt to clean it out, but don't fail:
238-
try:
239-
os.unlink(fn)
240-
except IOError:
241-
pass
242-
else:
243-
return s
244-
245-
def _remote(self, action):
246-
s = self._find_grail_rc()
247-
if not s:
248-
return 0
249-
s.send(action)
250-
s.close()
251-
return 1
252-
253-
def open(self, url, new=0, autoraise=1):
254-
if new:
255-
self._remote("LOADNEW " + url)
256-
else:
257-
self._remote("LOAD " + url)
258-
259-
# Deprecated. May be removed in 2.1.
260-
def open_new(self, url):
261-
self.open(url, 1)
262-
263268
register("grail", Grail, None)
264269

270+
271+
class InternetConfig:
272+
def open(self, url, new=0, autoraise=1):
273+
ic.launchurl(url)
274+
275+
def open_new(self, url):
276+
self.open(url)
277+
278+
265279
#
266280
# Platform support for Windows
267281
#
268282

269283
if sys.platform[:3] == "win":
270284
_tryorder = ("netscape", "windows-default")
271-
272-
class WindowsDefault:
273-
def open(self, url, new=0, autoraise=1):
274-
os.startfile(url)
275-
276-
def open_new(self, url): # Deprecated. May be removed in 2.1.
277-
self.open(url)
278-
279285
register("windows-default", WindowsDefault)
280286

281287
#
@@ -287,13 +293,6 @@ def open_new(self, url): # Deprecated. May be removed in 2.1.
287293
except ImportError:
288294
pass
289295
else:
290-
class InternetConfig:
291-
def open(self, url, new=0, autoraise=1):
292-
ic.launchurl(url)
293-
294-
def open_new(self, url): # Deprecated. May be removed in 2.1.
295-
self.open(url)
296-
297296
# internet-config is the only supported controller on MacOS,
298297
# so don't mess with the default!
299298
_tryorder = ("internet-config")
@@ -315,5 +314,3 @@ def open_new(self, url): # Deprecated. May be removed in 2.1.
315314
_tryorder = filter(lambda x: _browsers.has_key(x.lower())
316315
or x.find("%s") > -1, _tryorder)
317316
# what to do if _tryorder is now empty?
318-
319-
# end

0 commit comments

Comments
 (0)