| 
									
										
										
										
											2025-07-01 20:51:18 +05:30
										 |  |  | import json | 
					
						
							|  |  |  | import logging | 
					
						
							|  |  |  | from pathlib import Path | 
					
						
							| 
									
										
										
										
											2025-08-07 15:29:01 +05:30
										 |  |  | from typing import Any, Dict | 
					
						
							| 
									
										
										
										
											2025-07-01 20:51:18 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | logger = logging.getLogger(__name__) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def should_write_json_file( | 
					
						
							| 
									
										
										
										
											2025-08-07 15:29:01 +05:30
										 |  |  |     output_path: Path, | 
					
						
							|  |  |  |     new_json_data: Dict[str, Any], | 
					
						
							|  |  |  |     file_description: str = "JSON file", | 
					
						
							| 
									
										
										
										
											2025-07-01 20:51:18 +05:30
										 |  |  | ) -> bool: | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Check if a JSON file should be written by comparing content with existing file. | 
					
						
							| 
									
										
										
										
											2025-08-07 15:29:01 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 20:51:18 +05:30
										 |  |  |     This function compares the new JSON data with existing file content, excluding | 
					
						
							|  |  |  |     the 'generated_at' field from comparison since it changes on every generation. | 
					
						
							| 
									
										
										
										
											2025-08-07 15:29:01 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 20:51:18 +05:30
										 |  |  |     Args: | 
					
						
							|  |  |  |         output_path: Path to the output file | 
					
						
							|  |  |  |         new_json_data: The new JSON data to potentially write | 
					
						
							|  |  |  |         file_description: Description of the file for logging purposes | 
					
						
							| 
									
										
										
										
											2025-08-07 15:29:01 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 20:51:18 +05:30
										 |  |  |     Returns: | 
					
						
							|  |  |  |         True if the file should be written, False if content is unchanged | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     write_file = True | 
					
						
							| 
									
										
										
										
											2025-08-07 15:29:01 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-01 20:51:18 +05:30
										 |  |  |     if output_path.exists(): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with open(output_path, "r") as f: | 
					
						
							|  |  |  |                 existing_data = json.load(f) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Create copies without generated_at for comparison | 
					
						
							|  |  |  |             existing_for_comparison = existing_data.copy() | 
					
						
							|  |  |  |             new_for_comparison = new_json_data.copy() | 
					
						
							|  |  |  |             existing_for_comparison.pop("generated_at", None) | 
					
						
							|  |  |  |             new_for_comparison.pop("generated_at", None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if json.dumps( | 
					
						
							|  |  |  |                 existing_for_comparison, indent=2, sort_keys=True | 
					
						
							|  |  |  |             ) == json.dumps(new_for_comparison, indent=2, sort_keys=True): | 
					
						
							|  |  |  |                 logger.info(f"No changes detected in {output_path}, skipping write.") | 
					
						
							|  |  |  |                 write_file = False | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             logger.warning(f"Could not read existing {file_description}: {e}") | 
					
						
							| 
									
										
										
										
											2025-08-07 15:29:01 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     return write_file |