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 import os 54 import sys 55 import subprocess 56 import commands 57 58 def get_desktop(): 59 60 """ 61 Detect the current desktop environment, returning the name of the 62 environment. If no environment could be detected, None is returned. 63 """ 64 65 if os.environ.has_key("KDE_FULL_SESSION") or \ 66 os.environ.has_key("KDE_MULTIHEAD"): 67 return "KDE" 68 elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \ 69 os.environ.has_key("GNOME_KEYRING_SOCKET"): 70 return "GNOME" 71 elif sys.platform == "darwin": 72 return "Mac OS X" 73 elif hasattr(os, "startfile"): 74 return "Windows" 75 else: 76 return None 77 78 def is_standard(): 79 80 """ 81 Return whether the current desktop supports standardised application 82 launching. 83 """ 84 85 return os.environ.has_key("DESKTOP_LAUNCH") 86 87 def open(url, desktop=None): 88 89 """ 90 Open the 'url' in the current desktop's preferred file browser. If the 91 optional 'desktop' parameter is specified then attempt to use that 92 particular desktop environment's mechanisms to open the 'url' instead of 93 guessing or detecting which environment is being used. 94 95 Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X", 96 "Windows" where "standard" employs a DESKTOP_LAUNCH environment variable to 97 open the specified 'url'. DESKTOP_LAUNCH should be a command, possibly 98 followed by arguments, and must have any special characters shell-escaped. 99 100 The process identifier of the "opener" (ie. viewer, editor, browser or 101 program) associated with the 'url' is returned by this function. If the 102 process identifier cannot be determined, None is returned. 103 """ 104 105 # Attempt to detect a desktop environment. 106 107 detected = get_desktop() 108 109 # Start with desktops whose existence can be easily tested. 110 111 if (desktop is None or desktop == "standard") and is_standard(): 112 arg = "".join([os.environ["DESKTOP_LAUNCH"], commands.mkarg(url)]) 113 return subprocess.Popen(arg, shell=1).pid 114 115 elif (desktop is None or desktop == "Windows") and detected == "Windows": 116 # NOTE: This returns None in current implementations. 117 return os.startfile(url) 118 119 # Test for desktops where the overriding is not verified. 120 121 elif (desktop or detected) == "KDE": 122 cmd = ["kfmclient", "exec", url] 123 124 elif (desktop or detected) == "GNOME": 125 cmd = ["gnome-open", url] 126 127 elif (desktop or detected) == "Mac OS X": 128 cmd = ["open", url] 129 130 # Finish with an error where no suitable desktop was identified. 131 132 else: 133 raise OSError, "Desktop not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" 134 135 return subprocess.Popen(cmd).pid 136 137 # vim: tabstop=4 expandtab shiftwidth=4