"""Lambda update_template function module.""" from typing import Any from common.logic import template from common.schemas.catalog import Catalog from common.schemas.s3_reference import S3Reference from pydantic import ValidationError from src.connectors import s3 from src.logic import old_template def handle(s3_reference: S3Reference) -> S3Reference: """ The main handler function, executed by local dev as well as by Lambda function running inside Docker. Args: s3_event: S3Event an "s3_event" containing information about the key and bucket of the original spreadsheet Returns: S3: an "s3_event" containing information about the key and bucket of the updated spreadsheet """ try: file = s3.get_file(s3_reference, "/tmp/test.xls") catalog: Catalog = old_template.parse(file) file_name = f"{s3_reference.key.split('/')[-1].split('.')[0]}.xlsx" s3.get_file( S3Reference( key="template/bulk_upload_template.xlsx", bucket="qa-ows-product-staging", ), file_name, ) template.write(catalog, file_name) except Exception as e: raise e return s3_reference def local_handle() -> None: """ The local version of the main handler function. Reads from input.xls and writes to output.xlsx The output file must already exist and should be a copy of the bulk template. """ catalog: Catalog = old_template.parse("input.xls") template.write(catalog, "output.xlsx") def handler(event: dict[str, Any], context: Any) -> dict[str, Any]: """ Lambda entry point. Args: event: Lambda event payload (should look like event.shadow.json) context: Lambda context. Raises: ValidationError: If the event JSON does not conform to the S3Event schema. """ try: s3_reference = S3Reference(**event) if not s3.object_exists(s3_reference): return {"is_valid": False, "status": "NOT_FOUND"} return handle(s3_reference).model_dump() except ValidationError: return {"error": "oops!"}