"""Data transformation functions.""" from availability_etl import constants def transformed(dicts): """Generator of transformed data rows. Args: dicts (iterable): An iterable of data rows as dictionaries. Yields: dict: A dictionary with transformed row data. Raises: StopIteration: There are no more items to yield. """ transformation_functions = [ store_internal_id_to_storefront_url, ] for row in dicts: for transformation_function in transformation_functions: row = transformation_function(row) yield (row) def store_internal_id_to_storefront_url(row_dict): """Transform store_internal_id to store-specific storefront_url. Args: row_dict (dict): Data row as a dictionary. Returns: dict: transformed row_dict. """ store_internal_id = row_dict[constants.COLUMN_STORE_INTERNAL_ID] store_id = row_dict[constants.COLUMN_STORE_ID] storefront_url_pattern = constants.STOREFRONT_URLS_TEMPLATES[store_id] if store_internal_id: storefront_url = storefront_url_pattern.format(store_internal_id) else: storefront_url = constants.CSV_EMPTY_STR del row_dict[constants.COLUMN_STORE_INTERNAL_ID] row_dict[constants.COLUMN_STOREFRONT_URL] = storefront_url return row_dict