# SPDX-License-Identifier: MIT """Reading images in place: a plain file or the first matching member of a .zip. Owner's decision (docs/handoff-brief.md): the BIOS and commercial ROMs are read in place, never copied into the repository; a No-Intro .zip is opened and its member read into memory. """ from __future__ import annotations import zipfile from pathlib import Path def read_image(path: str | Path, suffixes: tuple[str, ...] = ()) -> bytes: """The bytes of ``path``; for a .zip, the first member ending in one of ``suffixes`` (or the first file member when none is given).""" path = Path(path).expanduser() if path.suffix.lower() != ".zip": return path.read_bytes() with zipfile.ZipFile(path) as archive: names = [n for n in archive.namelist() if not n.endswith("/")] chosen = [n for n in names if n.lower().endswith(suffixes)] if suffixes else names if not chosen: raise ValueError(f"{path}: no member ending in {suffixes}") return archive.read(chosen[0])