-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_extractor.py
More file actions
55 lines (48 loc) · 2.46 KB
/
Copy pathcode_extractor.py
File metadata and controls
55 lines (48 loc) · 2.46 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
import ast
import os
import nbformat
from typing import List, Tuple, Dict, Any
from config import ANALYZABLE_EXTENSIONS
def extract_code_units(file_path):
"""Extract code units from various file types with enhanced parsing"""
units = []
if file_path.endswith(".py"):
with open(file_path, "r", encoding="utf-8", errors='ignore') as f:
code = f.read()
try:
tree = ast.parse(code)
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.ClassDef)):
start_line = node.lineno
end_line = node.end_lineno
chunk = "\n".join(code.splitlines()[start_line-1:end_line])
metadata = {"file": file_path, "name": node.name, "type": type(node).__name__, "lines": end_line - start_line + 1}
units.append((chunk, metadata))
except SyntaxError:
metadata = {"file": file_path, "name": "unknown", "type": "file", "lines": len(code.splitlines())}
units.append((code, metadata))
elif file_path.endswith(".ipynb"):
with open(file_path, "r", encoding="utf-8", errors='ignore') as f:
notebook = nbformat.read(f, as_version=4)
for cell in notebook.cells:
if cell.cell_type == "code":
code = cell.source
try:
tree = ast.parse(code)
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.ClassDef)):
chunk = code
metadata = {"file": file_path, "name": node.name, "type": type(node).__name__, "cell_index": notebook.cells.index(cell), "lines": len(code.splitlines())}
units.append((chunk, metadata))
except SyntaxError:
metadata = {"file": file_path, "name": "unknown", "type": "cell", "cell_index": notebook.cells.index(cell), "lines": len(code.splitlines())}
units.append((code, metadata))
elif file_path.endswith(ANALYZABLE_EXTENSIONS):
try:
with open(file_path, "r", encoding="utf-8", errors='ignore') as f:
code = f.read()
metadata = {"file": file_path, "name": os.path.basename(file_path), "type": "file", "lines": len(code.splitlines())}
units.append((code, metadata))
except Exception:
pass
return units