root/desktop.py

Revision 79, 1.2 kB (checked in by ploum, 19 months ago)

#86 : patch from Moonz to not depend on GNOME and to open the xdg webbrowser.

Thanks Moonz :-)

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import os as _os
5import sys as _sys
6
7try:
8        import gnome as _gnome
9        _has_gnome = True
10except ImportError:
11        _has_gnome = False
12
13def _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
33def _test_executable(*args):
34        return _spawn_executable(True, True, *args)
35
36def _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
42def 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
Note: See TracBrowser for help on using the browser.