Convert other enums to int for Python 2.5 (Stephen Watson).
[rox-lib/lack.git] / python / rox / basedir.py
blobcbd2406d8bff807c75b70e71ec2745eb001093ba
1 """The freedesktop.org Base Directory specification provides a way for
2 applications to locate shared data and configuration:
4 http://www.freedesktop.org/standards/
6 This module can be used to load and save from and to these directories.
8 Typical usage:
10 from rox import basedir
12 for dir in basedir.load_config_paths('mydomain.org', 'MyProg', 'Options'):
13 print "Load settings from", dir
15 dir = basedir.save_config_path('mydomain.org', 'MyProg')
16 print >>file(os.path.join(dir, 'Options'), 'w'), "foo=2"
18 Note: see the rox.Options module for a higher-level API for managing options.
19 """
21 from __future__ import generators
22 import os
24 _home = os.environ.get('HOME', '/')
25 xdg_data_home = os.environ.get('XDG_DATA_HOME',
26 os.path.join(_home, '.local', 'share'))
28 xdg_data_dirs = [xdg_data_home] + \
29 os.environ.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')
31 xdg_config_home = os.environ.get('XDG_CONFIG_HOME',
32 os.path.join(_home, '.config'))
34 xdg_config_dirs = [xdg_config_home] + \
35 os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(':')
37 xdg_data_dirs = filter(lambda x: x, xdg_data_dirs)
38 xdg_config_dirs = filter(lambda x: x, xdg_config_dirs)
40 def save_config_path(*resource):
41 """Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
42 'resource' should normally be the name of your application. Use this
43 when SAVING configuration settings. Use the xdg_config_dirs variable
44 for loading."""
45 resource = os.path.join(*resource)
46 assert not resource.startswith('/')
47 path = os.path.join(xdg_config_home, resource)
48 if not os.path.isdir(path):
49 os.makedirs(path, 0700)
50 return path
52 def save_data_path(*resource):
53 """Ensure $XDG_DATA_HOME/<resource>/ exists, and return its path.
54 'resource' is the name of some shared resource. Use this when updating
55 a shared (between programs) database. Use the xdg_data_dirs variable
56 for loading."""
57 resource = os.path.join(*resource)
58 assert not resource.startswith('/')
59 path = os.path.join(xdg_data_home, resource)
60 if not os.path.isdir(path):
61 os.makedirs(path)
62 return path
64 def load_config_paths(*resource):
65 """Returns an iterator which gives each directory named 'resource' in the
66 configuration search path. Information provided by earlier directories should
67 take precedence over later ones (ie, the user's config dir comes first)."""
68 resource = os.path.join(*resource)
69 for config_dir in xdg_config_dirs:
70 path = os.path.join(config_dir, resource)
71 if os.path.exists(path): yield path
73 def load_first_config(*resource):
74 """Returns the first result from load_config_paths, or None if there is nothing
75 to load."""
76 for x in load_config_paths(*resource):
77 return x
78 return None
80 def load_data_paths(*resource):
81 """Returns an iterator which gives each directory named 'resource' in the
82 shared data search path. Information provided by earlier directories should
83 take precedence over later ones."""
84 resource = os.path.join(*resource)
85 for data_dir in xdg_data_dirs:
86 path = os.path.join(data_dir, resource)
87 if os.path.exists(path): yield path