1
1
import logging
2
2
from pathlib import Path
3
3
4
- from repo_context .ignore import EXTENSIONS , FILES , PATTERNS
5
4
from repo_context .utils import should_ignore
6
5
7
6
logger = logging .getLogger ("repo_context.structure" )
10
9
class RepoStructure :
11
10
def __init__ (self , ignore_patterns : list [str ] | None = None ) -> None :
12
11
self .ignore_patterns = ignore_patterns or []
13
- self .ignore_patterns += FILES + EXTENSIONS + PATTERNS
14
12
15
13
def generate_tree (
16
14
self ,
17
15
directory : Path ,
18
16
prefix : str = "" ,
19
17
is_last : bool = True ,
20
- ignore_patterns : list [str ] | None = None ,
21
18
) -> list [str ]:
22
19
"""
23
20
Recursively generate a tree structure of the directory.
24
21
25
22
Args:
26
- directory: Path object pointing to the directory
27
- prefix: Prefix for the current line (used for recursion)
28
- is_last: Boolean indicating if this is the last item in current directory
29
- ignore_patterns: List of patterns to ignore
23
+ directory (Path) : Path object pointing to the directory
24
+ prefix (str) : Prefix for the current line (used for recursion). default: ""
25
+ is_last (bool) : Boolean indicating if this is the last item in current directory. default: True
26
+ ignore_patterns (list[str] | None) : List of patterns to ignore. default: None
30
27
31
28
Returns:
32
- List [str]: Lines of the tree structure
29
+ list [str]: Lines of the tree structure
33
30
"""
34
- if ignore_patterns is None :
35
- ignore_patterns = []
36
-
37
31
if not directory .is_dir ():
38
32
logger .error (f"'{ directory } ' is not a valid directory" )
39
33
return []
@@ -42,65 +36,47 @@ def generate_tree(
42
36
items = [
43
37
item
44
38
for item in sorted (directory .iterdir ())
45
- if not should_ignore (item .name , ignore_patterns )
39
+ if not should_ignore (item .name , self . ignore_patterns )
46
40
]
47
41
48
42
for i , item in enumerate (items ):
49
43
is_last_item = i == len (items ) - 1
50
- connector = "??? " if is_last_item else "??? "
44
+ connector = "└── " if is_last_item else "├── "
51
45
52
46
tree_lines .append (f"{ prefix } { connector } { item .name } " )
53
47
54
48
if item .is_dir ():
55
- extension = " " if is_last_item else "? "
49
+ extension = " " if is_last_item else "│ "
56
50
tree_lines .extend (
57
51
self .generate_tree (
58
52
item ,
59
53
prefix + extension ,
60
54
is_last_item ,
61
- ignore_patterns ,
62
55
)
63
56
)
64
57
65
58
return tree_lines
66
59
67
- def create_tree_structure (
68
- self ,
69
- path : str ,
70
- output_file : str | None = None ,
71
- ignore_patterns : list [str ] | None = None ,
72
- ) -> None :
60
+ def create_tree_structure (self , path : str ) -> str :
73
61
"""
74
62
Create and display/save a tree structure of the specified directory.
75
63
76
64
Args:
77
65
path: Path to the directory
78
- output_file: Optional file path to save the tree structure
79
- ignore_patterns: List of patterns to ignore
66
+
67
+ Returns:
68
+ str: The tree structure
80
69
"""
81
70
directory = Path (path )
82
71
if not directory .exists ():
83
- logger .error (f"Directory '{ path } ' does not exist" )
84
- return
72
+ raise FileNotFoundError (f"Directory '{ path } ' does not exist" )
85
73
86
74
logger .info (f"Generating tree structure for: { directory .absolute ()} " )
87
75
88
- tree_lines = ["Directory Structure:" , directory .name ]
89
- tree_lines .extend (
90
- self .generate_tree (directory , ignore_patterns = ignore_patterns or [])
91
- )
76
+ tree_lines = ["# Directory Structure" , directory .name ]
77
+ tree_lines .extend (self .generate_tree (directory ))
92
78
93
79
# Join lines with newlines
94
- tree_structure = "\n " .join (tree_lines )
95
-
96
- # Print to console
97
- logger .info (tree_structure )
98
-
99
- # Save to file if specified
100
- if output_file :
101
- output_path = Path (output_file )
102
- try :
103
- output_path .write_text (tree_structure )
104
- logger .info (f"Tree structure saved to: { output_path .absolute ()} " )
105
- except Exception as e :
106
- logger .error (f"Failed to save tree structure: { e } " )
80
+ tree_structure = "\n " .join (tree_lines ) + "\n "
81
+
82
+ return tree_structure
0 commit comments