1
# -*- coding: utf-8 -*-
3
# Copyright 2011 Canonical Ltd.
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.
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.
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."""
21
__all__ = ['user_home',
23
'default_config_home',
24
'default_config_path',
32
def get_env_path(key, default):
33
"""Get a UTF-8 encoded path from an environment variable."""
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")
43
def unicode_path(utf8path):
44
"""Turn an utf8 path into a unicode path."""
45
return utf8path.decode("utf-8")
48
def get_special_folders():
49
""" Routine to grab all the Windows Special Folders locations.
51
If successful, returns dictionary
52
of shell folder locations indexed on Windows keyword for each;
53
otherwise, returns an empty dictionary.
55
# pylint: disable=W0621, F0401, E0611
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
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")
77
return special_folders
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')
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')