[Zodb-checkins] CVS: Packages/ZConfig - Substitution.py:1.4

Fred L. Drake, Jr. fred@zope.com
Mon, 2 Dec 2002 17:52:31 -0500


Update of /cvs-repository/Packages/ZConfig
In directory cvs.zope.org:/tmp/cvs-serv26939

Modified Files:
	Substitution.py 
Log Message:
Refactor the code that splits a referenced variable name from the
surrounding text.


=== Packages/ZConfig/Substitution.py 1.3 => 1.4 ===
--- Packages/ZConfig/Substitution.py:1.3	Thu Nov  7 14:03:22 2002
+++ Packages/ZConfig/Substitution.py	Mon Dec  2 17:52:27 2002
@@ -52,29 +52,10 @@
 
 def _interp(accum, rest, section, context):
     while 1:
-        i = rest.find("$")
-        if i < 0:
-            accum.append(rest)
-            break
-        accum.append(rest[:i])
-        rest = rest[i+1:]
-        if not rest:
-            accum.append("$")
-            break
-        if rest[0] == "$":
-            accum.append("$")
-            rest = rest[1:]
-        elif rest[0] == "{":
-            rest = rest[1:]
-            m = _name_match(rest[:])
-            if not m:
-                raise SubstitutionSyntaxError("'${' not followed by name",
-                                              context)
-            name = m.group(0)
-            length = len(name)
-            if rest[length:length+1] != "}":
-                raise SubstitutionSyntaxError(
-                    "'${%s' not followed by '}'" % name, context)
+        s, name, rest = _split(rest)
+        if s:
+            accum.append(s)
+        if name:
             v = section.get(name)
             if v is None:
                 parent = getattr(section, "container", None)
@@ -91,21 +72,38 @@
                 _interp(accum, v, section, context + [name])
             else:
                 accum.append(v)
-            rest = rest[length+1:]
+        if not rest:
+            return
+
+
+def _split(s, context=None):
+    if "$" in s:
+        i = s.find("$")
+        c = s[i+1:i+2]
+        if c == "":
+            return s, None, None
+        if c == "$":
+            return s[:i+1], None, s[i+2:]
+        prefix = s[:i]
+        if c == "{":
+            m = _name_match(s, i + 2)
+            if not m:
+                raise SubstitutionSyntaxError("'${' not followed by name",
+                                              context)
+            name = m.group(0)
+            i = m.end() + 1
+            if not s.startswith("}", i - 1):
+                raise SubstitutionSyntaxError(
+                    "'${%s' not followed by '}'" % name, context)
         else:
-            m = _name_match(rest)
+            m = _name_match(s, i+1)
             if not m:
-                accum.append("$")
-                continue
+                return prefix + "$", None, s[i+1:]
             name = m.group(0)
-            v = section.get(name, "")
-            if "$" in v and context:
-                if name in context:
-                    raise SubstitutionRecursionError(name, context)
-                _interp(accum, v, section, context + [name])
-            else:
-                accum.append(v)
-            rest = rest[len(name):]
+            i = m.end()
+        return prefix, name, s[i:]
+    else:
+        return s, None, None
 
 
 import re