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