[Python-Dev] urllib.browse() issues

Eric S. Raymond esr@thyrsus.com
Thu, 6 Jul 2000 12:21:22 -0400


Having finally wrestled CVS into at least temporary submission, 
I'm in the process of generating a patch to add a browser-launcher
function to the standard library.  There are a couple of issues
connected to this.

One: location.  I still think, after meditating on it, that urllib and
not os is the right place for this.  Yes, it launches a process -- but
that's implementation.  It's a way to fetch the contents of an URL.  In
the potential *user's* view of the Python library, it belongs with other
ways to fetch URLs.

Two: cross-platform capability is a Good Thing.  I rummaged around after a
half-forgotten bit of lore in my files and discovered an allegation
that under Windows, 

	os.system("start %s" % url)

is supposed to launch your default browser on the specified URL.  Now
here's what I'd like to write:

if os.environ.has_key("BROWSER"):
    _browsers = string.split(os.environ["BROWSER"], ":")
elif os.name == 'posix':
    _browsers = ["mozilla %s &",
                "netscape -remote 'openURL(%s)'",
                "netscape %s &",
                "lynx %s &",
                "w3m %s &"]
elif os.name == 'nt':
    _browsers = ["start %s"]

def urlbrowse(url):
    """Launch a browser, in background, pointed at the given URL.
    Accept either a string or a parsed URL tuple. 
    Interpret the BROWSER environment variable, if it exists,
    as a colon-separated list of browser commands to try.
    
    """
    from urlparse import urlunparse 
    if type(url) == ():
        url = urlunparse(url)
        for browser in _browsers:
            if iscommand(string.split(browser)[0]):
                if os.system((browser % url)) == 0:
                    return 1
    return 0

The missing piece of implementation is the function iscommand().  I know
how to write this under Unix:

def iscommand(cmd):
    return os.system('which 1>/dev/null 2>&1 ' + cmd) == 0

So my question is this: is there any analogous way to check for the existence
of a command under Windows?  If so...

Three: I'd like to add a cross-platform iscommand() function to os.
-- 
		<a href="http://www.tuxedo.org/~esr">Eric S. Raymond</a>

There's a truism that the road to Hell is often paved with good intentions.
The corollary is that evil is best known not by its motives but by its
*methods*.