+ def __init__(me, name, cp):
+ """Initialize a new, empty section with a given NAME and parent CP."""
+
+ ## The cache maps item keys to entries, which consist of a pair of
+ ## objects. There are four possible states for a cache entry:
+ ##
+ ## * missing -- there is no entry at all with this key, so we must
+ ## search for it;
+ ##
+ ## * None, None -- we are actively trying to resolve this key, so if we
+ ## encounter this state, we have found a cycle in the inheritance
+ ## graph;
+ ##
+ ## * None, [] -- we know that this key isn't reachable through any of
+ ## our parents;
+ ##
+ ## * VALUE, PATH -- we know that the key resolves to VALUE, along the
+ ## PATH from us (exclusive) to the defining parent (inclusive).
+ me.name = name
+ me._itemmap = dict()
+ me._cache = dict()
+ me._cp = cp
+
+ def _expand(me, string, resolvep):
+ """
+ Expands $(...) and (optionally) $FLAGS[...] placeholders in STRING.
+
+ RESOLVEP is a boolean switch: do we bother to tax the resolver or not?
+ This is turned off by MyConfigParser's resolve() method while it's
+ collecting hostnames to be resolved.
+ """
+ string = RX_REF.sub(lambda m: me.get(m.group(1), resolvep), string)
+ if resolvep:
+ string = RX_RESOLVE.sub(
+ lambda m: ' '.join(me._cp._resolver.lookup(m.group(2), m.group(1))),
+ string)
+ return string
+
+ def _parents(me):
+ """Yield this section's parents."""
+ try: names = me._itemmap['@inherit']
+ except KeyError: return
+ for name in names.replace(',', ' ').split():
+ yield me._cp.section(name)
+
+ def _get(me, key, path = None):
+ """
+ Low-level option-fetching method.
+
+ Fetch the value for the named KEY in this section, or maybe (recursively)
+ a section which it inherits from.
+
+ Returns a pair VALUE, PATH. The value is not expanded; nor do we check
+ for the special `name' key. The caller is expected to do these things.
+ Returns None if no value could be found.
+ """
+
+ ## If we weren't given a path, then we'd better make one.
+ if path is None: path = []
+
+ ## Extend the path to cover us, but remember to remove us again when
+ ## we've finished. If we need to pass the current path back upwards,
+ ## then remember to take a copy.
+ path.append(me.name)
+ try:
+
+ ## If we've been this way before on another pass through then return the
+ ## value we found then. If we're still thinking about it then we've
+ ## found a cycle.
+ try: v, p = me._cache[key]
+ except KeyError: pass
+ else:
+ if p is None: raise InheritanceCycleError(key, path[:])
+ else: return v, path + p