1.1 --- a/desktop.py Sun Sep 25 14:20:18 2005 +0000
1.2 +++ b/desktop.py Mon Sep 26 12:09:52 2005 +0000
1.3 @@ -1,11 +1,51 @@
1.4 #!/usr/bin/env python
1.5
1.6 -"Simple desktop integration for Python."
1.7 +"""
1.8 +Simple desktop integration for Python. This module provides desktop environment
1.9 +detection and resource opening support for a selection of common and
1.10 +standardised desktop environments.
1.11 +
1.12 +To detect a specific desktop environment, use the get_desktop function.
1.13 +To detect whether the desktop environment is standardised (according to a
1.14 +proposed DESKTOP_LAUNCH standard), use the is_standard function.
1.15 +
1.16 +Details of the DESKTOP_LAUNCH environment variable convention can be found here:
1.17 +http://lists.freedesktop.org/archives/xdg/2004-August/004489.html
1.18 +"""
1.19
1.20 import os
1.21 import sys
1.22 import subprocess
1.23
1.24 +def get_desktop():
1.25 +
1.26 + """
1.27 + Detect the current desktop environment, returning the name of the
1.28 + environment. If no environment could be detected, None is returned.
1.29 + """
1.30 +
1.31 + if os.environ.has_key("KDE_FULL_SESSION") or \
1.32 + os.environ.has_key("KDE_MULTIHEAD"):
1.33 + return "KDE"
1.34 + elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \
1.35 + os.environ.has_key("GNOME_KEYRING_SOCKET"):
1.36 + return "GNOME"
1.37 + elif sys.platform == "darwin":
1.38 + return "Mac OS X"
1.39 + elif hasattr(os, "startfile"):
1.40 + return "Windows"
1.41 + else:
1.42 + return None
1.43 +
1.44 +def is_standard():
1.45 +
1.46 + """
1.47 + Return whether the current desktop supports standardised application
1.48 + launching.
1.49 + """
1.50 +
1.51 + return os.environ.has_key("DESKTOP_LAUNCH")
1.52 +
1.53 def open(url, desktop=None):
1.54
1.55 """
1.56 @@ -14,48 +54,45 @@
1.57 particular desktop environment's mechanisms to open the 'url' instead of
1.58 guessing or detecting which environment is being used.
1.59
1.60 - Suggested values for 'desktop' are "KDE", "GNOME", "Mac OS X" and "generic",
1.61 - where "generic" uses an OPENER environment variable to open the specified
1.62 - 'url'.
1.63 + Suggested values for 'desktop' are "standard", "KDE", "GNOME", "Mac OS X",
1.64 + "Windows" where "standard" employs a DESKTOP_LAUNCH environment variable to
1.65 + open the specified 'url'.
1.66
1.67 The process identifier of the "opener" (ie. viewer, editor, browser or
1.68 program) associated with the 'url' is returned by this function. If the
1.69 process identifier cannot be determined, None is returned.
1.70 """
1.71
1.72 - if desktop == "generic" or \
1.73 - desktop is None and os.environ.has_key("OPENER"):
1.74 + # Attempt to detect a desktop environment.
1.75 +
1.76 + detected = get_desktop()
1.77 +
1.78 + # Start with desktops whose existence can be easily tested.
1.79
1.80 - try:
1.81 - # NOTE: This may not handle sophisticated commands properly.
1.82 - cmd = os.environ["OPENER"].split()
1.83 - cmd.append(url)
1.84 - except KeyError, exc:
1.85 - raise OSError, "Desktop not supported (OPENER could not be used)"
1.86 + if (desktop is None or desktop == "standard") and is_standard():
1.87 + # NOTE: This may not handle sophisticated commands properly.
1.88 + cmd = os.environ["DESKTOP_LAUNCH"].split()
1.89 + cmd.append(url)
1.90
1.91 - elif desktop == "KDE" or \
1.92 - desktop is None and (os.environ.has_key("KDE_FULL_SESSION") or
1.93 - os.environ.has_key("KDE_MULTIHEAD")):
1.94 + elif (desktop is None or desktop == "Windows") and detected == "Windows":
1.95 + # NOTE: This returns None in current implementations.
1.96 + return os.startfile(url)
1.97
1.98 + # Test for desktops where the overriding is not verified.
1.99 +
1.100 + elif (desktop or detected) == "KDE":
1.101 cmd = ["kfmclient", "exec", url]
1.102
1.103 - elif desktop == "GNOME" or \
1.104 - desktop is None and (os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or
1.105 - os.environ.has_key("GNOME_KEYRING_SOCKET")):
1.106 -
1.107 + elif (desktop or detected) == "GNOME":
1.108 cmd = ["gnome-open", url]
1.109
1.110 - elif desktop == "Mac OS X" or \
1.111 - desktop is None and sys.platform == "darwin":
1.112 + elif (desktop or detected) == "Mac OS X":
1.113 + cmd = ["open", url]
1.114
1.115 - cmd = ["open", url]
1.116 + # Finish with an error where no suitable desktop was identified.
1.117
1.118 else:
1.119 - try:
1.120 - # NOTE: This returns None in current implementations.
1.121 - return os.startfile(url)
1.122 - except AttributeError, exc:
1.123 - raise OSError, "Desktop not supported (os.startfile could not be used)"
1.124 + raise OSError, "Desktop not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)"
1.125
1.126 return subprocess.Popen(cmd).pid
1.127