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

Skip to content

Commit f4e5bd9

Browse files
committed
_synthesize(): Helper function: when the users passes a specific
value for the 'using' parameter of the get() function or the BROWSER environment variable, if the thing passed in is a path (as seems to be the case with KDE) instead of a short name, examine the available controllers to see if we can synthesize one based on a pre-registered controller that shares the same base name. get(): If the user specifies a browser we don't know about, use _synthesize() to attempt to create a usable controller. Some small adjustments were needed in some of the browser classes to support this.
1 parent bb0bae6 commit f4e5bd9

1 file changed

Lines changed: 50 additions & 7 deletions

File tree

Lib/webbrowser.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def get(using=None):
2727
return GenericBrowser(browser)
2828
else:
2929
# User gave us a browser name.
30-
command = _browsers[browser.lower()]
30+
try:
31+
command = _browsers[browser.lower()]
32+
except KeyError:
33+
command = _synthesize(browser)
3134
if command[1] is None:
3235
return command[0]()
3336
else:
@@ -42,6 +45,37 @@ def open(url, new=0, autoraise=1):
4245
def open_new(url): # Marked deprecated. May be removed in 2.1.
4346
get().open(url, 1)
4447

48+
49+
def _synthesize(browser):
50+
"""Attempt to synthesize a controller base on existing controllers.
51+
52+
This is useful to create a controller when a user specifies a path to
53+
an entry in the BROWSER environment variable -- we can copy a general
54+
controller to operate using a specific installation of the desired
55+
browser in this way.
56+
57+
If we can't create a controller in this way, or if there is no
58+
executable for the requested browser, return [None, None].
59+
60+
"""
61+
if not os.path.exists(browser):
62+
return [None, None]
63+
name = os.path.basename(browser)
64+
try:
65+
command = _browsers[name.lower()]
66+
except KeyError:
67+
return [None, None]
68+
# now attempt to clone to fit the new name:
69+
controller = command[1]
70+
if controller and name.lower() == controller.basename:
71+
import copy
72+
controller = copy.copy(controller)
73+
controller.name = browser
74+
controller.basename = os.path.basename(browser)
75+
register(browser, None, controller)
76+
return [None, controller]
77+
ret
78+
4579
#
4680
# Everything after this point initializes _browsers and _tryorder,
4781
# then disappears. Some class definitions and instances remain
@@ -74,10 +108,12 @@ def _iscommand(cmd):
74108

75109
class GenericBrowser:
76110
def __init__(self, cmd):
77-
self.command = cmd
111+
self.name, self.args = cmd.split(None, 1)
112+
self.basename = os.path.basename(self.name)
78113

79114
def open(self, url, new=0, autoraise=1):
80-
os.system(self.command % url)
115+
command = "%s %s" % (self.name, self.args)
116+
os.system(command % url)
81117

82118
def open_new(self, url): # Deprecated. May be removed in 2.1.
83119
self.open(url)
@@ -102,6 +138,7 @@ class Netscape:
102138
"Launcher class for Netscape browsers."
103139
def __init__(self, name):
104140
self.name = name
141+
self.basename = os.path.basename(name)
105142

106143
def _remote(self, action, autoraise):
107144
raise_opt = ("-noraise", "-raise")[autoraise]
@@ -144,15 +181,21 @@ class Konqueror:
144181
for more information on the Konqueror remote-control interface.
145182
146183
"""
184+
def __init__(self):
185+
if _iscommand("konqueror"):
186+
self.name = self.basename = "konqueror"
187+
else:
188+
self.name = self.basename = "kfm"
189+
147190
def _remote(self, action):
148191
cmd = "kfmclient %s >/dev/null 2>&1" % action
149192
rc = os.system(cmd)
150193
if rc:
151194
import time
152-
if _iscommand("konqueror"):
153-
os.system("konqueror --silent &")
195+
if self.basename == "konqueror":
196+
os.system(self.name + " --silent &")
154197
else:
155-
os.system("kfm -d &")
198+
os.system(self.name + " -d &")
156199
time.sleep(PROCESS_CREATION_DELAY)
157200
rc = os.system(cmd)
158201
return not rc
@@ -165,7 +208,7 @@ def open(self, url, new=1, autoraise=1):
165208
# Deprecated. May be removed in 2.1.
166209
open_new = open
167210

168-
register("kfm", Konqueror, None)
211+
register("kfm", Konqueror, Konqueror())
169212

170213
# Grail, the Python browser.
171214
if _iscommand("grail"):

0 commit comments

Comments
 (0)