"""Validate headers and content of adjustment file.""" import numpy as np import pandas as pd from lib import config from lib import constants from lib.utils import aws from lib.utils import ows from tasks.adjustment_file_upload import helpers def validate_format_task(dag_run, **kwargs): """Task to validate headers and contents.""" event = helpers.get_event_from_params(dag_run, **kwargs) statement_period_adjustment_file_id = event.target_id adjustment_file_details = ows.get_statement_period_adjustment_file_details( statement_period_adjustment_file_id ) adjustment_file_location = adjustment_file_details.get('valid_file_location') assert adjustment_file_location is not None, 'Missing adjustment file' s3_object = aws.split_path(adjustment_file_location) bucket = s3_object.bucket key = s3_object.key account_id = config.S3_ACCOUNT_ID file = aws.get_file(account_id, bucket, key) df = pd.read_excel(file, engine='openpyxl', na_filter=False) if len(df.columns) < (constants.DAG_ADJUSTMENT_FILE_REQUIRED_HEADERS): ows.update_adjustment_file(statement_period_adjustment_file_id, body=dict( error_type='format_error' )) raise Exception('Missing columns') required_headers = df.columns[:constants.DAG_ADJUSTMENT_FILE_REQUIRED_HEADERS] if not set(required_headers) == (constants.DAG_ADJUSTMENT_FILE_HEADERS): ows.update_adjustment_file(statement_period_adjustment_file_id, body=dict( error_type='format_error' )) raise Exception('Mismatched or missing column names') df = df.astype(str).replace(r'^\s*$', np.nan, regex=True) df = df.dropna(how='all') df = df.astype(object).where(pd.notnull(df), None) if df.empty: ows.update_adjustment_file(statement_period_adjustment_file_id, body=dict( error_type='format_error' )) raise Exception('There are no adjustment records in the file') if df.shape[0] > constants.DAG_ADJUSTMENT_FILE_MAX_ROWS: ows.update_adjustment_file(statement_period_adjustment_file_id, body=dict( error_type='row_count_error' )) raise Exception('The file exceeds the maximum number of rows')