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