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.

(cherry picked from commit 5c04e3c524)
This commit is contained in:
Petr Špaček 2022-06-24 15:17:22 +02:00
parent 9b2bf0f8be
commit ca866c7e92
No known key found for this signature in database
GPG key ID: ABD587CDF06581AE

View 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)