paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@5 | 3 | """ |
paulb@5 | 4 | Simple desktop integration for Python. This module provides desktop environment |
paulb@5 | 5 | detection and resource opening support for a selection of common and |
paulb@5 | 6 | standardised desktop environments. |
paulb@5 | 7 | |
paulb@7 | 8 | Desktop Detection |
paulb@7 | 9 | ----------------- |
paulb@7 | 10 | |
paulb@5 | 11 | To detect a specific desktop environment, use the get_desktop function. |
paulb@7 | 12 | To detect whether the desktop environment is standardised (according to the |
paulb@5 | 13 | proposed DESKTOP_LAUNCH standard), use the is_standard function. |
paulb@5 | 14 | |
paulb@7 | 15 | Opening URLs |
paulb@7 | 16 | ------------ |
paulb@7 | 17 | |
paulb@7 | 18 | To open a URL in the current desktop environment, relying on the automatic |
paulb@7 | 19 | detection of that environment, use the desktop.open function as follows: |
paulb@7 | 20 | |
paulb@7 | 21 | desktop.open("http://www.python.org") |
paulb@7 | 22 | |
paulb@7 | 23 | To override the detected desktop, specify the desktop parameter to the open |
paulb@7 | 24 | function as follows: |
paulb@7 | 25 | |
paulb@7 | 26 | desktop.open("http://www.python.org", "KDE") # Insists on KDE |
paulb@7 | 27 | desktop.open("http://www.python.org", "GNOME") # Insists on GNOME |
paulb@7 | 28 | |
paulb@7 | 29 | Without overriding using the desktop parameter, the open function will attempt |
paulb@7 | 30 | to use the "standard" desktop opening mechanism which is controlled by the |
paulb@7 | 31 | DESKTOP_LAUNCH environment variable as described below. |
paulb@7 | 32 | |
paulb@7 | 33 | The DESKTOP_LAUNCH Environment Variable |
paulb@7 | 34 | --------------------------------------- |
paulb@7 | 35 | |
paulb@7 | 36 | The DESKTOP_LAUNCH environment variable must be shell-quoted where appropriate, |
paulb@7 | 37 | as shown in some of the following examples: |
paulb@7 | 38 | |
paulb@7 | 39 | DESKTOP_LAUNCH="kdialog --msgbox" Should present any opened URLs in |
paulb@7 | 40 | their entirety in a KDE message box. |
paulb@7 | 41 | (Command "kdialog" plus parameter.) |
paulb@7 | 42 | DESKTOP_LAUNCH="my\ opener" Should run the "my opener" program to |
paulb@7 | 43 | open URLs. |
paulb@7 | 44 | (Command "my opener", no parameters.) |
paulb@7 | 45 | DESKTOP_LAUNCH="my\ opener --url" Should run the "my opener" program to |
paulb@7 | 46 | open URLs. |
paulb@7 | 47 | (Command "my opener" plus parameter.) |
paulb@7 | 48 | |
paulb@5 | 49 | Details of the DESKTOP_LAUNCH environment variable convention can be found here: |
paulb@5 | 50 | http://lists.freedesktop.org/archives/xdg/2004-August/004489.html |
paulb@5 | 51 | """ |
paulb@1 | 52 | |
paulb@8 | 53 | __version__ = "0.1" |
paulb@8 | 54 | |
paulb@1 | 55 | import os |
paulb@4 | 56 | import sys |
paulb@1 | 57 | import subprocess |
paulb@6 | 58 | import commands |
paulb@1 | 59 | |
paulb@5 | 60 | def get_desktop(): |
paulb@5 | 61 | |
paulb@5 | 62 | """ |
paulb@5 | 63 | Detect the current desktop environment, returning the name of the |
paulb@5 | 64 | environment. If no environment could be detected, None is returned. |
paulb@5 | 65 | """ |
paulb@5 | 66 | |
paulb@5 | 67 | if os.environ.has_key("KDE_FULL_SESSION") or \ |
paulb@5 | 68 | os.environ.has_key("KDE_MULTIHEAD"): |
paulb@5 | 69 | return "KDE" |
paulb@5 | 70 | elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \ |
paulb@5 | 71 | os.environ.has_key("GNOME_KEYRING_SOCKET"): |
paulb@5 | 72 | return "GNOME" |
paulb@5 | 73 | elif sys.platform == "darwin": |
paulb@5 | 74 | return "Mac OS X" |
paulb@5 | 75 | elif hasattr(os, "startfile"): |
paulb@5 | 76 | return "Windows" |
paulb@5 | 77 | else: |
paulb@5 | 78 | return None |
paulb@5 | 79 | |
paulb@5 | 80 | def is_standard(): |
paulb@5 | 81 | |
paulb@5 | 82 | """ |
paulb@5 | 83 | Return whether the current desktop supports standardised application |
paulb@5 | 84 | launching. |
paulb@5 | 85 | """ |
paulb@5 | 86 | |
paulb@5 | 87 | return os.environ.has_key("DESKTOP_LAUNCH") |
paulb@5 | 88 | |
paulb@1 | 89 | def open(url, desktop=None): |
paulb@1 | 90 | |
paulb@1 | 91 | """ |
paulb@1 | 92 | Open the 'url' in the current desktop's preferred file browser. If the |
paulb@1 | 93 | optional 'desktop' parameter is specified then attempt to use that |
paulb@1 | 94 | particular desktop environment's mechanisms to open the 'url' instead of |
paulb@1 | 95 | guessing or detecting which environment is being used. |
paulb@1 | 96 | |
paulb@5 | 97 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X", |
paulb@5 | 98 | "Windows" where "standard" employs a DESKTOP_LAUNCH environment variable to |
paulb@6 | 99 | open the specified 'url'. DESKTOP_LAUNCH should be a command, possibly |
paulb@6 | 100 | followed by arguments, and must have any special characters shell-escaped. |
paulb@3 | 101 | |
paulb@3 | 102 | The process identifier of the "opener" (ie. viewer, editor, browser or |
paulb@3 | 103 | program) associated with the 'url' is returned by this function. If the |
paulb@3 | 104 | process identifier cannot be determined, None is returned. |
paulb@1 | 105 | """ |
paulb@1 | 106 | |
paulb@5 | 107 | # Attempt to detect a desktop environment. |
paulb@5 | 108 | |
paulb@5 | 109 | detected = get_desktop() |
paulb@5 | 110 | |
paulb@5 | 111 | # Start with desktops whose existence can be easily tested. |
paulb@4 | 112 | |
paulb@5 | 113 | if (desktop is None or desktop == "standard") and is_standard(): |
paulb@6 | 114 | arg = "".join([os.environ["DESKTOP_LAUNCH"], commands.mkarg(url)]) |
paulb@6 | 115 | return subprocess.Popen(arg, shell=1).pid |
paulb@4 | 116 | |
paulb@5 | 117 | elif (desktop is None or desktop == "Windows") and detected == "Windows": |
paulb@5 | 118 | # NOTE: This returns None in current implementations. |
paulb@5 | 119 | return os.startfile(url) |
paulb@1 | 120 | |
paulb@5 | 121 | # Test for desktops where the overriding is not verified. |
paulb@5 | 122 | |
paulb@5 | 123 | elif (desktop or detected) == "KDE": |
paulb@4 | 124 | cmd = ["kfmclient", "exec", url] |
paulb@1 | 125 | |
paulb@5 | 126 | elif (desktop or detected) == "GNOME": |
paulb@1 | 127 | cmd = ["gnome-open", url] |
paulb@1 | 128 | |
paulb@5 | 129 | elif (desktop or detected) == "Mac OS X": |
paulb@5 | 130 | cmd = ["open", url] |
paulb@4 | 131 | |
paulb@5 | 132 | # Finish with an error where no suitable desktop was identified. |
paulb@4 | 133 | |
paulb@1 | 134 | else: |
paulb@5 | 135 | raise OSError, "Desktop not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" |
paulb@1 | 136 | |
paulb@3 | 137 | return subprocess.Popen(cmd).pid |
paulb@1 | 138 | |
paulb@1 | 139 | # vim: tabstop=4 expandtab shiftwidth=4 |