~ubuntu-branches/ubuntu/hardy/emesene/hardy-proposed

« back to all changes in this revision

Viewing changes to desktop.py

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2008-02-06 21:57:05 UTC
  • Revision ID: james.westby@ubuntu.com-20080206215705-d1abf07rdwcaju3p
Tags: upstream-1.0~r1013
ImportĀ upstreamĀ versionĀ 1.0~r1013

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
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.
 
5
 
 
6
Copyright (C) 2005, 2006 Paul Boddie <paul@boddie.org.uk>
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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
 
21
 
 
22
--------
 
23
 
 
24
Desktop Detection
 
25
-----------------
 
26
 
 
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.
 
30
 
 
31
Opening URLs
 
32
------------
 
33
 
 
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:
 
36
 
 
37
desktop.open("http://www.python.org")
 
38
 
 
39
To override the detected desktop, specify the desktop parameter to the open
 
40
function as follows:
 
41
 
 
42
desktop.open("http://www.python.org", "KDE") # Insists on KDE
 
43
desktop.open("http://www.python.org", "GNOME") # Insists on GNOME
 
44
 
 
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.
 
48
 
 
49
The DESKTOP_LAUNCH Environment Variable
 
50
---------------------------------------
 
51
 
 
52
The DESKTOP_LAUNCH environment variable must be shell-quoted where appropriate,
 
53
as shown in some of the following examples:
 
54
 
 
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
 
59
                                        open URLs.
 
60
                                        (Command "my opener", no parameters.)
 
61
DESKTOP_LAUNCH="my\ opener --url"       Should run the "my opener" program to
 
62
                                        open URLs.
 
63
                                        (Command "my opener" plus parameter.)
 
64
 
 
65
Details of the DESKTOP_LAUNCH environment variable convention can be found here:
 
66
http://lists.freedesktop.org/archives/xdg/2004-August/004489.html
 
67
"""
 
68
 
 
69
__version__ = "0.2.3"
 
70
 
 
71
import os
 
72
import sys
 
73
 
 
74
try:
 
75
    import subprocess
 
76
    def _run(cmd, shell, wait):
 
77
        opener = subprocess.Popen(cmd, shell=shell)
 
78
        if wait: opener.wait()
 
79
        return opener.pid
 
80
 
 
81
except ImportError:
 
82
    import popen2
 
83
    def _run(cmd, shell, wait):
 
84
        opener = popen2.Popen3(cmd)
 
85
        if wait: opener.wait()
 
86
        return opener.pid
 
87
 
 
88
import commands
 
89
 
 
90
# global
 
91
override = ''
 
92
 
 
93
def get_desktop(dontoverride=False):
 
94
 
 
95
    """
 
96
    Detect the current desktop environment, returning the name of the
 
97
    environment. If no environment could be detected, None is returned.
 
98
    """
 
99
 
 
100
    global override
 
101
    if override and not dontoverride:
 
102
        return 'override'
 
103
    elif os.environ.has_key("KDE_FULL_SESSION") or \
 
104
        os.environ.has_key("KDE_MULTIHEAD"):
 
105
        return "KDE"
 
106
    elif os.environ.has_key("DESKTOP_SESSION") and \
 
107
        os.environ['DESKTOP_SESSION'] == 'xfce4':
 
108
        return 'xfce4'
 
109
    elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \
 
110
        os.environ.has_key("GNOME_KEYRING_SOCKET"):
 
111
        return "GNOME"
 
112
    elif sys.platform == "darwin":
 
113
        return "Mac OS X"
 
114
    elif hasattr(os, "startfile"):
 
115
        return "Windows"
 
116
    else:
 
117
        return None
 
118
 
 
119
def is_standard():
 
120
 
 
121
    """
 
122
    Return whether the current desktop supports standardised application
 
123
    launching.
 
124
    """
 
125
 
 
126
    return os.environ.has_key("DESKTOP_LAUNCH")
 
127
 
 
128
def open(url, desktop=None, wait=0):
 
129
 
 
130
    """
 
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.
 
135
 
 
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. 
 
140
 
 
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.
 
144
 
 
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).
 
149
    """
 
150
 
 
151
    # Attempt to detect a desktop environment.
 
152
 
 
153
    detected = get_desktop()
 
154
    
 
155
    # Give higher priority to user preference
 
156
    
 
157
    if (desktop is None or desktop == "override") and detected == "override":
 
158
        global override
 
159
        arg = override.replace("%url%", commands.mkarg(url))
 
160
        return _run(arg, 1, wait)
 
161
 
 
162
    # Desktops whose existence can be easily tested.
 
163
 
 
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)
 
167
 
 
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:
 
172
        desktop = detected
 
173
            
 
174
    cmd = get_command(desktop, url)
 
175
    if cmd:
 
176
        return _run(cmd, 0, wait)
 
177
    else:
 
178
        # Finish with an error where no suitable desktop was identified.
 
179
        raise OSError, "Desktop not supported"
 
180
 
 
181
 
 
182
def get_command(desktop, url):
 
183
    '''Test for desktops where the overriding is not verified.'''
 
184
    if desktop == "KDE":
 
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":
 
191
        return ["open", url]
 
192
    elif desktop == "standard":
 
193
        return ['$DESKTOP_LAUNCH']
 
194
    elif desktop == "Windows":
 
195
        return ['os.startfile()']
 
196
    else:
 
197
        return None
 
198
 
 
199
# vim: tabstop=4 expandtab shiftwidth=4