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@12 | 8 | Copyright (C) 2005 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@24 | 71 | __version__ = "0.2.2" |
paulb@8 | 72 | |
paulb@1 | 73 | import os |
paulb@4 | 74 | import sys |
paulb@1 | 75 | import subprocess |
paulb@6 | 76 | import commands |
paulb@1 | 77 | |
paulb@5 | 78 | def get_desktop(): |
paulb@5 | 79 | |
paulb@5 | 80 | """ |
paulb@5 | 81 | Detect the current desktop environment, returning the name of the |
paulb@5 | 82 | environment. If no environment could be detected, None is returned. |
paulb@5 | 83 | """ |
paulb@5 | 84 | |
paulb@5 | 85 | if os.environ.has_key("KDE_FULL_SESSION") or \ |
paulb@5 | 86 | os.environ.has_key("KDE_MULTIHEAD"): |
paulb@5 | 87 | return "KDE" |
paulb@5 | 88 | elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \ |
paulb@5 | 89 | os.environ.has_key("GNOME_KEYRING_SOCKET"): |
paulb@5 | 90 | return "GNOME" |
paulb@5 | 91 | elif sys.platform == "darwin": |
paulb@5 | 92 | return "Mac OS X" |
paulb@5 | 93 | elif hasattr(os, "startfile"): |
paulb@5 | 94 | return "Windows" |
paulb@5 | 95 | else: |
paulb@5 | 96 | return None |
paulb@5 | 97 | |
paulb@5 | 98 | def is_standard(): |
paulb@5 | 99 | |
paulb@5 | 100 | """ |
paulb@5 | 101 | Return whether the current desktop supports standardised application |
paulb@5 | 102 | launching. |
paulb@5 | 103 | """ |
paulb@5 | 104 | |
paulb@5 | 105 | return os.environ.has_key("DESKTOP_LAUNCH") |
paulb@5 | 106 | |
paulb@16 | 107 | def _wait(pid, block): |
paulb@16 | 108 | |
paulb@16 | 109 | """ |
paulb@16 | 110 | Perform a blocking Wait for the given process identifier, 'pid', if the |
paulb@16 | 111 | 'block' flag is set to a true value. Return the process identifier. |
paulb@16 | 112 | """ |
paulb@16 | 113 | |
paulb@16 | 114 | if block: |
paulb@16 | 115 | os.waitpid(pid, os.P_WAIT) |
paulb@16 | 116 | return pid |
paulb@16 | 117 | |
paulb@18 | 118 | def open(url, desktop=None, wait=0): |
paulb@1 | 119 | |
paulb@1 | 120 | """ |
paulb@1 | 121 | Open the 'url' in the current desktop's preferred file browser. If the |
paulb@1 | 122 | optional 'desktop' parameter is specified then attempt to use that |
paulb@1 | 123 | particular desktop environment's mechanisms to open the 'url' instead of |
paulb@1 | 124 | guessing or detecting which environment is being used. |
paulb@1 | 125 | |
paulb@5 | 126 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X", |
paulb@5 | 127 | "Windows" where "standard" employs a DESKTOP_LAUNCH environment variable to |
paulb@6 | 128 | open the specified 'url'. DESKTOP_LAUNCH should be a command, possibly |
paulb@6 | 129 | followed by arguments, and must have any special characters shell-escaped. |
paulb@3 | 130 | |
paulb@3 | 131 | The process identifier of the "opener" (ie. viewer, editor, browser or |
paulb@3 | 132 | program) associated with the 'url' is returned by this function. If the |
paulb@3 | 133 | process identifier cannot be determined, None is returned. |
paulb@16 | 134 | |
paulb@16 | 135 | An optional 'wait' parameter is also available for advanced usage and, if |
paulb@18 | 136 | 'wait' is set to a true value, this function will wait for the launching |
paulb@18 | 137 | mechanism to complete before returning (as opposed to immediately returning |
paulb@18 | 138 | as is the default behaviour). |
paulb@1 | 139 | """ |
paulb@1 | 140 | |
paulb@5 | 141 | # Attempt to detect a desktop environment. |
paulb@5 | 142 | |
paulb@5 | 143 | detected = get_desktop() |
paulb@5 | 144 | |
paulb@5 | 145 | # Start with desktops whose existence can be easily tested. |
paulb@4 | 146 | |
paulb@5 | 147 | if (desktop is None or desktop == "standard") and is_standard(): |
paulb@6 | 148 | arg = "".join([os.environ["DESKTOP_LAUNCH"], commands.mkarg(url)]) |
paulb@16 | 149 | return _wait(subprocess.Popen(arg, shell=1).pid, wait) |
paulb@4 | 150 | |
paulb@5 | 151 | elif (desktop is None or desktop == "Windows") and detected == "Windows": |
paulb@5 | 152 | # NOTE: This returns None in current implementations. |
paulb@5 | 153 | return os.startfile(url) |
paulb@1 | 154 | |
paulb@5 | 155 | # Test for desktops where the overriding is not verified. |
paulb@5 | 156 | |
paulb@5 | 157 | elif (desktop or detected) == "KDE": |
paulb@4 | 158 | cmd = ["kfmclient", "exec", url] |
paulb@1 | 159 | |
paulb@5 | 160 | elif (desktop or detected) == "GNOME": |
paulb@1 | 161 | cmd = ["gnome-open", url] |
paulb@1 | 162 | |
paulb@5 | 163 | elif (desktop or detected) == "Mac OS X": |
paulb@5 | 164 | cmd = ["open", url] |
paulb@4 | 165 | |
paulb@5 | 166 | # Finish with an error where no suitable desktop was identified. |
paulb@4 | 167 | |
paulb@1 | 168 | else: |
paulb@5 | 169 | raise OSError, "Desktop not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" |
paulb@1 | 170 | |
paulb@16 | 171 | return _wait(subprocess.Popen(cmd).pid, wait) |
paulb@1 | 172 | |
paulb@1 | 173 | # vim: tabstop=4 expandtab shiftwidth=4 |