+RX_REF = RX.compile(r'(?x) \$ \( ([^)]+) \)')
+
+## Match a $FLAGS[HOST] name resolution reference; group 1 are the flags;
+## group 2 is the HOST.
+RX_RESOLVE = RX.compile(r'(?x) \$ ([46*]*) \[ ([^]]+) \]')
+
+class ConfigSyntaxError (ExpectedError):
+ def __init__(me, fname, lno, msg):
+ me.fname = fname
+ me.lno = lno
+ me.msg = msg
+ def __str__(me):
+ return '%s:%d: %s' % (me.fname, me.lno, me.msg)
+
+def _fmt_path(path):
+ return ' -> '.join(["`%s'" % hop for hop in path])
+
+class AmbiguousOptionError (ExpectedError):
+ def __init__(me, key, patha, vala, pathb, valb):
+ me.key = key
+ me.patha, me.vala = patha, vala
+ me.pathb, me.valb = pathb, valb
+ def __str__(me):
+ return "Ambiguous answer resolving key `%s': " \
+ "path %s yields `%s' but %s yields `%s'" % \
+ (me.key, _fmt_path(me.patha), me.vala, _fmt_path(me.pathb), me.valb)
+
+class InheritanceCycleError (ExpectedError):
+ def __init__(me, key, path):
+ me.key = key
+ me.path = path
+ def __str__(me):
+ return "Found a cycle %s looking up key `%s'" % \
+ (_fmt_path(me.path), me.key)
+
+class MissingSectionException (ExpectedError):
+ def __init__(me, sec):
+ me.sec = sec
+ def __str__(me):
+ return "Section `%s' not found" % (me.sec)
+
+class MissingKeyException (ExpectedError):
+ def __init__(me, sec, key):
+ me.sec = sec
+ me.key = key
+ def __str__(me):
+ return "Key `%s' not found in section `%s'" % (me.key, me.sec)
+
+class ConfigSection (object):
+ """
+ A section in a configuration parser.
+
+ This is where a lot of the nitty-gritty stuff actually happens. The
+ `MyConfigParser' knows a lot about the internals of this class, which saves
+ on building a complicated interface.
+ """
+
+ 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.