def finish(me):
pass
+class ExpectedError (Exception): pass
+
###--------------------------------------------------------------------------
### A bulk DNS resolver.
+class ResolverFailure (ExpectedError):
+ def __init__(me, host, msg):
+ me.host = host
+ me.msg = msg
+ def __str__(me):
+ return "failed to resolve `%s': %s" % (me.host, me.msg)
+
class BulkResolver (object):
"""
Resolve a number of DNS names in parallel.
"""
addr = me._namemap[host]
if addr is None:
- raise KeyError(host)
+ raise ResolverFailure(host, '(unknown failure)')
return addr
def _resolved(me, host, addr):
## Match a $[HOST] name resolution reference; group 1 is the HOST.
RX_RESOLVE = RX.compile(r'(?x) \$ \[ ([^]]+) \]')
-class ConfigSyntaxError (Exception):
+class ConfigSyntaxError (ExpectedError):
def __init__(me, fname, lno, msg):
me.fname = fname
me.lno = lno
def _fmt_path(path):
return ' -> '.join(["`%s'" % hop for hop in path])
-class AmbiguousOptionError (Exception):
+class AmbiguousOptionError (ExpectedError):
def __init__(me, key, patha, vala, pathb, valb):
me.key = key
me.patha, me.vala = patha, vala
"path %s yields `%s' but %s yields `%s'" % \
(me.key, _fmt_path(me.patha), me.vala, _fmt_path(me.pathb), me.valb)
-class InheritanceCycleError (Exception):
+class InheritanceCycleError (ExpectedError):
def __init__(me, key, path):
me.key = key
me.path = path
return "Found a cycle %s looking up key `%s'" % \
(_fmt_path(me.path), me.key)
-class MissingSectionException (Exception):
+class MissingSectionException (ExpectedError):
def __init__(me, sec):
- me.key = key
+ me.sec = sec
def __str__(me):
return "Section `%s' not found" % (me.sec)
-class MissingKeyException (Exception):
+class MissingKeyException (ExpectedError):
def __init__(me, sec, key):
me.sec = sec
me.key = key
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):
string)
return string
- def has_option(me, key):
- """
- Decide whether this section has a configuration key KEY.
+ 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)
- This version of the method properly handles the @inherit key.
- """
- return key == 'name' or me._get(key)[0] is not None
-
- def _get(me, key, map = None, path = None):
+ def _get(me, key, path = None):
"""
Low-level option-fetching method.
Returns None if no value could be found.
"""
- ## If we weren't given a memoization map or path, then we'd better make
- ## one.
- if map is None: map = {}
+ ## 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
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: threadp, value = map[me.name]
+ ## 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 threadp: raise InheritanceCycleError(key, path[:])
+ if p is None: raise InheritanceCycleError(key, path[:])
+ else: return v, path + p
## See whether the answer is ready waiting for us.
try: v = me._itemmap[key]
except KeyError: pass
- else: return v, path[:]
-
- ## No, apparently, not. Find out our list of parents.
- try:
- parents = [me._cp.section(p) for p in
- me._itemmap['@inherit'].replace(',', ' ').split()]
- except KeyError:
- parents = []
+ else:
+ p = path[:]
+ me._cache[key] = v, []
+ return v, p
## Initially we have no idea.
value = None
- winner = None
+ winner = []
## Go through our parents and ask them what they think.
- map[me.name] = True, None
- for p in parents:
+ me._cache[key] = None, None
+ for p in me._parents():
## See whether we get an answer. If not, keep on going.
- v, pp = p._get(key, map, path)
+ v, pp = p._get(key, path)
if v is None: continue
## If we got an answer, check that it matches any previous ones.
raise AmbiguousOptionError(key, winner, value, pp, v)
## That's the best we could manage.
- map[me.name] = False, value
+ me._cache[key] = value, winner[len(path):]
return value, winner
finally:
## Special handling for the `name' key.
if key == 'name':
value = me._itemmap.get('name', me.name)
+ elif key == '@inherits':
+ try: return me._itemmap['@inherits']
+ except KeyError: raise MissingKeyException(me.name, key)
else:
value, _ = me._get(key)
if value is None:
def items(me, resolvep = True):
"""
- Return a list of (NAME, VALUE) items in this section.
-
- This extends the default method by handling the inheritance chain.
+ Yield a list of item names in the section.
"""
## Initialize for a depth-first walk of the inheritance graph.
- d = {}
- visited = {}
- stack = [me.name]
+ seen = { 'name': True }
+ visiting = { me.name: True }
+ stack = [me]
## Visit nodes, collecting their keys. Don't believe the values:
## resolving inheritance is too hard to do like this.
while stack:
- sec = me._cp.section(stack.pop())
- if sec.name in visited: continue
- visited[sec.name] = True
-
- for key, value in sec._itemmap.iteritems():
- if key == '@inherit': stack += value.replace(',', ' ').split()
- else: d[key] = None
+ sec = stack.pop()
+ for p in sec._parents():
+ if p.name not in visiting:
+ stack.append(p); visiting[p.name] = True
- ## Now collect the values for the known keys, one by one.
- items = []
- for key in d: items.append((key, me.get(key, resolvep)))
+ for key in sec._itemmap.iterkeys(): seen[key] = None
## And we're done.
- return items
+ return seen.iterkeys()
class MyConfigParser (object):
"""
resolve hostnames will fail!
"""
for sec in me.sections():
- for key, value in sec.items(resolvep = False):
+ for key in sec.items():
+ value = sec.get(key, resolvep = False)
for match in RX_RESOLVE.finditer(value):
me._resolver.prepare(match.group(1))
me._resolver.run()
label = sec.name
else:
label = 'P%s' % sec.name
- if sec.has_option('auto') and \
- sec.get('auto') in ('y', 'yes', 't', 'true', '1', 'on'):
- auto.append(sec.name)
- if sec.has_option('user'):
- cdb.add('U%s' % sec.get('user'))
- url = M.URLEncode(laxp = True, semip = True)
- for key, value in sorted(sec.items(), key = lambda (k, v): k):
+ try: a = sec.get('auto')
+ except MissingKeyException: pass
+ else:
+ if a in ('y', 'yes', 't', 'true', '1', 'on'): auto.append(sec.name)
+ try: u = sec.get('user')
+ except MissingKeyException: pass
+ else: cdb.add('U%s' % u)
+ url = M.URLEncode(semip = True)
+ for key in sorted(sec.items()):
if not key.startswith('@'):
- url.encode(key, ' '.join(M.split(value)[0]))
+ url.encode(key, sec.get(key))
cdb.add(label, url.result)
cdb.add('%AUTO', ' '.join(auto))
cdb.finish()
cdb = CDB.cdbmake(opts.cdbfile, opts.cdbfile + '.new')
else:
cdb = CDBFake()
- conf = getconf(args[1:])
- output(conf, cdb)
+ try:
+ conf = getconf(args[1:])
+ output(conf, cdb)
+ except ExpectedError, e:
+ M.moan(str(e))
+ exit(2)
if __name__ == '__main__':
main()