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@36 | 8 | Copyright (C) 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@12 | 9 | |
paulb@24 | 10 | This library is free software; you can redistribute it and/or |
paulb@24 | 11 | modify it under the terms of the GNU Lesser General Public |
paulb@24 | 12 | License as published by the Free Software Foundation; either |
paulb@24 | 13 | version 2.1 of the License, or (at your option) any later version. |
paulb@24 | 14 | |
paulb@24 | 15 | This library is distributed in the hope that it will be useful, |
paulb@24 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@24 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@24 | 18 | Lesser General Public License for more details. |
paulb@24 | 19 | |
paulb@24 | 20 | You should have received a copy of the GNU Lesser General Public |
paulb@24 | 21 | License along with this library; if not, write to the Free Software |
paulb@24 | 22 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@12 | 23 | |
paulb@12 | 24 | -------- |
paulb@12 | 25 | |
paulb@7 | 26 | Desktop Detection |
paulb@7 | 27 | ----------------- |
paulb@7 | 28 | |
paulb@5 | 29 | To detect a specific desktop environment, use the get_desktop function. |
paulb@7 | 30 | To detect whether the desktop environment is standardised (according to the |
paulb@5 | 31 | proposed DESKTOP_LAUNCH standard), use the is_standard function. |
paulb@5 | 32 | |
paulb@7 | 33 | Opening URLs |
paulb@7 | 34 | ------------ |
paulb@7 | 35 | |
paulb@7 | 36 | To open a URL in the current desktop environment, relying on the automatic |
paulb@7 | 37 | detection of that environment, use the desktop.open function as follows: |
paulb@7 | 38 | |
paulb@7 | 39 | desktop.open("http://www.python.org") |
paulb@7 | 40 | |
paulb@7 | 41 | To override the detected desktop, specify the desktop parameter to the open |
paulb@7 | 42 | function as follows: |
paulb@7 | 43 | |
paulb@7 | 44 | desktop.open("http://www.python.org", "KDE") # Insists on KDE |
paulb@7 | 45 | desktop.open("http://www.python.org", "GNOME") # Insists on GNOME |
paulb@7 | 46 | |
paulb@7 | 47 | Without overriding using the desktop parameter, the open function will attempt |
paulb@7 | 48 | to use the "standard" desktop opening mechanism which is controlled by the |
paulb@7 | 49 | DESKTOP_LAUNCH environment variable as described below. |
paulb@7 | 50 | |
paulb@7 | 51 | The DESKTOP_LAUNCH Environment Variable |
paulb@7 | 52 | --------------------------------------- |
paulb@7 | 53 | |
paulb@7 | 54 | The DESKTOP_LAUNCH environment variable must be shell-quoted where appropriate, |
paulb@7 | 55 | as shown in some of the following examples: |
paulb@7 | 56 | |
paulb@7 | 57 | DESKTOP_LAUNCH="kdialog --msgbox" Should present any opened URLs in |
paulb@7 | 58 | their entirety in a KDE message box. |
paulb@7 | 59 | (Command "kdialog" plus parameter.) |
paulb@7 | 60 | DESKTOP_LAUNCH="my\ opener" Should run the "my opener" program to |
paulb@7 | 61 | open URLs. |
paulb@7 | 62 | (Command "my opener", no parameters.) |
paulb@7 | 63 | DESKTOP_LAUNCH="my\ opener --url" Should run the "my opener" program to |
paulb@7 | 64 | open URLs. |
paulb@7 | 65 | (Command "my opener" plus parameter.) |
paulb@7 | 66 | |
paulb@5 | 67 | Details of the DESKTOP_LAUNCH environment variable convention can be found here: |
paulb@5 | 68 | http://lists.freedesktop.org/archives/xdg/2004-August/004489.html |
paulb@5 | 69 | """ |
paulb@1 | 70 | |
paulb@31 | 71 | __version__ = "0.2.4" |
paulb@8 | 72 | |
paulb@1 | 73 | import os |
paulb@4 | 74 | import sys |
paulb@27 | 75 | |
paulb@32 | 76 | # Provide suitable process creation functions. |
paulb@32 | 77 | |
paulb@27 | 78 | try: |
paulb@27 | 79 | import subprocess |
paulb@27 | 80 | def _run(cmd, shell, wait): |
paulb@27 | 81 | opener = subprocess.Popen(cmd, shell=shell) |
paulb@27 | 82 | if wait: opener.wait() |
paulb@27 | 83 | return opener.pid |
paulb@27 | 84 | |
paulb@31 | 85 | def _readfrom(cmd, shell): |
paulb@31 | 86 | opener = subprocess.Popen(cmd, shell=shell, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
paulb@31 | 87 | opener.stdin.close() |
paulb@31 | 88 | return opener.stdout.read() |
paulb@31 | 89 | |
paulb@31 | 90 | def _status(cmd, shell): |
paulb@31 | 91 | opener = subprocess.Popen(cmd, shell=shell) |
paulb@31 | 92 | opener.wait() |
paulb@31 | 93 | return opener.returncode == 0 |
paulb@31 | 94 | |
paulb@27 | 95 | except ImportError: |
paulb@27 | 96 | import popen2 |
paulb@27 | 97 | def _run(cmd, shell, wait): |
paulb@27 | 98 | opener = popen2.Popen3(cmd) |
paulb@27 | 99 | if wait: opener.wait() |
paulb@27 | 100 | return opener.pid |
paulb@27 | 101 | |
paulb@31 | 102 | def _readfrom(cmd, shell): |
paulb@31 | 103 | opener = popen2.Popen3(cmd) |
paulb@31 | 104 | opener.tochild.close() |
paulb@31 | 105 | opener.childerr.close() |
paulb@31 | 106 | return opener.fromchild.read() |
paulb@31 | 107 | |
paulb@31 | 108 | def _status(cmd, shell): |
paulb@31 | 109 | opener = popen2.Popen3(cmd) |
paulb@31 | 110 | opener.wait() |
paulb@31 | 111 | return opener.poll() == 0 |
paulb@31 | 112 | |
paulb@6 | 113 | import commands |
paulb@1 | 114 | |
paulb@40 | 115 | # Private functions. |
paulb@40 | 116 | |
paulb@40 | 117 | def _is_xfce(): |
paulb@40 | 118 | |
paulb@40 | 119 | "Return whether XFCE is in use." |
paulb@40 | 120 | |
paulb@40 | 121 | # XFCE detection involves testing the output of a program. |
paulb@40 | 122 | |
paulb@40 | 123 | try: |
paulb@40 | 124 | if not os.environ.get("DISPLAY", "").strip(): |
paulb@40 | 125 | vars = "DISPLAY=:0.0 " |
paulb@40 | 126 | else: |
paulb@40 | 127 | vars = "" |
paulb@42 | 128 | return _readfrom(vars + "xprop -root _DT_SAVE_MODE", shell=1).strip().endswith(' = "xfce4"') |
paulb@40 | 129 | |
paulb@40 | 130 | except OSError: |
paulb@40 | 131 | return 0 |
paulb@40 | 132 | |
paulb@31 | 133 | # Introspection functions. |
paulb@31 | 134 | |
paulb@5 | 135 | def get_desktop(): |
paulb@5 | 136 | |
paulb@5 | 137 | """ |
paulb@5 | 138 | Detect the current desktop environment, returning the name of the |
paulb@5 | 139 | environment. If no environment could be detected, None is returned. |
paulb@5 | 140 | """ |
paulb@5 | 141 | |
paulb@5 | 142 | if os.environ.has_key("KDE_FULL_SESSION") or \ |
paulb@5 | 143 | os.environ.has_key("KDE_MULTIHEAD"): |
paulb@5 | 144 | return "KDE" |
paulb@5 | 145 | elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \ |
paulb@5 | 146 | os.environ.has_key("GNOME_KEYRING_SOCKET"): |
paulb@5 | 147 | return "GNOME" |
paulb@5 | 148 | elif sys.platform == "darwin": |
paulb@5 | 149 | return "Mac OS X" |
paulb@5 | 150 | elif hasattr(os, "startfile"): |
paulb@5 | 151 | return "Windows" |
paulb@40 | 152 | elif _is_xfce(): |
paulb@40 | 153 | return "XFCE" |
paulb@33 | 154 | |
paulb@33 | 155 | # XFCE runs on X11, so we have to test for X11 last. |
paulb@33 | 156 | |
paulb@33 | 157 | if os.environ.has_key("DISPLAY"): |
paulb@31 | 158 | return "X11" |
paulb@31 | 159 | else: |
paulb@31 | 160 | return None |
paulb@31 | 161 | |
paulb@31 | 162 | def use_desktop(desktop): |
paulb@31 | 163 | |
paulb@31 | 164 | """ |
paulb@31 | 165 | Decide which desktop should be used, based on the detected desktop and a |
paulb@31 | 166 | supplied 'desktop' argument (which may be None). Return an identifier |
paulb@31 | 167 | indicating the desktop type as being either "standard" or one of the results |
paulb@31 | 168 | from the 'get_desktop' function. |
paulb@31 | 169 | """ |
paulb@31 | 170 | |
paulb@31 | 171 | # Attempt to detect a desktop environment. |
paulb@31 | 172 | |
paulb@31 | 173 | detected = get_desktop() |
paulb@31 | 174 | |
paulb@31 | 175 | # Start with desktops whose existence can be easily tested. |
paulb@31 | 176 | |
paulb@31 | 177 | if (desktop is None or desktop == "standard") and is_standard(): |
paulb@31 | 178 | return "standard" |
paulb@31 | 179 | elif (desktop is None or desktop == "Windows") and detected == "Windows": |
paulb@31 | 180 | return "Windows" |
paulb@31 | 181 | |
paulb@31 | 182 | # Test for desktops where the overriding is not verified. |
paulb@31 | 183 | |
paulb@31 | 184 | elif (desktop or detected) == "KDE": |
paulb@31 | 185 | return "KDE" |
paulb@31 | 186 | elif (desktop or detected) == "GNOME": |
paulb@31 | 187 | return "GNOME" |
paulb@33 | 188 | elif (desktop or detected) == "XFCE": |
paulb@33 | 189 | return "XFCE" |
paulb@31 | 190 | elif (desktop or detected) == "Mac OS X": |
paulb@31 | 191 | return "Mac OS X" |
paulb@31 | 192 | elif (desktop or detected) == "X11": |
paulb@31 | 193 | return "X11" |
paulb@5 | 194 | else: |
paulb@5 | 195 | return None |
paulb@5 | 196 | |
paulb@5 | 197 | def is_standard(): |
paulb@5 | 198 | |
paulb@5 | 199 | """ |
paulb@5 | 200 | Return whether the current desktop supports standardised application |
paulb@5 | 201 | launching. |
paulb@5 | 202 | """ |
paulb@5 | 203 | |
paulb@5 | 204 | return os.environ.has_key("DESKTOP_LAUNCH") |
paulb@5 | 205 | |
paulb@31 | 206 | # Activity functions. |
paulb@31 | 207 | |
paulb@18 | 208 | def open(url, desktop=None, wait=0): |
paulb@1 | 209 | |
paulb@1 | 210 | """ |
paulb@1 | 211 | Open the 'url' in the current desktop's preferred file browser. If the |
paulb@1 | 212 | optional 'desktop' parameter is specified then attempt to use that |
paulb@1 | 213 | particular desktop environment's mechanisms to open the 'url' instead of |
paulb@1 | 214 | guessing or detecting which environment is being used. |
paulb@1 | 215 | |
paulb@33 | 216 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", "XFCE", |
paulb@33 | 217 | "Mac OS X", "Windows" where "standard" employs a DESKTOP_LAUNCH environment |
paulb@33 | 218 | variable to open the specified 'url'. DESKTOP_LAUNCH should be a command, |
paulb@33 | 219 | possibly followed by arguments, and must have any special characters |
paulb@33 | 220 | shell-escaped. |
paulb@3 | 221 | |
paulb@3 | 222 | The process identifier of the "opener" (ie. viewer, editor, browser or |
paulb@3 | 223 | program) associated with the 'url' is returned by this function. If the |
paulb@3 | 224 | process identifier cannot be determined, None is returned. |
paulb@16 | 225 | |
paulb@16 | 226 | An optional 'wait' parameter is also available for advanced usage and, if |
paulb@18 | 227 | 'wait' is set to a true value, this function will wait for the launching |
paulb@18 | 228 | mechanism to complete before returning (as opposed to immediately returning |
paulb@18 | 229 | as is the default behaviour). |
paulb@1 | 230 | """ |
paulb@1 | 231 | |
paulb@31 | 232 | # Decide on the desktop environment in use. |
paulb@5 | 233 | |
paulb@31 | 234 | desktop_in_use = use_desktop(desktop) |
paulb@4 | 235 | |
paulb@31 | 236 | if desktop_in_use == "standard": |
paulb@6 | 237 | arg = "".join([os.environ["DESKTOP_LAUNCH"], commands.mkarg(url)]) |
paulb@27 | 238 | return _run(arg, 1, wait) |
paulb@4 | 239 | |
paulb@31 | 240 | elif desktop_in_use == "Windows": |
paulb@5 | 241 | # NOTE: This returns None in current implementations. |
paulb@5 | 242 | return os.startfile(url) |
paulb@1 | 243 | |
paulb@31 | 244 | elif desktop_in_use == "KDE": |
paulb@4 | 245 | cmd = ["kfmclient", "exec", url] |
paulb@1 | 246 | |
paulb@31 | 247 | elif desktop_in_use == "GNOME": |
paulb@1 | 248 | cmd = ["gnome-open", url] |
paulb@1 | 249 | |
paulb@33 | 250 | elif desktop_in_use == "XFCE": |
paulb@33 | 251 | cmd = ["exo-open", url] |
paulb@33 | 252 | |
paulb@31 | 253 | elif desktop_in_use == "Mac OS X": |
paulb@5 | 254 | cmd = ["open", url] |
paulb@4 | 255 | |
paulb@33 | 256 | elif desktop_in_use == "X11" and os.environ.has_key("BROWSER"): |
paulb@33 | 257 | cmd = [os.environ["BROWSER"], url] |
paulb@33 | 258 | |
paulb@5 | 259 | # Finish with an error where no suitable desktop was identified. |
paulb@4 | 260 | |
paulb@1 | 261 | else: |
paulb@32 | 262 | raise OSError, "Desktop '%s' not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" % desktop_in_use |
paulb@1 | 263 | |
paulb@27 | 264 | return _run(cmd, 0, wait) |
paulb@1 | 265 | |
paulb@1 | 266 | # vim: tabstop=4 expandtab shiftwidth=4 |