File size: 885 Bytes
7c58cfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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))