"""Lambda function module.""" import config from src.constants import general from src import audio_validator from src.common import lambda_exceptions from src.common import s3 from src.common import status from src.common import util from src.common.constants import errors def _log_error(err, filename, bucket): status.send_general_status( function=config.LAMBDA_NAME, status_name=general.AUDIO_VALIDATION_ERROR_STATUS, filename=filename, bucket=bucket, errors=err ) def handler(event, context): """Lambda entry point.""" try: bucket, key, asset_config, file_type = util.extract_event(event) input_asset_url = s3.create_presigned_url(bucket, key) audio_validator.validate_audio_asset(input_asset_url) status.send_general_status( function=config.LAMBDA_NAME, status_name=general.AUDIO_VALIDATION_COMPLETE_STATUS, filename=key, bucket=bucket ) return { 'bucket': bucket, 'key': key, 'file_type': file_type, 'config': asset_config } except lambda_exceptions.LambdaStatusError as error: config.logger.exception(str(error)) # if this is a malformed event but we have the key, we can send the error to the microserivce. # if the key is missing, then we cant post the status if 'key' in event: _log_error(error.errors, event.get('key'), event.get('bucket', None)) raise except lambda_exceptions.LambdaError as error: config.logger.exception(str(error)) raise except Exception as error: config.logger.exception(str(error)) e = dict() e[errors.AUDIO_VALIDATION_ERROR_CODE] = errors.ERROR_MESSAGES[errors.AUDIO_VALIDATION_ERROR_CODE].format( message=str(error)) _log_error(e, key, bucket) raise