Skip to content

Commit 1274dbd

Browse files
committed
Treescriptify Package:
*u windows_tree.py - add method _dirs_only, add method _dirs_only_prune, modify method win_tree to accept a path parameter
1 parent 016d414 commit 1274dbd

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

treescriptify/windows_tree.py

+52-14
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@
88
from .tree_node_data import TreeNodeData
99

1010

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]:
1215
"""Generate Tree Node Data for all files and directories in the given path.
1316
1417
Parameters:
18+
- data (InputData): The
1519
- path (str): The root path to run tree in.
1620
1721
Return:
1822
Generator[TreeNodeData]
1923
"""
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)
2531

2632

2733
def _gen_tree(
@@ -36,13 +42,45 @@ def _gen_tree(
3642
if not data.include_hidden and entry.name.startswith('.'):
3743
continue
3844
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()):
4561
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
4886

0 commit comments

Comments
 (0)