# HG changeset patch # User David Steele # Date 1437180834 14400 # Fri Jul 17 20:53:54 2015 -0400 # Node ID 041946b60bef09f73182c4d8fa2e3314bc84750b # Parent b2a1fe8f1b11d95b329d92f2011ac894b4098386 Issue 24241: Promote webbrowser preferred browser diff -r b2a1fe8f1b11 -r 041946b60bef Doc/library/webbrowser.rst --- a/Doc/library/webbrowser.rst Fri Jul 17 11:59:57 2015 -0700 +++ b/Doc/library/webbrowser.rst Fri Jul 17 20:53:54 2015 -0400 @@ -94,9 +94,9 @@ variable or call :func:`get` with a nonempty argument matching the name of a handler you declare. -A number of browser types are predefined. This table gives the type names that -may be passed to the :func:`get` function and the corresponding instantiations -for the controller classes, all defined in this module. +A number of browser types are predefined. This table gives some of the type +names that may be passed to the :func:`get` function and the corresponding +instantiations for the controller classes, all defined in this module. +------------------------+-----------------------------------------+-------+ | Type Name | Class Name | Notes | diff -r b2a1fe8f1b11 -r 041946b60bef Lib/webbrowser.py --- a/Lib/webbrowser.py Fri Jul 17 11:59:57 2015 -0700 +++ b/Lib/webbrowser.py Fri Jul 17 20:53:54 2015 -0400 @@ -470,6 +470,22 @@ if shutil.which("grail"): register("grail", Grail, None) +def reorder(blist): + """Move the preferred browser to the top of the list""" + + try: + # freedesktop environments + cmd = "xdg-mime query default text/html" + bpath = subprocess.check_output(cmd.split()).decode() + + keyfn = lambda x: (bpath.find(x[1])<0 , x) + pref_order = sorted(enumerate(blist), key=keyfn) + blist = [x[1] for x in pref_order] + except OSError: + pass + + return blist + # Prefer X browsers if present if os.environ.get("DISPLAY"): register_X_browsers() @@ -704,6 +720,10 @@ register("firefox", None, MacOSXOSAScript('firefox'), -1) register("MacOSX", None, MacOSXOSAScript('default'), -1) +# Attempt to identify the user's preferred broswer for this environment, +# and move it to the top of th list +_tryorder = reorder(_tryorder) + # OK, now that we know what the default preference orders for each # platform are, allow user to override them with the BROWSER variable.