peerdb/tripe-newpeers.in: Factor out calculating the parent-section list.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 27 May 2018 01:20:47 +0000 (02:20 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 14 Jun 2018 11:50:38 +0000 (12:50 +0100)
peerdb/tripe-newpeers.in

index 2287dab..8b4d800 100644 (file)
@@ -199,6 +199,13 @@ class ConfigSection (object):
                               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, map = None, path = None):
     """
     Low-level option-fetching method.
@@ -235,20 +242,13 @@ class ConfigSection (object):
       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:
+      for p in me._parents():
 
         ## See whether we get an answer.  If not, keep on going.
         v, pp = p._get(key, map, path)
@@ -293,18 +293,18 @@ class ConfigSection (object):
     ## Initialize for a depth-first walk of the inheritance graph.
     d = {}
     visited = {}
-    stack = [me.name]
+    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())
+      sec = stack.pop()
       if sec.name in visited: continue
       visited[sec.name] = True
+      stack += sec._parents()
 
-      for key, value in sec._itemmap.iteritems():
-        if key == '@inherit': stack += value.replace(',', ' ').split()
-        else: d[key] = None
+      for key in sec._itemmap.iterkeys():
+        if key != '@inherit': d[key] = None
 
     ## And we're done.
     return d.iterkeys()