Repository navigation
Expand file tree
/
Copy pathfolder_tree.py
More file actions
110 lines (93 loc) · 3.27 KB
/
Copy pathfolder_tree.py
File metadata and controls
110 lines (93 loc) · 3.27 KB
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
#!/usr/bin/env python3
from pathlib import Path
from typing import List, Union
from rich.tree import Tree
from rich.console import Console
from rich import print
from rich.panel import Panel
from rich.style import Style
def create_tree(path: Path, tree: Tree) -> None:
"""Recursively build a Tree object representing the directory structure."""
try:
# Sort directories first, then files, but only include .py files
paths = sorted(
path.iterdir(),
key=lambda x: (not x.is_dir(), x.name.lower()),
)
for item in paths:
# Skip hidden files and directories
if item.name.startswith("."):
continue
# Only process directories and .py files
if item.is_dir():
# Check if directory contains any .py files
has_py_files = any(
f.suffix == ".py" for f in item.rglob("*.py")
)
if has_py_files:
style = Style(color="red", bold=True)
branch = tree.add(f"📁 {item.name}", style=style)
create_tree(item, branch)
elif item.suffix == ".py":
style = Style(color="bright_red")
tree.add(f"🐍 {item.name}", style=style)
except PermissionError:
tree.add("⚠️ Permission denied", style="red")
def get_file_icon(filename: str) -> str:
"""Return an appropriate emoji icon based on file extension."""
ext = filename.lower().split(".")[-1] if "." in filename else ""
icons = {
"py": "🐍",
"js": "📜",
"json": "📋",
"md": "📝",
"txt": "📄",
"yml": "⚙️",
"yaml": "⚙️",
"html": "🌐",
"css": "🎨",
"png": "🖼️",
"jpg": "🖼️",
"jpeg": "🖼️",
"gif": "🖼️",
"pdf": "📚",
"zip": "📦",
"gz": "📦",
"tar": "📦",
}
return icons.get(ext, "📄")
def display_folder_tree(folders: Union[str, List[str]]) -> None:
"""
Display a tree structure for one or more folders, showing only Python files.
Args:
folders: Either a single folder path as string or a list of folder paths
"""
console = Console()
# Convert single string input to list
if isinstance(folders, str):
folders = [folders]
for folder in folders:
path = Path(folder)
if not path.exists():
print(f"[red]❌ Path not found: {folder}[/red]")
continue
tree = Tree(
f"[bold red]📂 {path.absolute()}[/bold red]",
guide_style="dim black",
)
create_tree(path, tree)
# Create a panel around each tree for better visualization
panel = Panel(
tree,
title=f"[bold red]Python Files: {folder}[/bold red]",
border_style="red",
padding=(1, 2),
)
console.print(panel)
console.print() # Add a blank line between folders
# Example usage
if __name__ == "__main__":
# Example of using the function with a single folder
display_folder_tree("examples")
# Example of using the function with multiple folders
# display_folder_tree(["examples", "tests", "docs"])