"""Helper functions.""" from garcon_contrib.dynamo_feed_status import garcon_feed_status def handle_download_error(status, err): """Handle download error. Returns `{'stop': True}` if file is not available, reraises an exception otherwise. Args: status (int): Error status. err (Exception): Error. Returns: dict: stop response """ if status == 404: return {'stop': True} else: raise err def handle_s3_download_error(err): """Handle S3 download error. Returns `{'stop': True}` if file is not available, reraises an exception otherwise. Args: err (S3ResponseError): Error. Returns: dict: stop response """ return handle_download_error(err.status, err) def handle_http_download_error(err): """Handle S3 download error. Returns `{'stop': True}` if file is not available, reraises an exception otherwise. Args: err (HTTPError): Error. Returns: dict: stop response """ return handle_download_error(err.response.status_code, err) def get_completed_tasks(feed_name, datestamp): """Return a list of completed tasks for execution lookup params. This does not have different behavior if the task does not exist in DynamoDB. Consider a non existing task execution as simply not having any completed tasks instead of an exception. Args: feed_name (str): Feed name of workflow execution for status updates. datestamp (str): Workflow generated date. Returns: list: List of task names, empty list if no tasks are completed. """ status_item = garcon_feed_status._get_item(feed_name, datestamp) if not status_item: return [] # this is called with _status at the end because the # garcon_feed_status.set_status function appends it automatically. tasks = status_item.get('completed_tasks_status', '').split(',') return tasks if any(tasks) else [] def is_completed_task(feed_name, datestamp, task_name): """Check DynamoDB for task completion status. Args: feed_name (str): Feed name of workflow execution for status updates. datestamp (str): Workflow generated date. task_name (str): Name of task. Returns: bool: True if task_name is found in marker attribute of DynamoDB item. """ return task_name in get_completed_tasks(feed_name, datestamp) def mark_completed_task(feed_name, datestamp, task_name): """Update DynamoDB item with task completion status note. Args: feed_name (str): Feed name of workflow execution for status updates. datestamp (str): Workflow generated date. task_name (str): Name of task. """ completed_tasks = get_completed_tasks(feed_name, datestamp) if task_name not in completed_tasks: completed_tasks.append(task_name) garcon_feed_status.set_status( feed_name, datestamp, 'completed_tasks', ','.join(completed_tasks))