2
Simple desktop integration for Python. This module provides desktop environment
3
detection and resource opening support for a selection of common and
4
standardised desktop environments.
6
Copyright (C) 2005, 2006 Paul Boddie <paul@boddie.org.uk>
8
This library is free software; you can redistribute it and/or
9
modify it under the terms of the GNU Lesser General Public
10
License as published by the Free Software Foundation; either
11
version 2.1 of the License, or (at your option) any later version.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public
19
License along with this library; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27
To detect a specific desktop environment, use the get_desktop function.
28
To detect whether the desktop environment is standardised (according to the
29
proposed DESKTOP_LAUNCH standard), use the is_standard function.
34
To open a URL in the current desktop environment, relying on the automatic
35
detection of that environment, use the desktop.open function as follows:
37
desktop.open("http://www.python.org")
39
To override the detected desktop, specify the desktop parameter to the open
42
desktop.open("http://www.python.org", "KDE") # Insists on KDE
43
desktop.open("http://www.python.org", "GNOME") # Insists on GNOME
45
Without overriding using the desktop parameter, the open function will attempt
46
to use the "standard" desktop opening mechanism which is controlled by the
47
DESKTOP_LAUNCH environment variable as described below.
49
The DESKTOP_LAUNCH Environment Variable
50
---------------------------------------
52
The DESKTOP_LAUNCH environment variable must be shell-quoted where appropriate,
53
as shown in some of the following examples:
55
DESKTOP_LAUNCH="kdialog --msgbox" Should present any opened URLs in
56
their entirety in a KDE message box.
57
(Command "kdialog" plus parameter.)
58
DESKTOP_LAUNCH="my\ opener" Should run the "my opener" program to
60
(Command "my opener", no parameters.)
61
DESKTOP_LAUNCH="my\ opener --url" Should run the "my opener" program to
63
(Command "my opener" plus parameter.)
65
Details of the DESKTOP_LAUNCH environment variable convention can be found here:
66
http://lists.freedesktop.org/archives/xdg/2004-August/004489.html
76
def _run(cmd, shell, wait):
77
opener = subprocess.Popen(cmd, shell=shell)
78
if wait: opener.wait()
83
def _run(cmd, shell, wait):
84
opener = popen2.Popen3(cmd)
85
if wait: opener.wait()
93
def get_desktop(dontoverride=False):
96
Detect the current desktop environment, returning the name of the
97
environment. If no environment could be detected, None is returned.
101
if override and not dontoverride:
103
elif os.environ.has_key("KDE_FULL_SESSION") or \
104
os.environ.has_key("KDE_MULTIHEAD"):
106
elif os.environ.has_key("DESKTOP_SESSION") and \
107
os.environ['DESKTOP_SESSION'] == 'xfce4':
109
elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \
110
os.environ.has_key("GNOME_KEYRING_SOCKET"):
112
elif sys.platform == "darwin":
114
elif hasattr(os, "startfile"):
122
Return whether the current desktop supports standardised application
126
return os.environ.has_key("DESKTOP_LAUNCH")
128
def open(url, desktop=None, wait=0):
131
Open the 'url' in the current desktop's preferred file browser. If the
132
optional 'desktop' parameter is specified then attempt to use that
133
particular desktop environment's mechanisms to open the 'url' instead of
134
guessing or detecting which environment is being used.
136
Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X",
137
"Windows" where "standard" employs a DESKTOP_LAUNCH environment variable to
138
open the specified 'url'. DESKTOP_LAUNCH should be a command, possibly
139
followed by arguments, and must have any special characters shell-escaped.
141
The process identifier of the "opener" (ie. viewer, editor, browser or
142
program) associated with the 'url' is returned by this function. If the
143
process identifier cannot be determined, None is returned.
145
An optional 'wait' parameter is also available for advanced usage and, if
146
'wait' is set to a true value, this function will wait for the launching
147
mechanism to complete before returning (as opposed to immediately returning
148
as is the default behaviour).
151
# Attempt to detect a desktop environment.
153
detected = get_desktop()
155
# Give higher priority to user preference
157
if (desktop is None or desktop == "override") and detected == "override":
159
arg = override.replace("%url%", commands.mkarg(url))
160
return _run(arg, 1, wait)
162
# Desktops whose existence can be easily tested.
164
elif (desktop is None or desktop == "standard") and is_standard():
165
arg = "".join([os.environ["DESKTOP_LAUNCH"], commands.mkarg(url)])
166
return _run(arg, 1, wait)
168
elif (desktop is None or desktop == "Windows") and detected == "Windows":
169
# NOTE: This returns None in current implementations.
170
return os.startfile(url)
171
elif desktop is None:
174
cmd = get_command(desktop, url)
176
return _run(cmd, 0, wait)
178
# Finish with an error where no suitable desktop was identified.
179
raise OSError, "Desktop not supported"
182
def get_command(desktop, url):
183
'''Test for desktops where the overriding is not verified.'''
185
return ["kfmclient", "exec", url]
186
elif desktop == "GNOME":
187
return ["gnome-open", url]
188
elif desktop == 'xfce4':
189
return ["exo-open", url]
190
elif desktop == "Mac OS X":
192
elif desktop == "standard":
193
return ['$DESKTOP_LAUNCH']
194
elif desktop == "Windows":
195
return ['os.startfile()']
199
# vim: tabstop=4 expandtab shiftwidth=4