llm_context_builder / reader.py
pourimoto's picture
Create reader.py
7c58cfc verified
Raw
History Blame Contribute Delete
885 Bytes
"""
reader.py
Safe file reading utilities
"""
from __future__ import annotations
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Iterable
from config import MAX_WORKERS
def read_file_safe(path: Path) -> str:
"""
Read text file using utf-8 then latin-1 fallback.
"""
try:
return path.read_text(encoding="utf-8")
except UnicodeDecodeError:
try:
return path.read_text(encoding="latin-1")
except Exception:
return ""
except Exception:
return ""
def load_files(paths: Iterable[Path]) -> list[tuple[Path, str]]:
"""
Read many files concurrently.
"""
paths = list(paths)
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
contents = list(executor.map(read_file_safe, paths))
return list(zip(paths, contents))