move drlaunch in drlaunch
[drlaunch] / drlaunch / src / xdg / BaseDirectory.py
1 """
2 This module is based on a rox module (LGPL):
3
4 http://cvs.sourceforge.net/viewcvs.py/rox/ROX-Lib2/python/rox/basedir.py?rev=1.9&view=log
5
6 The freedesktop.org Base Directory specification provides a way for
7 applications to locate shared data and configuration:
8
9     http://standards.freedesktop.org/basedir-spec/
10
11 (based on version 0.6)
12
13 This module can be used to load and save from and to these directories.
14
15 Typical usage:
16
17     from rox import basedir
18     
19     for dir in basedir.load_config_paths('mydomain.org', 'MyProg', 'Options'):
20         print "Load settings from", dir
21
22     dir = basedir.save_config_path('mydomain.org', 'MyProg')
23     print >>file(os.path.join(dir, 'Options'), 'w'), "foo=2"
24
25 Note: see the rox.Options module for a higher-level API for managing options.
26 """
27
28 from __future__ import generators
29 import os
30
31 _home = os.environ.get('HOME', '/')
32 xdg_data_home = os.environ.get('XDG_DATA_HOME',
33             os.path.join(_home, '.local', 'share'))
34
35 xdg_data_dirs = [xdg_data_home] + \
36     os.environ.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')
37
38 xdg_config_home = os.environ.get('XDG_CONFIG_HOME',
39             os.path.join(_home, '.config'))
40
41 xdg_config_dirs = [xdg_config_home] + \
42     os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(':')
43
44 xdg_cache_home = os.environ.get('XDG_CACHE_HOME',
45             os.path.join(_home, '.cache'))
46
47 xdg_data_dirs = filter(lambda x: x, xdg_data_dirs)
48 xdg_config_dirs = filter(lambda x: x, xdg_config_dirs)
49
50 def save_config_path(*resource):
51     """Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
52     'resource' should normally be the name of your application. Use this
53     when SAVING configuration settings. Use the xdg_config_dirs variable
54     for loading."""
55     resource = os.path.join(*resource)
56     assert not resource.startswith('/')
57     path = os.path.join(xdg_config_home, resource)
58     if not os.path.isdir(path):
59         os.makedirs(path, 0700)
60     return path
61
62 def save_data_path(*resource):
63     """Ensure $XDG_DATA_HOME/<resource>/ exists, and return its path.
64     'resource' is the name of some shared resource. Use this when updating
65     a shared (between programs) database. Use the xdg_data_dirs variable
66     for loading."""
67     resource = os.path.join(*resource)
68     assert not resource.startswith('/')
69     path = os.path.join(xdg_data_home, resource)
70     if not os.path.isdir(path):
71         os.makedirs(path)
72     return path
73
74 def load_config_paths(*resource):
75     """Returns an iterator which gives each directory named 'resource' in the
76     configuration search path. Information provided by earlier directories should
77     take precedence over later ones (ie, the user's config dir comes first)."""
78     resource = os.path.join(*resource)
79     for config_dir in xdg_config_dirs:
80         path = os.path.join(config_dir, resource)
81         if os.path.exists(path): yield path
82
83 def load_first_config(*resource):
84     """Returns the first result from load_config_paths, or None if there is nothing
85     to load."""
86     for x in load_config_paths(*resource):
87         return x
88     return None
89
90 def load_data_paths(*resource):
91     """Returns an iterator which gives each directory named 'resource' in the
92     shared data search path. Information provided by earlier directories should
93     take precedence over later ones."""
94     resource = os.path.join(*resource)
95     for data_dir in xdg_data_dirs:
96         path = os.path.join(data_dir, resource)
97         if os.path.exists(path): yield path