"""Util functions for theatrical ETL.""" from datetime import datetime import json import boto3 from flows.theatrical import config DATE_FORMAT = '%Y-%m-%d' def serialize_date(date): """Convert date to str in YYYY-MM-DD format. Args: date (date): Date. Returns: str: YYYY-MM-DD formatted date. """ return date.strftime(DATE_FORMAT) def deserialize_date(string): """Convert str in YYYY-MM-DD format to date. Args: string (str): String representation of date. Returns: date: Date extracted from given string. """ return datetime.strptime(string, DATE_FORMAT).date() def send_success_notification(correlation_id, upcs): """Send SNS message after ETL completes. Args: correlation_id (str): pre-chained correlation id. upcs (list): upcs in the ETL. Returns: dict: with MessageId string. """ payload = { 'action': config.SNS_ACTION_SUCCESS, 'correlation_id': correlation_id, 'source': config.SNS_SOURCE, 'upcs': upcs} sns = boto3.client('sns', region_name=config.SNS_REGION_NAME) return sns.publish( TopicArn=config.SNS_TOPIC_ARN, Message=json.dumps(payload), Subject=payload['action'])