Skip to content

Commit

Permalink
adt: add --sort-nodes option
Browse files Browse the repository at this point in the history
Signed-off-by: Alba Mendez <[email protected]>
  • Loading branch information
mildsunrise committed Nov 2, 2024
1 parent 9acaf43 commit 282ac4f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions proxyclient/m1n1/adt.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,18 @@ def _fmt_prop(self, k, v):
else:
return str(v)

def __str__(self, t="", sort_keys=False):
def __str__(self, t="", sort_keys=False, sort_nodes=False):
props = self._properties.items()
if sort_keys:
props = sorted(props)
children = self._children
if sort_nodes:
children = sorted(children, key=lambda n: n.name)
return "\n".join([
t + f"{self.name} {{",
*(t + f" {repr(k)[1:-1]} = {self._fmt_prop(k, v).replace('\n', '\n'+t+' ')}" for k, v in props if k != "name"),
*([""] if self._children else []),
*(i.__str__(t + " ", sort_keys) for i in self._children),
*(i.__str__(t + " ", sort_keys, sort_nodes) for i in children),
t + "}"
])

Expand Down Expand Up @@ -813,6 +816,7 @@ def load_adt(data):
parser.add_argument('input', type=pathlib.Path)
parser.add_argument('output', nargs='?', type=pathlib.Path)
parser.add_argument('--sort-keys', help='sort property keys within a node (useful for diffs)', action='store_true')
parser.add_argument('--sort-nodes', help='sort node children by name (useful for diffs)', action='store_true')
parser.add_argument('-r', '--retrieve', help='retrieve and store the adt from m1n1', action='store_true')
parser.add_argument('-a', '--dump-addr', help='dump address lookup table', action='store_true')
args = parser.parse_args()
Expand All @@ -829,7 +833,7 @@ def load_adt(data):
adt_data = args.input.read_bytes()

adt = load_adt(adt_data)
print(adt.__str__(sort_keys=args.sort_keys))
print(adt.__str__(sort_keys=args.sort_keys, sort_nodes=args.sort_nodes))
new_data = adt.build()
if args.output is not None:
args.output.write_bytes(new_data)
Expand Down

0 comments on commit 282ac4f

Please sign in to comment.