1 #!/usr/bin/env python 2 3 "Simple desktop integration for Python." 4 5 import os 6 import subprocess 7 8 def open(url, desktop=None): 9 10 """ 11 Open the 'url' in the current desktop's preferred file browser. If the 12 optional 'desktop' parameter is specified then attempt to use that 13 particular desktop environment's mechanisms to open the 'url' instead of 14 guessing or detecting which environment is being used. 15 16 Suggested values for 'desktop' are "KDE" and "GNOME". 17 18 The process identifier of the "opener" (ie. viewer, editor, browser or 19 program) associated with the 'url' is returned by this function. If the 20 process identifier cannot be determined, None is returned. 21 """ 22 23 if desktop == "KDE" or \ 24 desktop is None and (os.environ.has_key("KDE_FULL_SESSION") or 25 os.environ.has_key("KDE_MULTIHEAD")): 26 27 cmd = ["kfmclient", "openURL", url] 28 29 elif desktop == "GNOME" or \ 30 desktop is None and (os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or 31 os.environ.has_key("GNOME_KEYRING_SOCKET")): 32 33 cmd = ["gnome-open", url] 34 35 else: 36 try: 37 # NOTE: This returns None in current implementations. 38 return os.startfile(url) 39 except AttributeError, exc: 40 raise OSError, "Desktop not supported (os.startfile could not be used)" 41 42 return subprocess.Popen(cmd).pid 43 44 # vim: tabstop=4 expandtab shiftwidth=4