aboutsummaryrefslogtreecommitdiffstats
path: root/lvc/openfiles.py
blob: 377f49ab0cfd009bf3ac82312ce1d61d582977c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""openfiles.py -- open files/folders."""

import logging
import os
import subprocess
import sys


# To open paths we use an OS-specific command.  The approach is from:
# http://stackoverflow.com/questions/6631299/python-opening-a-folder-in-explorer-nautilus-mac-thingie

def check_kde():
    return os.environ.get("KDE_FULL_SESSION", None) is not None


def check_xorg():
    return os.environ.get("XDG_SESSION_ID", None) is not None


def _open_path_osx(path):
    subprocess.call(['open', '--', path])


def _open_path_kde(path):
    # kfmclient is part of konqueror
    subprocess.call(["kfmclient", "exec", "file://" + path])


def _open_path_xorg(path):
    subprocess.call(['xdg-open', path])


def _open_path_gnome(path):
    subprocess.call(['gnome-open', '--', path])


def _open_path_windows(path):
    subprocess.call(['explorer', path])


def _open_path(path):
    if sys.platform == 'darwin':
        _open_path_osx(path)
    elif sys.platform == 'linux2':
        if check_kde():
            _open_path_kde(path)
        elif check_xorg():
            _open_path_xorg(path)
        else:
            _open_path_gnome(path)
    elif sys.platform == 'win32':
        _open_path_windows(path)
    else:
        logging.warn("unknown platform: %s", sys.platform)


def reveal_folder(path):
    """Show a folder in the desktop shell (finder/explorer/nautilous, etc)."""
    logging.info("reveal_folder: %s", path)
    if os.path.isdir(path):
        _open_path(path)
    else:
        _open_path(os.path.dirname(path))