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