-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_generator.py
76 lines (64 loc) · 3.04 KB
/
tree_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import fnmatch
def is_excluded(path, directory, exclude_dirs, exclude_files):
rel_path = os.path.relpath(path, directory)
for exclude_dir in exclude_dirs:
if rel_path.startswith(exclude_dir) or f"/{exclude_dir}/" in f"/{rel_path}/":
return True
for exclude_file in exclude_files:
if fnmatch.fnmatch(rel_path, exclude_file):
return True
return False
def build_tree_from_included_files(directory, include_files):
tree = {}
for include_file in include_files:
parts = include_file.split(os.sep)
current_level = tree
for part in parts:
if part not in current_level:
current_level[part] = {}
current_level = current_level[part]
return tree
def print_tree_from_structure(structure, prefix=''):
lines = []
items = list(structure.items())
for index, (name, subtree) in enumerate(items):
connector = '└── ' if index == len(items) - 1 else '├── '
lines.append(prefix + connector + name)
if subtree:
extension = ' ' if index == len(items) - 1 else '│ '
lines.extend(print_tree_from_structure(subtree, prefix + extension))
return lines
def print_tree_included(directory, include_files):
tree_structure = build_tree_from_included_files(directory, include_files)
return '\n'.join(print_tree_from_structure(tree_structure))
def print_tree(directory, prefix='', exclude_dirs=[], exclude_files=[]):
tree_output = ""
elements = os.listdir(directory)
elements.sort(key=lambda x: (not os.path.isdir(os.path.join(directory, x)), x.lower()))
filtered_elements = [e for e in elements if not is_excluded(os.path.join(directory, e), directory, exclude_dirs, exclude_files)]
for index, element in enumerate(filtered_elements):
full_path = os.path.join(directory, element)
is_last = index == len(filtered_elements) - 1
prefix_element = '└── ' if is_last else '├── '
tree_output += prefix + prefix_element + element + '\n'
if os.path.isdir(full_path):
secondary_prefix = ' ' if is_last else '│ '
tree_output += print_tree(full_path, prefix + secondary_prefix, exclude_dirs, exclude_files)
return tree_output
def build_tree_structure(directory, exclude_dirs=[], exclude_files=[]):
tree = []
for item in os.listdir(directory):
full_path = os.path.join(directory, item)
if os.path.isdir(full_path) and not is_excluded(full_path, directory, exclude_dirs, exclude_files):
tree.append({
"type": "folder",
"name": item,
"children": build_tree_structure(full_path, exclude_dirs, exclude_files)
})
elif os.path.isfile(full_path) and not is_excluded(full_path, directory, exclude_dirs, exclude_files):
tree.append({
"type": "file",
"name": item
})
return tree