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