~dobey/dirspec/python3

« back to all changes in this revision

Viewing changes to dirspec/utils.py

  • Committer: Rodney Dawes
  • Date: 2011-12-17 00:46:26 UTC
  • mto: This revision was merged to the branch mainline in revision 2.
  • Revision ID: rodney.dawes@canonical.com-20111217004626-0acfk0hg85ko9o3g
Add tests, and the implementation of the XDG Base Directory spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2011 Canonical Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3
 
7
# as published by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
"""Utilities for multiplatform support of XDG directory handling."""
 
17
 
 
18
import os
 
19
import sys
 
20
 
 
21
__all__ = ['user_home',
 
22
           'default_cache_home',
 
23
           'default_config_home',
 
24
           'default_config_path',
 
25
           'default_data_home',
 
26
           'default_data_path',
 
27
           'get_env_path',
 
28
           'unicode_path',
 
29
           ]
 
30
 
 
31
 
 
32
def get_env_path(key, default):
 
33
    """Get a UTF-8 encoded path from an environment variable."""
 
34
    if key in os.environ:
 
35
        # on windows, environment variables are mbcs bytes
 
36
        # so we must turn them into utf-8 Syncdaemon paths
 
37
        path = os.environ.get(key)
 
38
        return path.decode(sys.getfilesystemencoding()).encode("utf-8")
 
39
    else:
 
40
        return default
 
41
 
 
42
 
 
43
def unicode_path(utf8path):
 
44
    """Turn an utf8 path into a unicode path."""
 
45
    return utf8path.decode("utf-8")
 
46
 
 
47
 
 
48
def get_special_folders():
 
49
    """ Routine to grab all the Windows Special Folders locations.
 
50
 
 
51
    If successful, returns dictionary
 
52
    of shell folder locations indexed on Windows keyword for each;
 
53
    otherwise, returns an empty dictionary.
 
54
    """
 
55
    # pylint: disable=W0621, F0401, E0611
 
56
    special_folders = {}
 
57
 
 
58
    if sys.platform == 'win32':
 
59
        from win32com.shell import shell, shellcon
 
60
        # CSIDL_LOCAL_APPDATA = C:\Users\<username>\AppData\Local
 
61
        # CSIDL_PROFILE = C:\Users\<username>
 
62
        # CSIDL_COMMON_APPDATA = C:\ProgramData
 
63
        # More information on these constants at
 
64
        # http://msdn.microsoft.com/en-us/library/bb762494
 
65
 
 
66
        # per http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181,
 
67
        # SHGetFolderPath is deprecated, replaced by SHGetKnownFolderPath
 
68
        # (http://msdn.microsoft.com/en-us/library/windows/desktop/bb762188)
 
69
        get_path = lambda name: shell.SHGetFolderPath(
 
70
            0, getattr(shellcon, name), None, 0).encode('utf8')
 
71
        special_folders['Personal'] = get_path("CSIDL_PROFILE")
 
72
        special_folders['Local AppData'] = get_path("CSIDL_LOCAL_APPDATA")
 
73
        special_folders['AppData'] = os.path.dirname(
 
74
            special_folders['Local AppData'])
 
75
        special_folders['Common AppData'] = get_path("CSIDL_COMMON_APPDATA")
 
76
 
 
77
    return special_folders
 
78
 
 
79
 
 
80
# pylint: disable=C0103
 
81
if sys.platform == 'win32':
 
82
    special_folders = get_special_folders()
 
83
    user_home = special_folders['Personal']
 
84
    default_config_path = special_folders['Common AppData']
 
85
    default_config_home = special_folders['Local AppData']
 
86
    default_data_path = os.path.join(default_config_path, 'xdg')
 
87
    default_data_home = os.path.join(default_config_home, 'xdg')
 
88
    default_cache_home = os.path.join(default_data_home, 'cache')
 
89
else:
 
90
    user_home = os.path.expanduser('~')
 
91
    default_cache_home = os.path.join(user_home, '.cache')
 
92
    default_config_path = '/etc/xdg'
 
93
    default_config_home = os.path.join(user_home, '.config')
 
94
    default_data_path = '/usr/local/share:/usr/share'
 
95
    default_data_home = os.path.join(user_home, '.local', 'share')