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