forked from itigges22/ATLAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_builder.py
More file actions
238 lines (198 loc) · 6.59 KB
/
Copy pathtree_builder.py
File metadata and controls
238 lines (198 loc) · 6.59 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""Build a unified navigation tree from filesystem + AST structure."""
import hashlib
import logging
import os
from typing import List, Dict, Optional
from models.tree_node import TreeNode, TreeIndex, NodeType, NodeMetadata
from indexer.ast_parser import parse_python_file, ASTNode
logger = logging.getLogger(__name__)
# File extensions we index
INDEXABLE_EXTENSIONS = {
".py", ".js", ".ts", ".tsx", ".jsx", ".go", ".rs",
".java", ".c", ".cpp", ".h", ".hpp", ".rb",
}
# Directories to skip
SKIP_DIRS = {
"__pycache__", ".git", ".svn", "node_modules", ".venv", "venv",
".tox", ".mypy_cache", ".pytest_cache", "dist", "build",
".eggs", "*.egg-info",
}
# Files to skip
SKIP_FILES = {
".gitignore", ".dockerignore", "LICENSE", "Makefile",
}
def build_tree_from_files(
project_id: str,
files: List[Dict[str, str]],
project_name: str = "",
) -> TreeIndex:
"""
Build a unified navigation tree from a list of project files.
Args:
project_id: Unique project identifier
files: List of {"path": str, "content": str}
project_name: Human-readable project name
Returns:
TreeIndex with complete tree structure
"""
# Root node
root = TreeNode(
node_id=_node_id(project_id, "/"),
node_type=NodeType.REPOSITORY,
name=project_name or project_id,
path="/",
metadata=NodeMetadata(
line_count=sum(len(f["content"].split("\n")) for f in files),
),
)
file_hashes: Dict[str, str] = {}
# Group files by directory
dir_map: Dict[str, List[Dict[str, str]]] = {}
for f in files:
path = f["path"]
dir_path = os.path.dirname(path) or "/"
if dir_path not in dir_map:
dir_map[dir_path] = []
dir_map[dir_path].append(f)
file_hashes[path] = hashlib.sha256(f["content"].encode()).hexdigest()
# Build directory tree structure
dir_nodes: Dict[str, TreeNode] = {"/": root}
# Sort directories to ensure parents are created before children
all_dirs = sorted(dir_map.keys())
for dir_path in all_dirs:
_ensure_dir_node(dir_path, dir_nodes, project_id)
# Process each file
for f in files:
path = f["path"]
content = f["content"]
dir_path = os.path.dirname(path) or "/"
ext = os.path.splitext(path)[1]
language = _detect_language(ext)
# Create file node
file_node = TreeNode(
node_id=_node_id(project_id, path),
node_type=NodeType.FILE,
name=os.path.basename(path),
path=path,
metadata=NodeMetadata(
line_count=len(content.split("\n")),
language=language,
file_hash=file_hashes.get(path),
),
content=content,
)
# Parse AST for supported languages
if ext == ".py":
ast_nodes = parse_python_file(content, path)
_attach_ast_children(file_node, ast_nodes, project_id, path, content)
# TODO: Add JS/TS parsing when tree-sitter-javascript is added
# Count imports for metadata
if ext == ".py":
import_count = sum(
1 for line in content.split("\n")
if line.strip().startswith(("import ", "from "))
)
file_node.metadata.import_count = import_count
# Add to parent directory
parent = dir_nodes.get(dir_path, root)
parent.children.append(file_node)
return TreeIndex(
project_id=project_id,
root=root,
file_hashes=file_hashes,
)
def _ensure_dir_node(
dir_path: str,
dir_nodes: Dict[str, TreeNode],
project_id: str,
) -> TreeNode:
"""Ensure a directory node exists, creating parent directories as needed."""
if dir_path in dir_nodes:
return dir_nodes[dir_path]
# Create parent first
parent_path = os.path.dirname(dir_path) or "/"
if parent_path == dir_path:
parent_path = "/"
parent = _ensure_dir_node(parent_path, dir_nodes, project_id)
# Create this directory node
node = TreeNode(
node_id=_node_id(project_id, dir_path),
node_type=NodeType.DIRECTORY,
name=os.path.basename(dir_path) or dir_path,
path=dir_path,
)
dir_nodes[dir_path] = node
parent.children.append(node)
return node
def _attach_ast_children(
file_node: TreeNode,
ast_nodes: List[ASTNode],
project_id: str,
file_path: str,
full_content: str,
):
"""Convert AST nodes to TreeNode children and attach to file node."""
for ast_node in ast_nodes:
# Skip imports — they're captured in metadata
if ast_node.node_type == "import":
continue
child = _ast_to_tree_node(ast_node, project_id, file_path, full_content)
if child:
file_node.children.append(child)
# If file has no AST children (or only imports), keep the file as a leaf
# with its full content available
def _ast_to_tree_node(
ast_node: ASTNode,
project_id: str,
file_path: str,
full_content: str,
) -> Optional[TreeNode]:
"""Convert an ASTNode to a TreeNode."""
node_type_map = {
"class": NodeType.CLASS,
"function": NodeType.FUNCTION,
"block": NodeType.BLOCK,
}
nt = node_type_map.get(ast_node.node_type)
if nt is None:
return None
ast_path = f"{file_path}::{ast_node.name}"
node = TreeNode(
node_id=_node_id(project_id, ast_path),
node_type=nt,
name=ast_node.name,
path=ast_path,
metadata=NodeMetadata(
line_count=ast_node.end_line - ast_node.start_line + 1,
start_line=ast_node.start_line,
end_line=ast_node.end_line,
),
content=ast_node.content,
)
# Add method children for classes
for child_ast in ast_node.children:
child = _ast_to_tree_node(child_ast, project_id, file_path, full_content)
if child:
node.children.append(child)
return node
def _node_id(project_id: str, path: str) -> str:
"""Generate a deterministic node ID."""
return hashlib.sha256(f"{project_id}:{path}".encode()).hexdigest()[:16]
def _detect_language(ext: str) -> str:
"""Detect language from file extension."""
lang_map = {
".py": "python",
".js": "javascript",
".ts": "typescript",
".tsx": "typescript",
".jsx": "javascript",
".go": "go",
".rs": "rust",
".java": "java",
".c": "c",
".cpp": "cpp",
".h": "c",
".hpp": "cpp",
".rb": "ruby",
}
return lang_map.get(ext, "unknown")