-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir_tree.py
137 lines (117 loc) · 3.92 KB
/
dir_tree.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import argparse
import json
from pathlib import Path
from typing import List, Optional
DEFAULT_EXCLUSIONS = [
"ell*/",
"**/repos/**",
"examples/*",
"tests/*",
".venv/*",
"__pycache__/*",
"*.pyc",
".git/*",
".idea/*",
".vscode/*",
"node_modules/*",
"build/*",
"dist/*",
"*.egg-info/*",
".pytest_cache/*",
".coverage",
"htmlcov/*",
]
def load_config_exclusions() -> List[str]:
"""Load exclusion patterns from .tree.config file if it exists."""
config_file = Path('.tree.config')
config_exists = config_file.exists()
exclusions = []
if config_exists:
try:
with open(config_file, 'r') as f:
config = json.load(f)
exclusions = config.get('exclude', [])
except (json.JSONDecodeError, IOError):
print(f"Warning: Could not parse {config_file}")
return exclusions
def generate_tree(
directory: Path,
exclusions: List[str] = None,
indent: str = " ",
prefix: str = "",
) -> str:
"""Generate a directory tree string."""
# Initialize control flags and state
has_permission = True
exclusions = exclusions or []
output = []
items = []
# Get directory contents
try:
items = sorted(directory.iterdir(), key=lambda x: (not x.is_dir(), x.name))
except PermissionError:
has_permission = False
# Handle permission denied case
if not has_permission:
return f"{prefix}[Permission Denied]\n"
# Process each item
for i, item in enumerate(items):
# Control flags for current item
is_last = i == len(items) - 1
should_exclude = any(item.match(pattern) for pattern in exclusions)
is_directory = item.is_dir()
# Skip excluded items
if should_exclude:
continue
# Prepare formatting
node_prefix = "└── " if is_last else "├── "
next_prefix = prefix + (" " if is_last else "│ ")
# Handle directories
if is_directory:
output.append(f"{prefix}{node_prefix}{item.name}/")
subtree = generate_tree(item, exclusions, indent, next_prefix)
output.append(subtree)
continue
# Handle files
try:
output.append(f"{prefix}{node_prefix}{item.name}")
except (UnicodeDecodeError, PermissionError):
output.append(f"{prefix}{node_prefix}{item.name} [Binary or inaccessible]")
return "\n".join(output)
def main():
# Parse arguments
parser = argparse.ArgumentParser(
description="Generate a directory tree with optional file content concatenation"
)
parser.add_argument("directory", help="The directory to process")
parser.add_argument("-e", "--exclude", action="append", default=[],
help="Exclusion patterns (can be used multiple times)")
parser.add_argument("--output", choices=["text", "json"], default="text",
help="Output format (text or json)")
args = parser.parse_args()
# Initialize control flags and state
directory = Path(args.directory)
directory_exists = directory.exists()
output_format = args.output
# Validate directory
if not directory_exists:
print(f"Error: Directory '{directory}' does not exist")
return
# Prepare exclusions
config_exclusions = load_config_exclusions()
exclusions = DEFAULT_EXCLUSIONS + config_exclusions + args.exclude
# Generate tree
tree = generate_tree(directory, exclusions=exclusions)
# Output results
if output_format == "json":
result = {
"directory": str(directory),
"tree": tree.split("\n"),
"exclusions": exclusions
}
print(json.dumps(result, indent=2))
else:
with open("tree.txt", "w", encoding="utf-8") as f:
f.write(tree)
if __name__ == "__main__":
main()