8
8
from .tree_node_data import TreeNodeData
9
9
10
10
11
- def win_tree (data : InputData ) -> Generator [TreeNodeData , None , None ]:
11
+ def win_tree (
12
+ data : InputData ,
13
+ path : Path = Path ('./' ),
14
+ ) -> Generator [TreeNodeData , None , None ]:
12
15
"""Generate Tree Node Data for all files and directories in the given path.
13
16
14
17
Parameters:
18
+ - data (InputData): The
15
19
- path (str): The root path to run tree in.
16
20
17
21
Return:
18
22
Generator[TreeNodeData]
19
23
"""
20
- try :
21
- tree_root = Path ('./' )
22
- except :
23
- exit ('Failed to initiate Path' )
24
- return _gen_tree (data , tree_root )
24
+ if data .directories_only :
25
+ if data .prune_dirs :
26
+ yield from _dirs_only_prune (data , path )
27
+ else :
28
+ yield from _dirs_only (data , path )
29
+ else :
30
+ yield from _gen_tree (data , path )
25
31
26
32
27
33
def _gen_tree (
@@ -36,13 +42,45 @@ def _gen_tree(
36
42
if not data .include_hidden and entry .name .startswith ('.' ):
37
43
continue
38
44
is_directory = entry .is_dir ()
39
- if data .directories_only :
40
- if is_directory :
41
- # Check if prune_dirs is True and directory is empty
42
- if not data .prune_dirs or any (_gen_tree (data , entry , depth + 1 )):
43
- yield TreeNodeData (depth , is_directory , entry .name )
44
- else :
45
+ yield TreeNodeData (depth , is_directory , entry .name )
46
+ if is_directory :
47
+ yield from _gen_tree (data , entry , depth + 1 )
48
+
49
+
50
+ def _dirs_only (
51
+ data : InputData ,
52
+ path : Path ,
53
+ depth : int = 0
54
+ ) -> Generator [TreeNodeData , None , None ]:
55
+ """
56
+ """
57
+ for entry in path .iterdir ():
58
+ if not data .include_hidden and entry .name .startswith ('.' ):
59
+ continue
60
+ if (is_directory := entry .is_dir ()):
45
61
yield TreeNodeData (depth , is_directory , entry .name )
46
- if is_directory :
47
- yield from _gen_tree (data , entry , depth + 1 )
62
+ yield from _dirs_only (data , entry , depth + 1 )
63
+ else :
64
+ #print(f"Ignoring File {entry.name}")
65
+ pass
66
+
67
+
68
+ def _dirs_only_prune (
69
+ data : InputData ,
70
+ path : Path ,
71
+ depth : int = 0
72
+ ) -> Generator [TreeNodeData , None , None ]:
73
+ """
74
+ """
75
+ for entry in path .iterdir ():
76
+ if not data .include_hidden and entry .name .startswith ('.' ):
77
+ continue
78
+ if (is_directory := entry .is_dir ()):
79
+ # Check if directory is empty
80
+ if any (_dirs_only_prune (data , entry , depth + 1 )):
81
+ yield TreeNodeData (depth , is_directory , entry .name )
82
+ yield from _dirs_only_prune (data , entry , depth + 1 )
83
+ else :
84
+ #print(f"Ignoring File {entry.name}")
85
+ pass
48
86
0 commit comments