2 Support code for the freedesktop.org basedir spec.
4 This module provides functions for locating configuration files.
6 @see: U{http://freedesktop.org/wiki/Standards/basedir-spec}
8 @var home: The value of $HOME (or '/' if not set). If we're running as root and
9 $HOME isn't owned by root, then this will be root's home from /etc/passwd
13 # Copyright (C) 2009, Thomas Leonard
14 # See the README file for details, or visit http://0install.net.
16 from zeroinstall
import _
19 home
= os
.environ
.get('HOME', '/')
23 except AttributeError:
27 # We're running as root. Ensure that $HOME really is root's home,
28 # not the user's home, or we're likely to fill it will unreadable
30 home_owner
= os
.stat(home
).st_uid
33 from logging
import info
35 home
= pwd
.getpwuid(0).pw_dir
or '/'
36 info(_("$HOME (%(home)s) is owned by user %(user)d, but we are root (0). Using %(root_home)s instead."), {'old_home': old_home
, 'user': home_owner
, 'root_home': home
})
41 from win32com
.shell
import shell
, shellcon
42 appData
= shell
.SHGetFolderPath(0, shellcon
.CSIDL_APPDATA
, 0, 0)
43 localAppData
= shell
.SHGetFolderPath(0, shellcon
.CSIDL_LOCAL_APPDATA
, 0, 0)
44 commonAppData
= shell
.SHGetFolderPath(0, shellcon
.CSIDL_COMMON_APPDATA
, 0, 0)
46 xdg_data_home
= appData
47 xdg_data_dirs
= [xdg_data_home
, commonAppData
]
49 xdg_cache_home
= localAppData
50 xdg_cache_dirs
= [xdg_cache_home
, commonAppData
]
52 xdg_config_home
= appData
53 xdg_config_dirs
= [xdg_config_home
, commonAppData
]
55 xdg_data_home
= os
.environ
.get('XDG_DATA_HOME',
56 os
.path
.join(home
, '.local', 'share'))
58 xdg_data_dirs
= [xdg_data_home
] + \
59 os
.environ
.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')
61 xdg_cache_home
= os
.environ
.get('XDG_CACHE_HOME',
62 os
.path
.join(home
, '.cache'))
64 xdg_cache_dirs
= [xdg_cache_home
] + \
65 os
.environ
.get('XDG_CACHE_DIRS', '/var/cache').split(':')
67 xdg_config_home
= os
.environ
.get('XDG_CONFIG_HOME',
68 os
.path
.join(home
, '.config'))
70 xdg_config_dirs
= [xdg_config_home
] + \
71 os
.environ
.get('XDG_CONFIG_DIRS', '/etc/xdg').split(':')
73 xdg_data_dirs
= filter(lambda x
: x
, xdg_data_dirs
)
74 xdg_cache_dirs
= filter(lambda x
: x
, xdg_cache_dirs
)
75 xdg_config_dirs
= filter(lambda x
: x
, xdg_config_dirs
)
77 def save_config_path(*resource
):
78 """Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
79 'resource' should normally be the name of your application. Use this
80 when SAVING configuration settings. Use the xdg_config_dirs variable
82 resource
= os
.path
.join(*resource
)
83 assert not os
.path
.isabs(resource
)
84 path
= os
.path
.join(xdg_config_home
, resource
)
85 if not os
.path
.isdir(path
):
86 os
.makedirs(path
, 0o700)
89 def load_config_paths(*resource
):
90 """Returns an iterator which gives each directory named 'resource' in the
91 configuration search path. Information provided by earlier directories should
92 take precedence over later ones (ie, the user's config dir comes first)."""
93 resource
= os
.path
.join(*resource
)
94 for config_dir
in xdg_config_dirs
:
95 path
= os
.path
.join(config_dir
, resource
)
96 if os
.path
.exists(path
): yield path
98 def load_first_config(*resource
):
99 """Returns the first result from load_config_paths, or None if there is nothing
101 for x
in load_config_paths(*resource
):
105 def save_cache_path(*resource
):
106 """Ensure $XDG_CACHE_HOME/<resource>/ exists, and return its path.
107 'resource' should normally be the name of your application."""
108 resource
= os
.path
.join(*resource
)
109 assert not os
.path
.isabs(resource
)
110 path
= os
.path
.join(xdg_cache_home
, resource
)
111 if not os
.path
.isdir(path
):
112 os
.makedirs(path
, 0o700)
115 def load_cache_paths(*resource
):
116 """Returns an iterator which gives each directory named 'resource' in the
117 cache search path. Information provided by earlier directories should
118 take precedence over later ones (ie, the user's cache dir comes first)."""
119 resource
= os
.path
.join(*resource
)
120 for cache_dir
in xdg_cache_dirs
:
121 path
= os
.path
.join(cache_dir
, resource
)
122 if os
.path
.exists(path
): yield path
124 def load_first_cache(*resource
):
125 """Returns the first result from load_cache_paths, or None if there is nothing
127 for x
in load_cache_paths(*resource
):
131 def load_data_paths(*resource
):
132 """Returns an iterator which gives each directory named 'resource' in the
133 shared data search path. Information provided by earlier directories should
134 take precedence over later ones.
136 resource
= os
.path
.join(*resource
)
137 for data_dir
in xdg_data_dirs
:
138 path
= os
.path
.join(data_dir
, resource
)
139 if os
.path
.exists(path
): yield path
141 def load_first_data(*resource
):
142 """Returns the first result from load_data_paths, or None if there is nothing
145 for x
in load_data_paths(*resource
):