diff --git a/src/__pycache__/pretty_print.cpython-39.pyc b/src/__pycache__/pretty_print.cpython-39.pyc new file mode 100644 index 0000000..8d1da4a Binary files /dev/null and b/src/__pycache__/pretty_print.cpython-39.pyc differ diff --git a/src/__pycache__/tree.cpython-39.pyc b/src/__pycache__/tree.cpython-39.pyc index 2aa560a..1294641 100644 Binary files a/src/__pycache__/tree.cpython-39.pyc and b/src/__pycache__/tree.cpython-39.pyc differ diff --git a/src/pretty_print.py b/src/pretty_print.py new file mode 100644 index 0000000..778708d --- /dev/null +++ b/src/pretty_print.py @@ -0,0 +1,5 @@ +import json + +def pp(json_obj, indent=2): + dumps = json.dumps(json_obj, indent=2) + print(dumps) diff --git a/src/tree.py b/src/tree.py index 33f49a0..51bde54 100644 --- a/src/tree.py +++ b/src/tree.py @@ -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