Terminal / tools /repo_reader.py
GitHub Action
sync: backend da GitHub 2026-05-18T11:59:53Z
e6fbc88
Raw
History Blame Contribute Delete
1.46 kB
"""repo_reader.py — Legge struttura e file di un repository locale."""
from pathlib import Path
SKIP={'.git','node_modules','__pycache__','.venv','dist','build'}
TEXT={'.py','.ts','.tsx','.js','.jsx','.html','.css','.md','.json','.yaml','.yml','.toml','.txt','.sh'}
def read_tree(root:str,max_files:int=80)->dict:
rp=Path(root)
if not rp.exists(): return {"error":f"Path non trovato: {root}","files":[]}
files=[]
for p in sorted(rp.rglob("*")):
if len(files)>=max_files: break
if any(s in p.parts for s in SKIP): continue
if p.is_file():
rel=str(p.relative_to(rp)); sz=p.stat().st_size
files.append({"path":rel,"size":sz,"type":"text" if p.suffix.lower() in TEXT else "binary","ext":p.suffix})
return {"root":root,"total_files":len(files),"files":files}
def read_file(root:str,filepath:str)->dict:
full=Path(root)/filepath
if not full.exists(): return {"error":f"Non trovato: {filepath}","content":""}
if full.stat().st_size>50000: return {"error":"File troppo grande","content":""}
if full.suffix.lower() not in TEXT and full.suffix!="": return {"error":"File binario","content":""}
try:
c=full.read_text(encoding="utf-8",errors="replace")
return {"path":filepath,"content":c,"lines":c.count("\n")+1,"size":len(c)}
except Exception as e: return {"error":str(e),"content":""}
def read_files_batch(root:str,paths:list[str])->list[dict]:
return [read_file(root,p) for p in paths[:10]]