[Zope-Checkins] CVS: Packages/ZConfig - substitution.py:1.1.2.1 cfgparser.py:1.1.2.6 Substitution.py:NONE

Fred L. Drake, Jr. fred@zope.com
Fri, 20 Dec 2002 12:49:31 -0500


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

Modified Files:
      Tag: zconfig-schema-devel-branch
	cfgparser.py 
Added Files:
      Tag: zconfig-schema-devel-branch
	substitution.py 
Removed Files:
      Tag: zconfig-schema-devel-branch
	Substitution.py 
Log Message:
Use new-style name for the Substitution module (now substitution).

=== Added File Packages/ZConfig/substitution.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Substitution support for ZConfig values."""

import ZConfig

try:
    True
except NameError:
    True = 1
    False = 0


def substitute(s, mapping):
    """Interpolate variables from `section` into `s`."""
    if "$" in s:
        result = ''
        rest = s
        while rest:
            p, name, rest = _split(rest)
            result += p
            if name:
                v = mapping.get(name)
                if v is None:
                    raise ZConfig.SubstitutionReplacementError(s, name)
                result += v
        return result
    else:
        return s


def isname(s):
    """Return True iff s is a valid substitution name."""
    m = _name_match(s)
    if m:
        return m.group() == s
    else:
        return False


def _split(s):
    # Return a triple:  prefix, name, suffix
    # - prefix is text that can be used literally in the result (may be '')
    # - name is a referenced name, or None
    # - suffix is trailling text that may contain additional references
    #   (may be '' or None)
    if "$" in s:
        i = s.find("$")
        c = s[i+1:i+2]
        if c == "":
            raise ZConfig.SubstitutionSyntaxError(
                "illegal lone '$' at end of source")
        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 ZConfig.SubstitutionSyntaxError(
                    "'${' not followed by name")
            name = m.group(0)
            i = m.end() + 1
            if not s.startswith("}", i - 1):
                raise ZConfig.SubstitutionSyntaxError(
                    "'${%s' not followed by '}'" % name)
        else:
            m = _name_match(s, i+1)
            if not m:
                raise ZConfig.SubstitutionSyntaxError(
                    "'$' not followed by '$' or name")
            name = m.group(0)
            i = m.end()
        return prefix, name.lower(), s[i:]
    else:
        return s, None, None


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


=== Packages/ZConfig/cfgparser.py 1.1.2.5 => 1.1.2.6 ===
--- Packages/ZConfig/cfgparser.py:1.1.2.5	Thu Dec 12 14:25:21 2002
+++ Packages/ZConfig/cfgparser.py	Fri Dec 20 12:49:00 2002
@@ -14,7 +14,7 @@
 """Configuration parser."""
 
 from ZConfig import ConfigurationError, ConfigurationSyntaxError
-from ZConfig.Substitution import isname, substitute
+from ZConfig.substitution import isname, substitute
 
 try:
     True

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