1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Simple XDG Base Directory Specification implementation.
23
24 See http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
25
26 Currently only configuration files handling is implemented."""
27
28
29 import os
30 from flumotion.common.python import makedirs
31
32
33 APPLICATION = 'flumotion'
34
35
37 """
38 Get the path of the config directory, taking into account the
39 XDG_CONFIG_HOME environment variable.
40 """
41
42
43 home = os.environ.get('HOME', '')
44 config_home = os.environ.get('XDG_CONFIG_HOME')
45 if not config_home:
46 config_home = os.path.join(home, '.config')
47 return config_home
48
49
51 """
52 Get the path of the config file with the given name, taking into account
53 the XDG_CONFIG_HOME and XDG_CONFIG_DIRS environment variables.
54
55 @param name: The name of the config file
56 @type name: str
57
58 @returns: full path to the file or None if it does not exist
59 """
60
61 search_path = [config_home_path()]
62
63 config_dirs = os.environ.get('XDG_CONFIG_DIRS')
64 if config_dirs:
65 search_path.extend(config_dirs.split(':'))
66
67 for path in search_path:
68 candidate = os.path.join(path, APPLICATION, name)
69 if os.access(candidate, os.F_OK | os.R_OK):
70 return candidate
71 return None
72
73
75 """
76 Get file-like object for the config file with the given name, taking into
77 account the XDG_CONFIG_HOME environment variable.
78 Create intermidient directories and the file itself according to the XDG
79 Specification in case the file does not exist.
80
81 May raise EnvironmentError if the file or directories cannot be created.
82
83 @param name: The name of the config file
84 @type name: str
85 @param mode: The mode to use when opening the file, 'w' by default.
86 @type mode: str
87
88 @returns: a file-like object
89 """
90
91 path = os.path.join(config_home_path(), APPLICATION, name)
92 dirname = os.path.dirname(path)
93
94 if not os.path.exists(dirname):
95
96 makedirs(dirname, 0700)
97
98 return file(path, mode)
99