2426
2426
return int(unicode_str)
2429
# Use a an empty dict to initialize an empty configobj avoiding all
2430
# parsing and encoding checks
2431
_list_converter_config = configobj.ConfigObj(
2432
{}, encoding='utf-8', list_values=True, interpolation=False)
2435
2429
def list_from_store(unicode_str):
2436
if not isinstance(unicode_str, basestring):
2438
# Now inject our string directly as unicode. All callers got their value
2439
# from configobj, so values that need to be quoted are already properly
2441
_list_converter_config.reset()
2442
_list_converter_config._parse([u"list=%s" % (unicode_str,)])
2443
maybe_list = _list_converter_config['list']
2444
2430
# ConfigObj return '' instead of u''. Use 'str' below to catch all cases.
2445
if isinstance(maybe_list, basestring):
2431
if isinstance(unicode_str, (str, unicode)):
2447
2433
# A single value, most probably the user forgot (or didn't care to
2448
2434
# add) the final ','
2451
2437
# The empty string, convert to empty list
2454
2440
# We rely on ConfigObj providing us with a list already
2729
2715
co_input = StringIO(bytes)
2731
2717
# The config files are always stored utf8-encoded
2732
self._config_obj = ConfigObj(co_input, encoding='utf-8',
2718
self._config_obj = ConfigObj(co_input, encoding='utf-8')
2734
2719
except configobj.ConfigObjError, e:
2735
2720
self._config_obj = None
2736
2721
raise errors.ParseConfigError(e.errors, self.external_url())
3062
3047
except KeyError:
3063
3048
# Not registered
3065
def expand_and_convert(val):
3066
# This may need to be called twice if the value is None or ends up
3067
# being None during expansion or conversion.
3070
if isinstance(val, basestring):
3071
val = self._expand_options_in_string(val)
3073
trace.warning('Cannot expand "%s":'
3074
' %s does not support option expansion'
3075
% (name, type(val)))
3077
val = opt.convert_from_unicode(val)
3079
value = expand_and_convert(value)
3080
3050
if opt is not None and value is None:
3081
3051
# If the option is registered, it may provide a default value
3082
3052
value = opt.get_default()
3083
value = expand_and_convert(value)
3054
value = self._expand_option_value(value)
3055
if opt is not None and value is not None:
3056
value = opt.convert_from_unicode(value)
3058
# The conversion failed, fallback to the default value
3059
value = opt.get_default()
3061
value = self._expand_option_value(value)
3062
value = opt.convert_from_unicode(value)
3084
3063
for hook in ConfigHooks['get']:
3085
3064
hook(self, name, value)
3067
def _expand_option_value(self, value):
3068
"""Expand the option value depending on its type."""
3069
if isinstance(value, list):
3070
value = self._expand_options_in_list(value)
3071
elif isinstance(value, dict):
3072
trace.warning('Cannot expand "%s":'
3073
' Dicts do not support option expansion'
3075
elif isinstance(value, (str, unicode)):
3076
value = self._expand_options_in_string(value)
3088
3079
def expand_options(self, string, env=None):
3089
3080
"""Expand option references in the string in the configuration context.
3098
3089
return self._expand_options_in_string(string, env)
3091
def _expand_options_in_list(self, slist, env=None, _refs=None):
3092
"""Expand options in a list of strings in the configuration context.
3094
:param slist: A list of strings.
3096
:param env: An option dict defining additional configuration options or
3097
overriding existing ones.
3099
:param _refs: Private list (FIFO) containing the options being
3100
expanded to detect loops.
3102
:returns: The flatten list of expanded strings.
3104
# expand options in each value separately flattening lists
3107
value = self._expand_options_in_string(s, env, _refs)
3108
if isinstance(value, list):
3109
result.extend(value)
3111
result.append(value)
3100
3114
def _expand_options_in_string(self, string, env=None, _refs=None):
3101
3115
"""Expand options in the string in the configuration context.
3139
3157
value = self._expand_option(name, env, _refs)
3140
3158
if value is None:
3141
3159
raise errors.ExpandingUnknownOption(name, string)
3142
chunks.append(value)
3160
if isinstance(value, list):
3162
chunks.extend(value)
3164
chunks.append(value)
3144
3166
chunk_is_ref = False
3145
result = ''.join(chunks)
3168
# Once a list appears as the result of an expansion, all
3169
# callers will get a list result. This allows a consistent
3170
# behavior even when some options in the expansion chain
3171
# defined as strings (no comma in their value) but their
3172
# expanded value is a list.
3173
return self._expand_options_in_list(chunks, env, _refs)
3175
result = ''.join(chunks)
3148
3178
def _expand_option(self, name, env, _refs):
3157
3187
# configs, getting the option value should restart from the top
3158
3188
# config, not the current one) -- vila 20101222
3159
3189
value = self.get(name, expand=False)
3160
value = self._expand_options_in_string(value, env, _refs)
3190
if isinstance(value, list):
3191
value = self._expand_options_in_list(value, env, _refs)
3193
value = self._expand_options_in_string(value, env, _refs)
3163
3196
def _get_mutable_section(self):