1
# Authors: Manuel de la Pena <manuel@canonical.com>
2
# Diego Sarmentero <diego.sarmentero@canonical.com>
4
# Copyright 2011 Canonical Ltd.
6
# This program is free software: you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License version 3, as published
8
# by the Free Software Foundation.
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranties of
12
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13
# PURPOSE. See the GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License along
16
# with this program. If not, see <http://www.gnu.org/licenses/>.
18
"""XDG helpers for windows."""
23
def get_special_folders():
24
# Routine to grab all the Windows Special Folders locations.
25
# If successful, returns dictionary
26
# of shell folder locations indexed on Windows keyword for each;
27
# otherwise, returns an empty dictionary.
30
from win32com.shell import shell, shellcon
31
# CSIDL_LOCAL_APPDATA = C:\Users\<username>\AppData\Local
32
# CSIDL_PROFILE = C:\Users\<username>
33
# CSIDL_COMMON_APPDATA = C:\ProgramData
34
# More information on these at
35
# http://msdn.microsoft.com/en-us/library/bb762494(v=vs.85).aspx
36
get_path = lambda name: shell.SHGetFolderPath(
37
0, getattr(shellcon, name), None, 0).encode('utf8')
38
special_folders['Personal'] = get_path("CSIDL_PROFILE")
39
special_folders['Local AppData'] = get_path("CSIDL_LOCAL_APPDATA")
40
special_folders['AppData'] = os.path.dirname(
41
special_folders['Local AppData'])
42
special_folders['Common AppData'] = get_path("CSIDL_COMMON_APPDATA")
43
return special_folders
45
special_folders = get_special_folders()
47
home_path = special_folders['Personal']
48
app_local_data_path = special_folders['Local AppData']
49
app_global_data_path = special_folders['Common AppData']
51
# use the non roaming app data
52
xdg_data_home = os.environ.get('XDG_DATA_HOME',
53
os.path.join(app_local_data_path, 'xdg'))
57
"""Returns XDG data directories."""
58
return os.environ.get('XDG_DATA_DIRS',
59
'{0}{1}{2}'.format(app_local_data_path, os.pathsep,
60
app_global_data_path)).split(os.pathsep)
62
xdg_data_dirs = get_data_dirs()
64
# we will return the roaming data wich is as close as we get in windows
66
xdg_cache_home = os.environ.get('XDG_CACHE_HOME',
67
os.path.join(xdg_data_home, 'cache'))
69
# point to the not roaming app data for the user
70
xdg_config_home = os.environ.get('XDG_CONFIG_HOME',
74
def get_config_dirs():
75
"""Return XDG config directories."""
76
return [xdg_config_home] + \
77
os.environ.get('XDG_CONFIG_DIRS',
81
xdg_config_dirs = get_config_dirs()
83
xdg_data_dirs = filter(lambda x: x, xdg_data_dirs)
84
xdg_config_dirs = filter(lambda x: x, xdg_config_dirs)
87
def load_config_paths(*resource):
88
"""Iterator of configuration paths.
90
Return an iterator which gives each directory named 'resource' in
91
the configuration search path. Information provided by earlier
92
directories should take precedence over later ones (ie, the user's
93
config dir comes first).
95
resource = os.path.join(*resource)
96
for config_dir in xdg_config_dirs:
97
path = os.path.join(config_dir, resource)
98
if os.path.exists(path):
102
def save_config_path(*resource):
103
"""Path to save configuration.
105
Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
106
'resource' should normally be the name of your application. Use this
107
when SAVING configuration settings. Use the xdg_config_dirs variable
110
resource = os.path.join(*resource)
111
assert not resource.startswith('/')
112
path = os.path.join(xdg_config_home, resource)
113
if not os.path.isdir(path):
114
os.makedirs(path, 0700)