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@5 | 8 | To detect a specific desktop environment, use the get_desktop function. |
paulb@5 | 9 | To detect whether the desktop environment is standardised (according to a |
paulb@5 | 10 | proposed DESKTOP_LAUNCH standard), use the is_standard function. |
paulb@5 | 11 | |
paulb@5 | 12 | Details of the DESKTOP_LAUNCH environment variable convention can be found here: |
paulb@5 | 13 | http://lists.freedesktop.org/archives/xdg/2004-August/004489.html |
paulb@5 | 14 | """ |
paulb@1 | 15 | |
paulb@1 | 16 | import os |
paulb@4 | 17 | import sys |
paulb@1 | 18 | import subprocess |
paulb@1 | 19 | |
paulb@5 | 20 | def get_desktop(): |
paulb@5 | 21 | |
paulb@5 | 22 | """ |
paulb@5 | 23 | Detect the current desktop environment, returning the name of the |
paulb@5 | 24 | environment. If no environment could be detected, None is returned. |
paulb@5 | 25 | """ |
paulb@5 | 26 | |
paulb@5 | 27 | if os.environ.has_key("KDE_FULL_SESSION") or \ |
paulb@5 | 28 | os.environ.has_key("KDE_MULTIHEAD"): |
paulb@5 | 29 | return "KDE" |
paulb@5 | 30 | elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \ |
paulb@5 | 31 | os.environ.has_key("GNOME_KEYRING_SOCKET"): |
paulb@5 | 32 | return "GNOME" |
paulb@5 | 33 | elif sys.platform == "darwin": |
paulb@5 | 34 | return "Mac OS X" |
paulb@5 | 35 | elif hasattr(os, "startfile"): |
paulb@5 | 36 | return "Windows" |
paulb@5 | 37 | else: |
paulb@5 | 38 | return None |
paulb@5 | 39 | |
paulb@5 | 40 | def is_standard(): |
paulb@5 | 41 | |
paulb@5 | 42 | """ |
paulb@5 | 43 | Return whether the current desktop supports standardised application |
paulb@5 | 44 | launching. |
paulb@5 | 45 | """ |
paulb@5 | 46 | |
paulb@5 | 47 | return os.environ.has_key("DESKTOP_LAUNCH") |
paulb@5 | 48 | |
paulb@1 | 49 | def open(url, desktop=None): |
paulb@1 | 50 | |
paulb@1 | 51 | """ |
paulb@1 | 52 | Open the 'url' in the current desktop's preferred file browser. If the |
paulb@1 | 53 | optional 'desktop' parameter is specified then attempt to use that |
paulb@1 | 54 | particular desktop environment's mechanisms to open the 'url' instead of |
paulb@1 | 55 | guessing or detecting which environment is being used. |
paulb@1 | 56 | |
paulb@5 | 57 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X", |
paulb@5 | 58 | "Windows" where "standard" employs a DESKTOP_LAUNCH environment variable to |
paulb@5 | 59 | open the specified 'url'. |
paulb@3 | 60 | |
paulb@3 | 61 | The process identifier of the "opener" (ie. viewer, editor, browser or |
paulb@3 | 62 | program) associated with the 'url' is returned by this function. If the |
paulb@3 | 63 | process identifier cannot be determined, None is returned. |
paulb@1 | 64 | """ |
paulb@1 | 65 | |
paulb@5 | 66 | # Attempt to detect a desktop environment. |
paulb@5 | 67 | |
paulb@5 | 68 | detected = get_desktop() |
paulb@5 | 69 | |
paulb@5 | 70 | # Start with desktops whose existence can be easily tested. |
paulb@4 | 71 | |
paulb@5 | 72 | if (desktop is None or desktop == "standard") and is_standard(): |
paulb@5 | 73 | # NOTE: This may not handle sophisticated commands properly. |
paulb@5 | 74 | cmd = os.environ["DESKTOP_LAUNCH"].split() |
paulb@5 | 75 | cmd.append(url) |
paulb@4 | 76 | |
paulb@5 | 77 | elif (desktop is None or desktop == "Windows") and detected == "Windows": |
paulb@5 | 78 | # NOTE: This returns None in current implementations. |
paulb@5 | 79 | return os.startfile(url) |
paulb@1 | 80 | |
paulb@5 | 81 | # Test for desktops where the overriding is not verified. |
paulb@5 | 82 | |
paulb@5 | 83 | elif (desktop or detected) == "KDE": |
paulb@4 | 84 | cmd = ["kfmclient", "exec", url] |
paulb@1 | 85 | |
paulb@5 | 86 | elif (desktop or detected) == "GNOME": |
paulb@1 | 87 | cmd = ["gnome-open", url] |
paulb@1 | 88 | |
paulb@5 | 89 | elif (desktop or detected) == "Mac OS X": |
paulb@5 | 90 | cmd = ["open", url] |
paulb@4 | 91 | |
paulb@5 | 92 | # Finish with an error where no suitable desktop was identified. |
paulb@4 | 93 | |
paulb@1 | 94 | else: |
paulb@5 | 95 | raise OSError, "Desktop not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" |
paulb@1 | 96 | |
paulb@3 | 97 | return subprocess.Popen(cmd).pid |
paulb@1 | 98 | |
paulb@1 | 99 | # vim: tabstop=4 expandtab shiftwidth=4 |