"""Report I/O functions.""" from io import BytesIO from pathlib import Path from pptx import presentation from ....constants import FileExtensions def save_to_disk( *, as_pdf: bool, buffer: BytesIO, output_path: str, source: presentation.Presentation, ) -> None: """Save the report to disk, either as PPTX or PDF. Args: as_pdf: Whether to save the report as PDF. If False, the report will be saved as PPTX. buffer: BytesIO object containing the report data. output_path: Path to save the report to. source: PPTX Presentation object. """ output_path = output_path or "" file_extension = Path(output_path).suffix.lower() if as_pdf: if file_extension != FileExtensions.PDF: raise ValueError("Output path must end with `.pdf` when `as_pdf` is True.") with open(output_path, "wb") as file: file.write(buffer.read()) buffer.seek(0) else: if file_extension != FileExtensions.PPTX: raise ValueError( "Output path must end with `.pptx` when `as_pdf` is False." ) source.save(output_path)