1. import argparse
  2. from datetime import datetime, timedelta
  3. def mirror_dates(source_data, staging_data, date_field, days_back=7, dry_run=False):
  4. """Mirrors date values from source to staging data."""
  5. try:
  6. source_dates = source_data[date_field]
  7. staging_dates = staging_data[date_field]
  8. except KeyError as e:
  9. raise ValueError(f"Date field '{date_field}' not found in source or staging data.")
  10. cutoff_date = datetime.now() - timedelta(days=days_back)
  11. for i in range(len(source_dates)):
  12. source_date = source_dates[i]
  13. # Only mirror if the source date is within the cutoff
  14. if source_date >= cutoff_date:
  15. try:
  16. staging_dates[i] = source_date # Mirror the date
  17. except TypeError:
  18. print(f"Warning: Could not mirror date. Type mismatch. Source: {source_date}, Staging: {staging_dates[i]}. Skipping.")
  19. continue #skip to next iteration
  20. if not dry_run:
  21. staging_data[date_field] = source_dates[source_dates >= cutoff_date] # overwrite with source data
  22. else:
  23. print("Dry run: No changes made.")
  24. def main():
  25. parser = argparse.ArgumentParser(description="Mirror date values from source to staging.")
  26. parser.add_argument("source_file", help="Path to the source data file.")
  27. parser.add_argument("staging_file", help="Path to the staging data file.")
  28. parser.add_argument("date_field", help="Name of the date field.")
  29. parser.add_argument("--days_back", type=int, default=7, help="Number of days to go back for mirroring (default: 7).")
  30. parser.add_argument("--dry_run", action="store_true", help="Perform a dry run (no changes made).")
  31. args = parser.parse_args()
  32. try:
  33. # Simulate reading data from files (replace with actual file reading logic)
  34. source_data = {"dates": [datetime.now() - timedelta(days=i) for i in range(15)]}
  35. staging_data = {"dates": [datetime.now() - timedelta(days=i) for i in range(15)]}
  36. mirror_dates(source_data, staging_data, args.date_field, args.days_back, args.dry_run)
  37. # Simulate writing back to files (replace with actual file writing logic)
  38. print("Mirroring complete.")
  39. except ValueError as e:
  40. print(f"Error: {e}")
  41. except Exception as e:
  42. print(f"An unexpected error occurred: {e}")
  43. if __name__ == "__main__":
  44. main()

Add your comment