From: Mark Wooding Date: Sat, 26 May 2018 13:38:03 +0000 (+0100) Subject: peerdb/tripe-newpeers.in: Split out a class for config sections. X-Git-Tag: 1.5.0~76 X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/commitdiff_plain/e3ec3a3a808f410b778583d9910dd37c877c8ffb?hp=b7e5aa06ec192af281f7acb38f7cf8c8d8363dc8 peerdb/tripe-newpeers.in: Split out a class for config sections. This looks like a mess, but it mostly involves moving a bunch of methods from `MyConfigParser' into the new `ConfigSection' class and fiddling with callers. --- diff --git a/peerdb/tripe-newpeers.in b/peerdb/tripe-newpeers.in index 37c0a341..ed87189f 100644 --- a/peerdb/tripe-newpeers.in +++ b/peerdb/tripe-newpeers.in @@ -156,6 +156,12 @@ class InheritanceCycleError (Exception): return "Found a cycle %s looking up key `%s'" % \ (_fmt_path(me.path), me.key) +class MissingSectionException (Exception): + def __init__(me, sec): + me.key = key + def __str__(me): + return "Section `%s' not found" % (me.sec) + class MissingKeyException (Exception): def __init__(me, sec, key): me.sec = sec @@ -163,6 +169,160 @@ class MissingKeyException (Exception): 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.""" + me.name = name + me._itemmap = dict() + me._cp = cp + + def _expand(me, string, resolvep): + """ + Expands $(...) and (optionally) $[...] 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: me._cp._resolver.lookup(m.group(1)), + string) + return string + + def has_option(me, key): + """ + Decide whether this section has a configuration key KEY. + + 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): + """ + 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 memoization map or path, then we'd better make + ## one. + if map is None: map = {} + 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: threadp, value = map[me.name] + except KeyError: pass + else: + if threadp: raise InheritanceCycleError(key, path[:]) + + ## 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 = [] + + ## Initially we have no idea. + value = None + winner = None + + ## Go through our parents and ask them what they think. + map[me.name] = True, None + for p in parents: + + ## See whether we get an answer. If not, keep on going. + v, pp = p._get(key, map, path) + if v is None: continue + + ## If we got an answer, check that it matches any previous ones. + if value is None: + value = v + winner = pp + elif value != v: + raise AmbiguousOptionError(key, winner, value, pp, v) + + ## That's the best we could manage. + map[me.name] = False, value + return value, winner + + finally: + ## Remove us from the path again. + path.pop() + + def get(me, key, resolvep = True): + """ + Retrieve the value of KEY from this section. + """ + + ## Special handling for the `name' key. + if key == 'name': + value = me._itemmap.get('name', me.name) + else: + value, _ = me._get(key) + if value is None: + raise MissingKeyException(me.name, key) + + ## Expand the value and return it. + return me._expand(value, resolvep) + + 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. + """ + + ## Initialize for a depth-first walk of the inheritance graph. + d = {} + visited = {} + stack = [me.name] + + ## 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 + + ## Now collect the values for the known keys, one by one. + items = [] + for key in d: items.append((key, me.get(key, resolvep))) + + ## And we're done. + return items + class MyConfigParser (object): """ A more advanced configuration parser. @@ -188,7 +348,10 @@ class MyConfigParser (object): 2. Call resolve() to collect the hostnames which need to be resolved and actually do the name resolution. - 3. Call get(SECTION, ITEM) to collect the results, or items(SECTION) to + 3. Call sections() to get a list of the configuration sections, or + section(NAME) to find a named section. + + 4. Call get(ITEM) on a section to collect the results, or items() to iterate over them. """ @@ -219,7 +382,7 @@ class MyConfigParser (object): ## Commit a key's value when we've determined that there are no further ## continuation lines. def flush(): - if key is not None: sect[key] = val.getvalue() + if key is not None: sect._itemmap[key] = val.getvalue() ## Work through all of the input lines. for line in f: @@ -239,7 +402,7 @@ class MyConfigParser (object): flush() name = m[0].group(1) try: sect = me._sectmap[name] - except KeyError: sect = me._sectmap[name] = dict() + except KeyError: sect = me._sectmap[name] = ConfigSection(name, me) key = None elif match(RX_ASSGN): @@ -267,9 +430,14 @@ class MyConfigParser (object): ## Don't forget to commit any final value material. flush() + def section(me, name): + """Return a ConfigSection with the given NAME.""" + try: return me._sectmap[name] + except KeyError: raise MissingSectionException(name) + def sections(me): - """Yield the known section names.""" - return me._sectmap.iterkeys() + """Yield the known sections.""" + return me._sectmap.itervalues() def resolve(me): """ @@ -278,152 +446,12 @@ class MyConfigParser (object): Until you call this, attempts to fetch configuration items which need to resolve hostnames will fail! """ - for sec in me._sectmap.iterkeys(): - for key, value in me.items(sec, resolvep = False): + for sec in me.sections(): + for key, value in sec.items(resolvep = False): for match in RX_RESOLVE.finditer(value): me._resolver.prepare(match.group(1)) me._resolver.run() - def _expand(me, sec, string, resolvep): - """ - Expands $(...) and (optionally) $[...] placeholders in STRING. - - The SEC is the configuration section from which to satisfy $(...) - requests. RESOLVEP is a boolean switch: do we bother to tax the resolver - or not? This is turned off by the resolve() method while it's collecting - hostnames to be resolved. - """ - string = RX_REF.sub \ - (lambda m: me.get(sec, m.group(1), resolvep), string) - if resolvep: - string = RX_RESOLVE.sub(lambda m: me._resolver.lookup(m.group(1)), - string) - return string - - def has_option(me, sec, key): - """ - Decide whether section SEC has a configuration key KEY. - - This version of the method properly handles the @inherit key. - """ - return key == 'name' or me._get(sec, key)[0] is not None - - def _get(me, sec, key, map = None, path = None): - """ - Low-level option-fetching method. - - Fetch the value for the named KEY from section SEC, or maybe - (recursively) a section which SEC 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 memoization map or path, then we'd better make - ## one. - if map is None: map = {} - if path is None: path = [] - - ## Extend the path to cover the lookup section, 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(sec) - 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[sec] - except KeyError: pass - else: - if threadp: raise InheritanceCycleError(key, path[:]) - - ## See whether the answer is ready waiting for us. - try: v = me._sectmap[sec][key] - except KeyError: pass - else: return v, path[:] - - ## No, apparently, not. Find out our list of parents. - try: - parents = me._sectmap[sec]['@inherit'].replace(',', ' ').split() - except KeyError: - parents = [] - - ## Initially we have no idea. - value = None - winner = None - - ## Go through our parents and ask them what they think. - map[sec] = True, None - for p in parents: - - ## See whether we get an answer. If not, keep on going. - v, pp = me._get(p, key, map, path) - if v is None: continue - - ## If we got an answer, check that it matches any previous ones. - if value is None: - value = v - winner = pp - elif value != v: - raise AmbiguousOptionError(key, winner, value, pp, v) - - ## That's the best we could manage. - map[sec] = False, value - return value, winner - - finally: - ## Remove us from the path again. - path.pop() - - def get(me, sec, key, resolvep = True): - """ - Retrieve the value of KEY from section SEC. - """ - - ## Special handling for the `name' key. - if key == 'name': - value = me._sectmap[sec].get('name', sec) - else: - value, _ = me._get(sec, key) - if value is None: - raise MissingKeyException(sec, key) - - ## Expand the value and return it. - return me._expand(sec, value, resolvep) - - def items(me, sec, resolvep = True): - """ - Return a list of (NAME, VALUE) items in section SEC. - - This extends the default method by handling the inheritance chain. - """ - - ## Initialize for a depth-first walk of the inheritance graph. - d = {} - visited = {} - basesec = sec - stack = [sec] - - ## Visit nodes, collecting their keys. Don't believe the values: - ## resolving inheritance is too hard to do like this. - while stack: - sec = stack.pop() - if sec in visited: continue - visited[sec] = True - - for key, value in me._sectmap[sec].iteritems(): - if key == '@inherit': stack += value.replace(',', ' ').split() - else: d[key] = None - - ## Now collect the values for the known keys, one by one. - items = [] - for key in d: items.append((key, me.get(basesec, key, resolvep))) - - ## And we're done. - return items - ###-------------------------------------------------------------------------- ### Command-line handling. @@ -490,20 +518,20 @@ def output(conf, cdb): This is where the special `user' and `auto' database entries get set. """ auto = [] - for sec in sorted(conf.sections()): - if sec.startswith('@'): + for sec in sorted(conf.sections(), key = lambda sec: sec.name): + if sec.name.startswith('@'): continue - elif sec.startswith('$'): - label = sec + elif sec.name.startswith('$'): + label = sec.name else: - label = 'P%s' % sec - if conf.has_option(sec, 'auto') and \ - conf.get(sec, 'auto') in ('y', 'yes', 't', 'true', '1', 'on'): - auto.append(sec) - if conf.has_option(sec, 'user'): - cdb.add('U%s' % conf.get(sec, 'user'), sec) + 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(conf.items(sec), key = lambda (k, v): k): + for key, value in sorted(sec.items(), key = lambda (k, v): k): if not key.startswith('@'): url.encode(key, ' '.join(M.split(value)[0])) cdb.add(label, url.result)