Refactor tests to use the Selections object instead of the Policy object
[zeroinstall/zeroinstall-mseaborn.git] / zeroinstall / support / basedir.py
blob8d2b9678843ff34f825e4b730f66277573a1aeb2
1 """
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}
7 """
9 # Copyright (C) 2006, Thomas Leonard
10 # See the README file for details, or visit http://0install.net.
12 import os
14 _home = os.environ.get('HOME', '/')
16 xdg_data_home = os.environ.get('XDG_DATA_HOME',
17 os.path.join(_home, '.local', 'share'))
19 xdg_data_dirs = [xdg_data_home] + \
20 os.environ.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')
22 xdg_cache_home = os.environ.get('XDG_CACHE_HOME',
23 os.path.join(_home, '.cache'))
25 xdg_cache_dirs = [xdg_cache_home] + \
26 os.environ.get('XDG_CACHE_DIRS', '/var/cache').split(':')
28 xdg_config_home = os.environ.get('XDG_CONFIG_HOME',
29 os.path.join(_home, '.config'))
31 xdg_config_dirs = [xdg_config_home] + \
32 os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(':')
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)
38 def save_config_path(*resource):
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
50 def load_config_paths(*resource):
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
59 def load_first_config(*resource):
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
66 def save_cache_path(*resource):
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
76 def load_cache_paths(*resource):
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
85 def load_first_cache(*resource):
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
92 def load_data_paths(*resource):
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
102 def load_first_data(*resource):
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