Example #1
0
def substitute(s, mapping):
    """Substitute values from *mapping* into *s*.

    *mapping* can be a :class:`dict` or any type that supports the
    ``get()`` method of the mapping protocol. Replacement values are
    copied into the result without further interpretation. Raises
    :exc:`~.SubstitutionSyntaxError` if there are malformed constructs
    in *s*.
    """

    if "$" in s:
        result = ''
        rest = s
        while rest:
            p, name, namecase, rest, vtype = _split(rest)
            result += p
            if name:
                v = None
                if vtype == 'define':
                    v = mapping.get(name)
                if vtype == 'env':
                    v = os.getenv(namecase)

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