133
class ConfigObj(configobj.ConfigObj):
135
def __init__(self, infile=None, **kwargs):
136
# We define our own interpolation mechanism calling it option expansion
137
super(ConfigObj, self).__init__(infile=infile,
142
def get_bool(self, section, key):
143
return self[section].as_bool(key)
145
def get_value(self, section, name):
146
# Try [] for the old DEFAULT section.
147
if section == "DEFAULT":
152
return self[section][name]
155
# FIXME: Until we can guarantee that each config file is loaded once and and
156
# only once for a given bzrlib session, we don't want to re-read the file every
157
# time we query for an option so we cache the value (bad ! watch out for tests
158
# needing to restore the proper value).This shouldn't be part of 2.4.0 final,
159
# yell at mgz^W vila and the RM if this is still present at that time
161
_expand_default_value = None
162
def _get_expand_default_value():
163
global _expand_default_value
164
if _expand_default_value is not None:
165
return _expand_default_value
166
conf = GlobalConfig()
167
# Note that we must not use None for the expand value below or we'll run
168
# into infinite recursion. Using False really would be quite silly ;)
169
expand = conf.get_user_option_as_bool('bzr.config.expand', expand=True)
171
# This is an opt-in feature, you *really* need to clearly say you want
174
_expand_default_value = expand
133
def ConfigObj(*args, **kwargs):
135
if _ConfigObj is None:
136
class ConfigObj(configobj.ConfigObj):
138
def get_bool(self, section, key):
139
return self[section].as_bool(key)
141
def get_value(self, section, name):
142
# Try [] for the old DEFAULT section.
143
if section == "DEFAULT":
148
return self[section][name]
149
_ConfigObj = ConfigObj
150
return _ConfigObj(*args, **kwargs)
178
153
class Config(object):
214
189
def _get_signing_policy(self):
215
190
"""Template method to override signature creation policy."""
219
def expand_options(self, string, env=None):
220
"""Expand option references in the string in the configuration context.
222
:param string: The string containing option to expand.
224
:param env: An option dict defining additional configuration options or
225
overriding existing ones.
227
:returns: The expanded string.
229
return self._expand_options_in_string(string, env)
231
def _expand_options_in_list(self, slist, env=None, _ref_stack=None):
232
"""Expand options in a list of strings in the configuration context.
234
:param slist: A list of strings.
236
:param env: An option dict defining additional configuration options or
237
overriding existing ones.
239
:param _ref_stack: Private list containing the options being
240
expanded to detect loops.
242
:returns: The flatten list of expanded strings.
244
# expand options in each value separately flattening lists
247
value = self._expand_options_in_string(s, env, _ref_stack)
248
if isinstance(value, list):
254
def _expand_options_in_string(self, string, env=None, _ref_stack=None):
255
"""Expand options in the string in the configuration context.
257
:param string: The string to be expanded.
259
:param env: An option dict defining additional configuration options or
260
overriding existing ones.
262
:param _ref_stack: Private list containing the options being
263
expanded to detect loops.
265
:returns: The expanded string.
268
# Not much to expand there
270
if _ref_stack is None:
271
# What references are currently resolved (to detect loops)
273
if self.option_ref_re is None:
274
# We want to match the most embedded reference first (i.e. for
275
# '{{foo}}' we will get '{foo}',
276
# for '{bar{baz}}' we will get '{baz}'
277
self.option_ref_re = re.compile('({[^{}]+})')
279
# We need to iterate until no more refs appear ({{foo}} will need two
280
# iterations for example).
283
raw_chunks = self.option_ref_re.split(result)
285
import pdb; pdb.set_trace()
286
if len(raw_chunks) == 1:
287
# Shorcut the trivial case: no refs
291
# Split will isolate refs so that every other chunk is a ref
293
for chunk in raw_chunks:
296
# Keep only non-empty strings (or we get bogus empty
297
# slots when a list value is involved).
302
if name in _ref_stack:
303
raise errors.OptionExpansionLoop(string, _ref_stack)
304
_ref_stack.append(name)
305
value = self._expand_option(name, env, _ref_stack)
307
raise errors.ExpandingUnknownOption(name, string)
308
if isinstance(value, list):
316
# Once a list appears as the result of an expansion, all
317
# callers will get a list result. This allows a consistent
318
# behavior even when some options in the expansion chain
319
# defined as strings (no comma in their value) but their
320
# expanded value is a list.
321
return self._expand_options_in_list(chunks, env, _ref_stack)
323
result = ''.join(chunks)
326
def _expand_option(self, name, env, _ref_stack):
327
if env is not None and name in env:
328
# Special case, values provided in env takes precedence over
332
# FIXME: This is a limited implementation, what we really need is a
333
# way to query the bzr config for the value of an option,
334
# respecting the scope rules (That is, once we implement fallback
335
# configs, getting the option value should restart from the top
336
# config, not the current one) -- vila 20101222
337
value = self.get_user_option(name, expand=False)
338
if isinstance(value, list):
339
value = self._expand_options_in_list(value, env, _ref_stack)
341
value = self._expand_options_in_string(value, env, _ref_stack)
344
192
def _get_user_option(self, option_name):
345
193
"""Template method to provide a user option."""
348
def get_user_option(self, option_name, expand=None):
349
"""Get a generic option - no special process, no default.
351
:param option_name: The queried option.
353
:param expand: Whether options references should be expanded.
355
:returns: The value of the option.
358
expand = _get_expand_default_value()
359
value = self._get_user_option(option_name)
361
if isinstance(value, list):
362
value = self._expand_options_in_list(value)
363
elif isinstance(value, dict):
364
trace.warning('Cannot expand "%s":'
365
' Dicts do not support option expansion'
368
value = self._expand_options_in_string(value)
371
def get_user_option_as_bool(self, option_name, expand=None):
196
def get_user_option(self, option_name):
197
"""Get a generic option - no special process, no default."""
198
return self._get_user_option(option_name)
200
def get_user_option_as_bool(self, option_name):
372
201
"""Get a generic option as a boolean - no special process, no default.
374
203
:return None if the option doesn't exist or its value can't be
375
204
interpreted as a boolean. Returns True or False otherwise.
377
s = self.get_user_option(option_name, expand=expand)
206
s = self._get_user_option(option_name)
379
208
# The option doesn't exist