| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | import os as _os |
|---|
| 5 | import sys as _sys |
|---|
| 6 | |
|---|
| 7 | try: |
|---|
| 8 | import gnome as _gnome |
|---|
| 9 | _has_gnome = True |
|---|
| 10 | except ImportError: |
|---|
| 11 | _has_gnome = False |
|---|
| 12 | |
|---|
| 13 | def _spawn_executable(close_stdout = False, close_stderr = False, *args): |
|---|
| 14 | pid = _os.fork() |
|---|
| 15 | if pid == 0: |
|---|
| 16 | # Child process |
|---|
| 17 | |
|---|
| 18 | # Close stdout and/or stderr |
|---|
| 19 | null = _os.open('/dev/null', _os.O_WRONLY) |
|---|
| 20 | if close_stdout: _os.dup2(null, 1) |
|---|
| 21 | if close_stderr: _os.dup2(null, 2) |
|---|
| 22 | _os.close(null) |
|---|
| 23 | |
|---|
| 24 | # Run it |
|---|
| 25 | try: |
|---|
| 26 | _os.execlp(args[0], *args) |
|---|
| 27 | except OSError: |
|---|
| 28 | _sys.exit(127) |
|---|
| 29 | else: |
|---|
| 30 | status = _os.waitpid(pid, 0)[1] |
|---|
| 31 | return _os.WIFEXITED(status) and (_os.WEXITSTATUS(status) == 0) |
|---|
| 32 | |
|---|
| 33 | def _test_executable(*args): |
|---|
| 34 | return _spawn_executable(True, True, *args) |
|---|
| 35 | |
|---|
| 36 | def _spawn_quiet(*args): |
|---|
| 37 | return _spawn_executable(True, False, *args) |
|---|
| 38 | |
|---|
| 39 | _has_xdg = _test_executable('xdg-open', '--help') |
|---|
| 40 | _has_exo = _test_executable('exo-open', '--help') |
|---|
| 41 | |
|---|
| 42 | def url_show(url): |
|---|
| 43 | if _has_xdg: # freedesktop is the best choice :p |
|---|
| 44 | return _spawn_quiet('xdg-open', url) |
|---|
| 45 | elif _has_gnome: # shouldn't also check for gnome-open ? |
|---|
| 46 | return _gnome.url_show(url) |
|---|
| 47 | elif _has_exo: # for xfce |
|---|
| 48 | return _spawn_quiet('exo-open', url) |
|---|
| 49 | # add your favorite desktop here ;) |
|---|
| 50 | |
|---|
| 51 | return False |
|---|