From: Petr Špaček Date: Fri, 24 Jun 2022 13:17:22 +0000 (+0200) Subject: Sort grammar map keys while pretty printing them X-Git-Tag: v9.19.3~16^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5c04e3c524cc236c73e98d84d292ae4944b1cc43;p=thirdparty%2Fbind9.git Sort grammar map keys while pretty printing them 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. --- diff --git a/doc/misc/checkgrammar.py b/doc/misc/checkgrammar.py index 8483b2edaf8..09984eceb45 100644 --- a/doc/misc/checkgrammar.py +++ b/doc/misc/checkgrammar.py @@ -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)