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