1
2
3
4
5
6
7
8
9 """Registry-like monster"""
10
11 __docformat__ = 'restructuredtext'
12
13 from ConfigParser import SafeConfigParser
14 import os.path
15
16
18 """Central configuration registry for PyMVPA.
19
20 The purpose of this class is to collect all configurable settings used by
21 various parts of PyMVPA. It is fairly simple and does only little more
22 than the standard Python ConfigParser. Like ConfigParser it is blind to the
23 data that it stores, i.e. not type checking is performed.
24
25 Configuration files (INI syntax) in multiple location are passed when the
26 class is instanciated or whenever `Config.reload()` is called later on.
27 By default it looks for a config file named `pymvpa.cfg` in the current
28 directory and `.pymvpa.cfg` in the user's home directory. Morever, the
29 constructor takes an optional argument with a list of additional file names
30 to parse.
31
32 In addition to configuration files, this class also looks for special
33 environment variables to read settings from. Names of such variables have to
34 start with `MVPA_` following by the an optional section name and the
35 variable name itself ('_' as delimiter). If no section name is provided,
36 the variables will be associated with section `general`. Some examples::
37
38 MVPA_VERBOSE=1
39
40 will become::
41
42 [general]
43 verbose = 1
44
45 However, `MVPA_VERBOSE_OUTPUT=stdout` becomes::
46
47 [verbose]
48 output = stdout
49
50 Any lenght of variable name as allowed, e.g. MVPA_SEC1_LONG_VARIABLE_NAME=1
51 becomes::
52
53 [sec1]
54 long variable name = 1
55
56 Settings from custom configuration files (specified by the constructor
57 argument) have the highest priority and override settings found in the
58 current directory. They in turn override user-specific settings and finally
59 the content of any `MVPA_*` environment variables overrides all settings
60 read from any file.
61 """
62
63
64 _DEFAULTS = {'general':
65 {
66 'verbose': '1',
67 }
68 }
69
70
72 """Initialization reads settings from config files and env. variables.
73
74 :Parameters:
75 filenames: list of filenames
76 """
77 SafeConfigParser.__init__(self)
78
79
80 if not filenames is None:
81 self.__cfg_filenames = filenames
82 else:
83 self.__cfg_filenames = []
84
85
86 for sec, vars in ConfigManager._DEFAULTS.iteritems():
87 self.add_section(sec)
88 for key, value in vars.iteritems():
89 self.set(sec, key, value)
90
91
92 self.reload()
93
94
96 """Re-read settings from all configured locations.
97 """
98
99 filenames = self.__cfg_filenames \
100 + ['pymvpa.cfg',
101 os.path.join(os.path.expanduser('~'), '.pymvpa.cfg')]
102
103
104 files = self.read(filenames)
105
106
107 for var in [v for v in os.environ.keys() if v.startswith('MVPA_')]:
108
109 svar = var[5:].lower()
110
111
112 if not svar.count('_'):
113 sec = 'general'
114 else:
115 cut = svar.find('_')
116 sec = svar[:cut]
117 svar = svar[cut + 1:].replace('_', ' ')
118
119
120 if not self.has_section(sec):
121 self.add_section(sec)
122
123
124 self.set(sec, svar, os.environ[var])
125
126
128 """Generate INI file content with current configuration.
129 """
130
131 class file2str(object):
132 def __init__(self):
133 self.__s = ''
134 def write(self, val):
135 self.__s += val
136 def str(self):
137 return self.__s
138
139 r = file2str()
140 self.write(r)
141
142 return r.str()
143
144
145 - def save(self, filename):
146 """Write current configuration to a file.
147 """
148 f = open(filename, 'w')
149 self.write(f)
150 f.close()
151
152
153 - def get(self, section, option, default=None, **kwargs):
154 """Wrapper around SafeConfigParser.get() with a custom default value.
155
156 This method simply wraps the base class method, but adds a `default`
157 keyword argument. The value of `default` is returned whenever the
158 config parser does not have the requested option and/or section.
159 """
160 if not self.has_option(section, option):
161 return default
162
163 return SafeConfigParser.get(self, section, option, **kwargs)
164
165
166 - def getboolean(self, section, option, default=None):
167 """Wrapper around SafeConfigParser.getboolean() with a custom default.
168
169 This method simply wraps the base class method, but adds a `default`
170 keyword argument. The value of `default` is returned whenever the
171 config parser does not have the requested option and/or section.
172 """
173 if not self.has_option(section, option):
174 if isinstance(default, bool):
175 return default
176 else:
177 if default.lower() not in self._boolean_states:
178 raise ValueError, 'Not a boolean: %s' % default
179 return self._boolean_states[default.lower()]
180
181 return SafeConfigParser.getboolean(self, section, option)
182
183
184 - def getAsDType(self, section, option, dtype, default=None):
185 """Convenience method to query options with a custom default and type
186
187 This method simply wraps the base class method, but adds a `default`
188 keyword argument. The value of `default` is returned whenever the
189 config parser does not have the requested option and/or section.
190
191 In addition, the returned value is converted into the specified `dtype`.
192 """
193 if not self.has_option(section, option):
194 return default
195
196 return SafeConfigParser._get(self, section, dtype, option)
197