[Patches] Giving Python launch capav\bility

Eric S. Raymond esr@snark.thyrsus.com
Sat, 10 Jun 2000 04:49:02 -0400


Patch description:

Add a small function to urllib that supports launching a browser on
a specified URL.  Doc patch included because it's short and 
explains the function in detail.

Disclaimer:

I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.

--- liburllib.tex	2000/06/10 08:26:00	1.1
+++ liburllib.tex	2000/06/10 08:36:57
@@ -91,6 +91,18 @@
 \function{urlretrieve()}.
 \end{funcdesc}
 
+\begin{funcdesc}{urlbrowse}{url}
+Launch a browser, in background, pointed at the given URL.  Accept
+either a string or a parsed URL tuple as returned by
+\code{urlparse.urparse()}.  Under Unix, interpret the BROWSER environment
+variable, if it exists, as a colon-separated list of browser commands
+to try; in each commasnd, the URL is substituted for \%s.  By default,
+the Unix version of this function tries to hand the URL to Mozilla,
+then to a Netscape running on the current display, then to a new
+instance of Netscape, then to lynx, then to w3m.  The function returns
+1 if the launch succeeds, 0 otherwise.
+\end{funcdesc}
+
 \begin{funcdesc}{quote}{string\optional{, safe}}
 Replace special characters in \var{string} using the \samp{\%xx} escape.
 Letters, digits, and the characters \character{_,.-} are never quoted.
--- urllib.py	2000/06/10 08:39:03	1.1
+++ urllib.py	2000/06/10 08:39:30
@@ -973,6 +973,32 @@
         return proxies
 
 
+# Support for launching a browser
+
+if os.environ.has_key("BROWSER"):
+    _browsers = string.split(os.environ["BROWSER"], ":")
+else:
+    _browsers = ["mozilla %s &",
+                "netscape -remote 'openURL(%s)'",
+                "netscape %s &",
+                "lynx %s &",
+                "w3m %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 not os.system('which 1>/dev/null 2>&1 '+string.split(browser)[0]):
+            if os.system((browser % url)) == 0:
+                return 1
+    return 0
+
 # Test and time quote() and unquote()
 def test1():
     import time

End of diffs.
-- 
		<a href="http://www.tuxedo.org/~esr">Eric S. Raymond</a>

Still, if you will not fight for the right when you can easily
win without bloodshed, if you will not fight when your victory
will be sure and not so costly, you may come to the moment when
you will have to fight with all the odds against you and only a
precarious chance for survival. There may be a worse case.  You
may have to fight when there is no chance of victory, because it
is better to perish than to live as slaves.
	--Winston Churchill