Rev 6094: Remove duplication in stack.get() and useless code now that list conversion is explicit and delayed. in file:///home/vila/src/bzr/experimental/expand-in-stack/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Sep 2 15:15:56 UTC 2011


At file:///home/vila/src/bzr/experimental/expand-in-stack/

------------------------------------------------------------
revno: 6094
revision-id: v.ladeuil+lp at free.fr-20110902151556-6nr1f3tvpa0d9tc0
parent: v.ladeuil+lp at free.fr-20110902143023-61h8n8cm3r96lov9
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: expand-in-stack
timestamp: Fri 2011-09-02 17:15:56 +0200
message:
  Remove duplication in stack.get() and useless code now that list conversion is explicit and delayed.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-09-02 14:30:23 +0000
+++ b/bzrlib/config.py	2011-09-02 15:15:56 +0000
@@ -3048,35 +3048,29 @@
         except KeyError:
             # Not registered
             opt = None
+        def expand_and_convert(val):
+            # This may need to be called twice if the value is None and ends up
+            # being None during expansion or conversion.
+            if val is not None:
+                if expand:
+                    if isinstance(val, basestring):
+                        val = self._expand_options_in_string(val)
+                    else:
+                        trace.warning('Cannot expand "%s":'
+                                      ' %s does not support option expansion'
+                                      % (name, type(val)))
+                if opt is not None and value is not None:
+                    val = opt.convert_from_unicode(val)
+            return val
+        value = expand_and_convert(value)
         if opt is not None and value is None:
             # If the option is registered, it may provide a default value
             value = opt.get_default()
-        if expand:
-            value = self._expand_option_value(value)
-        if opt is not None and value is not None:
-            value = opt.convert_from_unicode(value)
-            if value is None:
-                # The conversion failed, fallback to the default value
-                value = opt.get_default()
-                if expand:
-                    value = self._expand_option_value(value)
-                value = opt.convert_from_unicode(value)
+            value = expand_and_convert(value)
         for hook in ConfigHooks['get']:
             hook(self, name, value)
         return value
 
-    def _expand_option_value(self, value):
-        """Expand the option value depending on its type."""
-        if isinstance(value, list):
-            value = self._expand_options_in_list(value)
-        elif isinstance(value, dict):
-            trace.warning('Cannot expand "%s":'
-                          ' Dicts do not support option expansion'
-                          % (name,))
-        elif isinstance(value, (str, unicode)):
-            value = self._expand_options_in_string(value)
-        return value
-
     def expand_options(self, string, env=None):
         """Expand option references in the string in the configuration context.
 
@@ -3089,29 +3083,6 @@
         """
         return self._expand_options_in_string(string, env)
 
-    def _expand_options_in_list(self, slist, env=None, _refs=None):
-        """Expand options in  a list of strings in the configuration context.
-
-        :param slist: A list of strings.
-
-        :param env: An option dict defining additional configuration options or
-            overriding existing ones.
-
-        :param _refs: Private list (FIFO) containing the options being
-            expanded to detect loops.
-
-        :returns: The flatten list of expanded strings.
-        """
-        # expand options in each value separately flattening lists
-        result = []
-        for s in slist:
-            value = self._expand_options_in_string(s, env, _refs)
-            if isinstance(value, list):
-                result.extend(value)
-            else:
-                result.append(value)
-        return result
-
     def _expand_options_in_string(self, string, env=None, _refs=None):
         """Expand options in the string in the configuration context.
 
@@ -3140,15 +3111,11 @@
                 # Shorcut the trivial case: no refs
                 return result
             chunks = []
-            list_value = False
             # Split will isolate refs so that every other chunk is a ref
             chunk_is_ref = False
             for chunk in raw_chunks:
                 if not chunk_is_ref:
-                    if chunk:
-                        # Keep only non-empty strings (or we get bogus empty
-                        # slots when a list value is involved).
-                        chunks.append(chunk)
+                    chunks.append(chunk)
                     chunk_is_ref = True
                 else:
                     name = chunk[1:-1]
@@ -3158,22 +3125,10 @@
                     value = self._expand_option(name, env, _refs)
                     if value is None:
                         raise errors.ExpandingUnknownOption(name, string)
-                    if isinstance(value, list):
-                        list_value = True
-                        chunks.extend(value)
-                    else:
-                        chunks.append(value)
+                    chunks.append(value)
                     _refs.pop()
                     chunk_is_ref = False
-            if list_value:
-                # Once a list appears as the result of an expansion, all
-                # callers will get a list result. This allows a consistent
-                # behavior even when some options in the expansion chain
-                # defined as strings (no comma in their value) but their
-                # expanded value is a list.
-                return self._expand_options_in_list(chunks, env, _refs)
-            else:
-                result = ''.join(chunks)
+            result = ''.join(chunks)
         return result
 
     def _expand_option(self, name, env, _refs):
@@ -3188,10 +3143,7 @@
             # configs, getting the option value should restart from the top
             # config, not the current one) -- vila 20101222
             value = self.get(name, expand=False)
-            if isinstance(value, list):
-                value = self._expand_options_in_list(value, env, _refs)
-            else:
-                value = self._expand_options_in_string(value, env, _refs)
+            value = self._expand_options_in_string(value, env, _refs)
         return value
 
     def _get_mutable_section(self):



More information about the bazaar-commits mailing list