]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Sort grammar map keys while pretty printing them
authorPetr Špaček <pspacek@isc.org>
Fri, 24 Jun 2022 13:17:22 +0000 (15:17 +0200)
committerPetr Špaček <pspacek@isc.org>
Fri, 1 Jul 2022 06:59:23 +0000 (08:59 +0200)
It would be too easy if we could just call sorted(). Thanks to zone
grammar the most important key "type" gets sorted near end, so we pull
it up to the top using a hack.

doc/misc/checkgrammar.py

index 8483b2edaf8844fee132b7c9fef027c4f2e1cc90..09984eceb450624c4c8a74b282d26a0f37cda95c 100644 (file)
@@ -102,6 +102,14 @@ def diff_statements(whole_grammar, places):
 
 def pformat_grammar(node, level=1):
     """Pretty print a given grammar node in the same way as cfg_test would"""
+
+    def sortkey(item):
+        """Treat 'type' specially and always put it first, for zone types"""
+        key, _ = item
+        if key == "type":
+            return ""
+        return key
+
     if "_grammar" in node:  # no nesting
         assert "_id" not in node
         assert "_mapbody" not in node
@@ -118,7 +126,7 @@ def pformat_grammar(node, level=1):
             out += node["_id"] + " "
         out += "{\n"
 
-    for key, subnode in node["_mapbody"].items():
+    for key, subnode in sorted(node["_mapbody"].items(), key=sortkey):
         if not subnode.get("_ignore_this_level"):
             out += f"{indent}{subnode.get('_pprint_name', key)}"
             inner_grammar = pformat_grammar(node["_mapbody"][key], level=level + 1)