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