1 """
2 Support code for the freedesktop.org basedir spec.
3
4 This module provides functions for locating configuration files.
5
6 @see: U{http://freedesktop.org/wiki/Standards/basedir-spec}
7 """
8
9
10
11
12 import os
13
14 _home = os.environ.get('HOME', '/')
15
16 xdg_data_home = os.environ.get('XDG_DATA_HOME',
17 os.path.join(_home, '.local', 'share'))
18
19 xdg_data_dirs = [xdg_data_home] + \
20 os.environ.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')
21
22 xdg_cache_home = os.environ.get('XDG_CACHE_HOME',
23 os.path.join(_home, '.cache'))
24
25 xdg_cache_dirs = [xdg_cache_home] + \
26 os.environ.get('XDG_CACHE_DIRS', '/var/cache').split(':')
27
28 xdg_config_home = os.environ.get('XDG_CONFIG_HOME',
29 os.path.join(_home, '.config'))
30
31 xdg_config_dirs = [xdg_config_home] + \
32 os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(':')
33
34 xdg_data_dirs = filter(lambda x: x, xdg_data_dirs)
35 xdg_cache_dirs = filter(lambda x: x, xdg_cache_dirs)
36 xdg_config_dirs = filter(lambda x: x, xdg_config_dirs)
37
39 """Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
40 'resource' should normally be the name of your application. Use this
41 when SAVING configuration settings. Use the xdg_config_dirs variable
42 for loading."""
43 resource = os.path.join(*resource)
44 assert not resource.startswith('/')
45 path = os.path.join(xdg_config_home, resource)
46 if not os.path.isdir(path):
47 os.makedirs(path, 0700)
48 return path
49
51 """Returns an iterator which gives each directory named 'resource' in the
52 configuration search path. Information provided by earlier directories should
53 take precedence over later ones (ie, the user's config dir comes first)."""
54 resource = os.path.join(*resource)
55 for config_dir in xdg_config_dirs:
56 path = os.path.join(config_dir, resource)
57 if os.path.exists(path): yield path
58
60 """Returns the first result from load_config_paths, or None if there is nothing
61 to load."""
62 for x in load_config_paths(*resource):
63 return x
64 return None
65
67 """Ensure $XDG_CACHE_HOME/<resource>/ exists, and return its path.
68 'resource' should normally be the name of your application."""
69 resource = os.path.join(*resource)
70 assert not resource.startswith('/')
71 path = os.path.join(xdg_cache_home, resource)
72 if not os.path.isdir(path):
73 os.makedirs(path, 0700)
74 return path
75
77 """Returns an iterator which gives each directory named 'resource' in the
78 cache search path. Information provided by earlier directories should
79 take precedence over later ones (ie, the user's cache dir comes first)."""
80 resource = os.path.join(*resource)
81 for cache_dir in xdg_cache_dirs:
82 path = os.path.join(cache_dir, resource)
83 if os.path.exists(path): yield path
84
86 """Returns the first result from load_cache_paths, or None if there is nothing
87 to load."""
88 for x in load_cache_paths(*resource):
89 return x
90 return None
91
93 """Returns an iterator which gives each directory named 'resource' in the
94 shared data search path. Information provided by earlier directories should
95 take precedence over later ones.
96 @since: 0.28"""
97 resource = os.path.join(*resource)
98 for data_dir in xdg_data_dirs:
99 path = os.path.join(data_dir, resource)
100 if os.path.exists(path): yield path
101
103 """Returns the first result from load_data_paths, or None if there is nothing
104 to load.
105 @since: 0.28"""
106 for x in load_data_paths(*resource):
107 return x
108 return None
109