3048
3048
except KeyError:
3049
3049
# Not registered
3051
def expand_and_convert(val):
3052
# This may need to be called twice if the value is None and ends up
3053
# being None during expansion or conversion.
3056
if isinstance(val, basestring):
3057
val = self._expand_options_in_string(val)
3059
trace.warning('Cannot expand "%s":'
3060
' %s does not support option expansion'
3061
% (name, type(val)))
3062
if opt is not None and value is not None:
3063
val = opt.convert_from_unicode(val)
3065
value = expand_and_convert(value)
3051
3066
if opt is not None and value is None:
3052
3067
# If the option is registered, it may provide a default value
3053
3068
value = opt.get_default()
3055
value = self._expand_option_value(value)
3056
if opt is not None and value is not None:
3057
value = opt.convert_from_unicode(value)
3059
# The conversion failed, fallback to the default value
3060
value = opt.get_default()
3062
value = self._expand_option_value(value)
3063
value = opt.convert_from_unicode(value)
3069
value = expand_and_convert(value)
3064
3070
for hook in ConfigHooks['get']:
3065
3071
hook(self, name, value)
3068
def _expand_option_value(self, value):
3069
"""Expand the option value depending on its type."""
3070
if isinstance(value, list):
3071
value = self._expand_options_in_list(value)
3072
elif isinstance(value, dict):
3073
trace.warning('Cannot expand "%s":'
3074
' Dicts do not support option expansion'
3076
elif isinstance(value, (str, unicode)):
3077
value = self._expand_options_in_string(value)
3080
3074
def expand_options(self, string, env=None):
3081
3075
"""Expand option references in the string in the configuration context.
3090
3084
return self._expand_options_in_string(string, env)
3092
def _expand_options_in_list(self, slist, env=None, _refs=None):
3093
"""Expand options in a list of strings in the configuration context.
3095
:param slist: A list of strings.
3097
:param env: An option dict defining additional configuration options or
3098
overriding existing ones.
3100
:param _refs: Private list (FIFO) containing the options being
3101
expanded to detect loops.
3103
:returns: The flatten list of expanded strings.
3105
# expand options in each value separately flattening lists
3108
value = self._expand_options_in_string(s, env, _refs)
3109
if isinstance(value, list):
3110
result.extend(value)
3112
result.append(value)
3115
3086
def _expand_options_in_string(self, string, env=None, _refs=None):
3116
3087
"""Expand options in the string in the configuration context.
3158
3125
value = self._expand_option(name, env, _refs)
3159
3126
if value is None:
3160
3127
raise errors.ExpandingUnknownOption(name, string)
3161
if isinstance(value, list):
3163
chunks.extend(value)
3165
chunks.append(value)
3128
chunks.append(value)
3167
3130
chunk_is_ref = False
3169
# Once a list appears as the result of an expansion, all
3170
# callers will get a list result. This allows a consistent
3171
# behavior even when some options in the expansion chain
3172
# defined as strings (no comma in their value) but their
3173
# expanded value is a list.
3174
return self._expand_options_in_list(chunks, env, _refs)
3176
result = ''.join(chunks)
3131
result = ''.join(chunks)
3179
3134
def _expand_option(self, name, env, _refs):
3188
3143
# configs, getting the option value should restart from the top
3189
3144
# config, not the current one) -- vila 20101222
3190
3145
value = self.get(name, expand=False)
3191
if isinstance(value, list):
3192
value = self._expand_options_in_list(value, env, _refs)
3194
value = self._expand_options_in_string(value, env, _refs)
3146
value = self._expand_options_in_string(value, env, _refs)
3197
3149
def _get_mutable_section(self):