~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Vincent Ladeuil
  • Date: 2011-09-02 15:15:56 UTC
  • mto: (6123.1.7 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6124.
  • Revision ID: v.ladeuil+lp@free.fr-20110902151556-6nr1f3tvpa0d9tc0
Remove duplication in stack.get() and useless code now that list conversion is explicit and delayed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3048
3048
        except KeyError:
3049
3049
            # Not registered
3050
3050
            opt = None
 
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.
 
3054
            if val is not None:
 
3055
                if expand:
 
3056
                    if isinstance(val, basestring):
 
3057
                        val = self._expand_options_in_string(val)
 
3058
                    else:
 
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)
 
3064
            return 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()
3054
 
        if expand:
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)
3058
 
            if value is None:
3059
 
                # The conversion failed, fallback to the default value
3060
 
                value = opt.get_default()
3061
 
                if expand:
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)
3066
3072
        return value
3067
3073
 
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'
3075
 
                          % (name,))
3076
 
        elif isinstance(value, (str, unicode)):
3077
 
            value = self._expand_options_in_string(value)
3078
 
        return value
3079
 
 
3080
3074
    def expand_options(self, string, env=None):
3081
3075
        """Expand option references in the string in the configuration context.
3082
3076
 
3089
3083
        """
3090
3084
        return self._expand_options_in_string(string, env)
3091
3085
 
3092
 
    def _expand_options_in_list(self, slist, env=None, _refs=None):
3093
 
        """Expand options in  a list of strings in the configuration context.
3094
 
 
3095
 
        :param slist: A list of strings.
3096
 
 
3097
 
        :param env: An option dict defining additional configuration options or
3098
 
            overriding existing ones.
3099
 
 
3100
 
        :param _refs: Private list (FIFO) containing the options being
3101
 
            expanded to detect loops.
3102
 
 
3103
 
        :returns: The flatten list of expanded strings.
3104
 
        """
3105
 
        # expand options in each value separately flattening lists
3106
 
        result = []
3107
 
        for s in slist:
3108
 
            value = self._expand_options_in_string(s, env, _refs)
3109
 
            if isinstance(value, list):
3110
 
                result.extend(value)
3111
 
            else:
3112
 
                result.append(value)
3113
 
        return result
3114
 
 
3115
3086
    def _expand_options_in_string(self, string, env=None, _refs=None):
3116
3087
        """Expand options in the string in the configuration context.
3117
3088
 
3140
3111
                # Shorcut the trivial case: no refs
3141
3112
                return result
3142
3113
            chunks = []
3143
 
            list_value = False
3144
3114
            # Split will isolate refs so that every other chunk is a ref
3145
3115
            chunk_is_ref = False
3146
3116
            for chunk in raw_chunks:
3147
3117
                if not chunk_is_ref:
3148
 
                    if chunk:
3149
 
                        # Keep only non-empty strings (or we get bogus empty
3150
 
                        # slots when a list value is involved).
3151
 
                        chunks.append(chunk)
 
3118
                    chunks.append(chunk)
3152
3119
                    chunk_is_ref = True
3153
3120
                else:
3154
3121
                    name = chunk[1:-1]
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):
3162
 
                        list_value = True
3163
 
                        chunks.extend(value)
3164
 
                    else:
3165
 
                        chunks.append(value)
 
3128
                    chunks.append(value)
3166
3129
                    _refs.pop()
3167
3130
                    chunk_is_ref = False
3168
 
            if list_value:
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)
3175
 
            else:
3176
 
                result = ''.join(chunks)
 
3131
            result = ''.join(chunks)
3177
3132
        return result
3178
3133
 
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)
3193
 
            else:
3194
 
                value = self._expand_options_in_string(value, env, _refs)
 
3146
            value = self._expand_options_in_string(value, env, _refs)
3195
3147
        return value
3196
3148
 
3197
3149
    def _get_mutable_section(self):