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