import json import shutil from pathlib import Path def load_json_file(file_path: str): """Load and parse a JSON file""" with open(file_path, 'r', encoding='utf-8') as f: return json.load(f) def save_json_file(data, file_path: str): """Save data to a JSON file""" with open(file_path, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2, ensure_ascii=False) def clean_and_move_files(data): """Remove filePath and years properties, and move files to new locations""" new_data = {} for country, country_data in data.items(): country_slug = country_data['slug'] new_country_data = country_data.copy() for city, city_data in country_data['cities'].items(): city_slug = city_data['slug'] for circuit_name, circuit_data in city_data['circuits'].items(): circuit_slug = circuit_data['slug'] if 'layouts' in circuit_data: for layout_years, layout_data in circuit_data['layouts'].items(): # Get the old file path old_file_path = None if 'filePath' in layout_data: old_file_path = layout_data['filePath'] del layout_data['filePath'] # Remove years property if 'years' in layout_data: del layout_data['years'] # Construct new path layout_slug = layout_data['slug'] new_base_path = Path(f"{country_slug}/{city_slug}/{circuit_slug}") # Create directory if it doesn't exist new_base_path.mkdir(parents=True, exist_ok=True) # Move files if they exist if old_file_path: for ext in ['.svg', '.png', '.geo.json']: old_file = Path(f"{old_file_path}{ext}") new_file = new_base_path / f"{layout_slug}{ext}" if old_file.exists(): print(f"Moving {old_file} to {new_file}") shutil.move(old_file, new_file) else: print(f"File not found: {old_file}") new_data[country] = new_country_data return new_data def main(): # Load the data data = load_json_file("circuits.json") # Clean data and move files print("Cleaning data and moving files...") new_data = clean_and_move_files(data) # Save the modified data print("Saving modified data...") save_json_file(new_data, "circuits.json") print("Done!") if __name__ == "__main__": main()