42 lines
1002 B
Python
42 lines
1002 B
Python
from anytree import LevelOrderGroupIter
|
|
from anytree.importer import JsonImporter
|
|
from sys import argv
|
|
|
|
|
|
def build_rows_depth_first(node, rows, leaf_count=0):
|
|
|
|
for child in sorted(node.children, key=lambda x: x.size, reverse=True):
|
|
leaf_count = build_rows_depth_first(child, rows, leaf_count)
|
|
|
|
if node.is_leaf:
|
|
cur_row = leaf_count
|
|
rows[cur_row].append(f'<td>{node.name}</td>')
|
|
leaf_count += 1
|
|
else:
|
|
child_leaves = len(node.leaves)
|
|
cur_row = leaf_count - child_leaves
|
|
rows[cur_row].append(f'<td rowspan="{child_leaves}">{node.name}</td>')
|
|
|
|
return leaf_count
|
|
|
|
|
|
if len(argv) < 2:
|
|
print(f"usage: {argv[0]} <json file>")
|
|
exit()
|
|
|
|
with open(argv[1], 'r') as f:
|
|
root = JsonImporter().read(f)
|
|
|
|
output_rows = []
|
|
|
|
for i in range(len(root.leaves)):
|
|
output_rows.append([])
|
|
|
|
build_rows_depth_first(root, output_rows)
|
|
|
|
for row in output_rows:
|
|
print("<tr>")
|
|
for cell in row:
|
|
print(f" {cell}")
|
|
print("</tr>")
|