File size: 1,461 Bytes
28a08e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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]]