"""Decode source text without silently replacing characters in localisations.""" import io import tokenize from pathlib import Path from app.converter.rpy_lexer import ParseError def read_source(path: Path) -> str: raw = path.read_bytes() try: encoding, _ = tokenize.detect_encoding(io.BytesIO(raw).readline) return raw.decode(encoding) except (UnicodeError, SyntaxError, LookupError) as exc: raise ParseError(f"Не удалось прочитать кодировку сценария: {exc}", path.name) from exc