[Zodb-checkins] CVS: Packages/ZConfig - Substitution.py:1.1 ApacheStyle.py:1.3 Config.py:1.4 Interpolation.py:NONE

Fred L. Drake, Jr. fred@zope.com
Thu, 7 Nov 2002 10:29:44 -0500


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

Modified Files:
	ApacheStyle.py Config.py 
Added Files:
	Substitution.py 
Removed Files:
	Interpolation.py 
Log Message:
Call string substitution "substitution" instead of "interpolation".  As Shane
pointed out, interpolation is a bad name for this, since it implies something
different.


=== Added File Packages/ZConfig/Substitution.py ===
"""Substitution support for ZConfig values."""

class SubstitutionError(Exception):
    """Base exception for string substitution errors."""

    def __init__(self, msg, context):
        self.message = msg
        self.context = context

    def __str__(self):
        return self.message

class SubstitutionSyntaxError(SubstitutionError):
    """Raised when interpolation source text contains syntactical errors."""

    def __init__(self, msg, context):
        if context is not None:
            context = context[:]
        SubstitutionError.__init__(self, msg, context)

class SubstitutionRecursionError(SubstitutionError):
    """Raised when a nested interpolation is recursive."""

    def __init__(self, name, context):
        self.name = name
        msg = ("recursion on %s; current context:\n%s"
               % (repr(name), ", ".join(context)))
        SubstitutionError.__init__(self, msg, context[:])


def get(section, name, default=None):
    # XXX should this interpolate from default if that's what's used?
    missing = []
    s = section.get(name, missing)
    if s is missing:
        return default
    if "$" in s:
        accum = []
        _interp(accum, s, section, [name])
        s = ''.join(accum)
    return s


def substitute(s, section):
    """Interpolate variables from `section` into `s`."""
    if '$' in s:
        accum = []
        _interp(accum, s, section, None)
        s = ''.join(accum)
    return s


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)
            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[length+1:]
        else:
            m = _name_match(rest)
            if not m:
                accum.append("$")
                continue
            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):]


import re
_name_match = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*").match
del re


=== Packages/ZConfig/ApacheStyle.py 1.2 => 1.3 ===
--- Packages/ZConfig/ApacheStyle.py:1.2	Mon Oct 21 14:40:09 2002
+++ Packages/ZConfig/ApacheStyle.py	Thu Nov  7 10:29:14 2002
@@ -32,6 +32,7 @@
             if type.lower() != section.type:
                 raise ConfigurationSyntaxError(
                     "unbalanced section end", url, lineno)
+            section.finish()
             section = stack.pop()
             continue
         if line[0] == "<":


=== Packages/ZConfig/Config.py 1.3 => 1.4 ===
--- Packages/ZConfig/Config.py:1.3	Mon Oct 21 14:59:54 2002
+++ Packages/ZConfig/Config.py	Thu Nov  7 10:29:14 2002
@@ -25,6 +25,9 @@
         else:
             return "<%s at 0x%x>" % (classname, id(self))
 
+    def finish(self):
+        return self
+
     def setDelegate(self, section):
         if self.delegate is not None:
             raise ConfigurationError("cannot modify delegation")

=== Removed File Packages/ZConfig/Interpolation.py ===