informations about subtree

This commit is contained in:
Amber 2024-01-26 12:36:01 +01:00
parent aec7377780
commit bd2eef0cf7
4 changed files with 40 additions and 0 deletions

Binary file not shown.

Binary file not shown.

5
src/pretty_print.py Normal file
View File

@ -0,0 +1,5 @@
import json
def pp(json_obj, indent=2):
dumps = json.dumps(json_obj, indent=2)
print(dumps)

View File

@ -119,6 +119,41 @@ class NerdTree():
return d
def compute_aggregate_recursively(self, node=None):
'''
node is the rootnode of the subtree
at the moment compute size and number of items of subtree starting from node
'''
subtree = {
'subtree_size' : 0,
'subtree_items' : 0,
}
node = node or self.json_tree
for item in node.get('children') or []:
if item['type'] == 'd':
if item.get('subtree_size') is None:
subtree_compute = self.compute_aggregate_recursively(item)
else:
subtree_compute = {
'subtree_size' : item['subtree_size'],
'subtree_items' : item['subtree_items'],
}
## if you want count the current directory as item you must sum 1
## if you want count the current directory size you must include item['size']
subtree['subtree_size'] += subtree_compute['subtree_size'] + item['size']
subtree['subtree_items'] += subtree_compute['subtree_items'] + 1
else:
subtree['subtree_size'] += item['size']
subtree['subtree_items'] += 1
# return subtree
node.update(subtree)
return subtree
def tree_from_struct(self, rootnode, prec_seps=[]):
'''
recursively produce a string for representing the tree