Kaydet (Commit) f4e5bd9d authored tarafından Fred Drake's avatar Fred Drake

_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.
üst bb0bae6d
...@@ -27,7 +27,10 @@ def get(using=None): ...@@ -27,7 +27,10 @@ def get(using=None):
return GenericBrowser(browser) return GenericBrowser(browser)
else: else:
# User gave us a browser name. # User gave us a browser name.
command = _browsers[browser.lower()] try:
command = _browsers[browser.lower()]
except KeyError:
command = _synthesize(browser)
if command[1] is None: if command[1] is None:
return command[0]() return command[0]()
else: else:
...@@ -42,6 +45,37 @@ def open(url, new=0, autoraise=1): ...@@ -42,6 +45,37 @@ def open(url, new=0, autoraise=1):
def open_new(url): # Marked deprecated. May be removed in 2.1. def open_new(url): # Marked deprecated. May be removed in 2.1.
get().open(url, 1) get().open(url, 1)
def _synthesize(browser):
"""Attempt to synthesize a controller base on existing controllers.
This is useful to create a controller when a user specifies a path to
an entry in the BROWSER environment variable -- we can copy a general
controller to operate using a specific installation of the desired
browser in this way.
If we can't create a controller in this way, or if there is no
executable for the requested browser, return [None, None].
"""
if not os.path.exists(browser):
return [None, None]
name = os.path.basename(browser)
try:
command = _browsers[name.lower()]
except KeyError:
return [None, None]
# now attempt to clone to fit the new name:
controller = command[1]
if controller and name.lower() == controller.basename:
import copy
controller = copy.copy(controller)
controller.name = browser
controller.basename = os.path.basename(browser)
register(browser, None, controller)
return [None, controller]
ret
# #
# Everything after this point initializes _browsers and _tryorder, # Everything after this point initializes _browsers and _tryorder,
# then disappears. Some class definitions and instances remain # then disappears. Some class definitions and instances remain
...@@ -74,10 +108,12 @@ if os.environ.get("TERM") or os.environ.get("DISPLAY"): ...@@ -74,10 +108,12 @@ if os.environ.get("TERM") or os.environ.get("DISPLAY"):
class GenericBrowser: class GenericBrowser:
def __init__(self, cmd): def __init__(self, cmd):
self.command = cmd self.name, self.args = cmd.split(None, 1)
self.basename = os.path.basename(self.name)
def open(self, url, new=0, autoraise=1): def open(self, url, new=0, autoraise=1):
os.system(self.command % url) command = "%s %s" % (self.name, self.args)
os.system(command % url)
def open_new(self, url): # Deprecated. May be removed in 2.1. def open_new(self, url): # Deprecated. May be removed in 2.1.
self.open(url) self.open(url)
...@@ -102,6 +138,7 @@ if os.environ.get("TERM") or os.environ.get("DISPLAY"): ...@@ -102,6 +138,7 @@ if os.environ.get("TERM") or os.environ.get("DISPLAY"):
"Launcher class for Netscape browsers." "Launcher class for Netscape browsers."
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
self.basename = os.path.basename(name)
def _remote(self, action, autoraise): def _remote(self, action, autoraise):
raise_opt = ("-noraise", "-raise")[autoraise] raise_opt = ("-noraise", "-raise")[autoraise]
...@@ -144,15 +181,21 @@ if os.environ.get("TERM") or os.environ.get("DISPLAY"): ...@@ -144,15 +181,21 @@ if os.environ.get("TERM") or os.environ.get("DISPLAY"):
for more information on the Konqueror remote-control interface. for more information on the Konqueror remote-control interface.
""" """
def __init__(self):
if _iscommand("konqueror"):
self.name = self.basename = "konqueror"
else:
self.name = self.basename = "kfm"
def _remote(self, action): def _remote(self, action):
cmd = "kfmclient %s >/dev/null 2>&1" % action cmd = "kfmclient %s >/dev/null 2>&1" % action
rc = os.system(cmd) rc = os.system(cmd)
if rc: if rc:
import time import time
if _iscommand("konqueror"): if self.basename == "konqueror":
os.system("konqueror --silent &") os.system(self.name + " --silent &")
else: else:
os.system("kfm -d &") os.system(self.name + " -d &")
time.sleep(PROCESS_CREATION_DELAY) time.sleep(PROCESS_CREATION_DELAY)
rc = os.system(cmd) rc = os.system(cmd)
return not rc return not rc
...@@ -165,7 +208,7 @@ if os.environ.get("TERM") or os.environ.get("DISPLAY"): ...@@ -165,7 +208,7 @@ if os.environ.get("TERM") or os.environ.get("DISPLAY"):
# Deprecated. May be removed in 2.1. # Deprecated. May be removed in 2.1.
open_new = open open_new = open
register("kfm", Konqueror, None) register("kfm", Konqueror, Konqueror())
# Grail, the Python browser. # Grail, the Python browser.
if _iscommand("grail"): if _iscommand("grail"):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment