import argparse
from datetime import datetime, timedelta
def mirror_dates(source_data, staging_data, date_field, days_back=7, dry_run=False):
"""Mirrors date values from source to staging data."""
try:
source_dates = source_data[date_field]
staging_dates = staging_data[date_field]
except KeyError as e:
raise ValueError(f"Date field '{date_field}' not found in source or staging data.")
cutoff_date = datetime.now() - timedelta(days=days_back)
for i in range(len(source_dates)):
source_date = source_dates[i]
# Only mirror if the source date is within the cutoff
if source_date >= cutoff_date:
try:
staging_dates[i] = source_date # Mirror the date
except TypeError:
print(f"Warning: Could not mirror date. Type mismatch. Source: {source_date}, Staging: {staging_dates[i]}. Skipping.")
continue #skip to next iteration
if not dry_run:
staging_data[date_field] = source_dates[source_dates >= cutoff_date] # overwrite with source data
else:
print("Dry run: No changes made.")
def main():
parser = argparse.ArgumentParser(description="Mirror date values from source to staging.")
parser.add_argument("source_file", help="Path to the source data file.")
parser.add_argument("staging_file", help="Path to the staging data file.")
parser.add_argument("date_field", help="Name of the date field.")
parser.add_argument("--days_back", type=int, default=7, help="Number of days to go back for mirroring (default: 7).")
parser.add_argument("--dry_run", action="store_true", help="Perform a dry run (no changes made).")
args = parser.parse_args()
try:
# Simulate reading data from files (replace with actual file reading logic)
source_data = {"dates": [datetime.now() - timedelta(days=i) for i in range(15)]}
staging_data = {"dates": [datetime.now() - timedelta(days=i) for i in range(15)]}
mirror_dates(source_data, staging_data, args.date_field, args.days_back, args.dry_run)
# Simulate writing back to files (replace with actual file writing logic)
print("Mirroring complete.")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()
Add your comment