F1 circuit layouts with year-by-year SVGs — manually traced track variations
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

79 satır
2.8KB

  1. import json
  2. import shutil
  3. from pathlib import Path
  4. def load_json_file(file_path: str):
  5. """Load and parse a JSON file"""
  6. with open(file_path, 'r', encoding='utf-8') as f:
  7. return json.load(f)
  8. def save_json_file(data, file_path: str):
  9. """Save data to a JSON file"""
  10. with open(file_path, 'w', encoding='utf-8') as f:
  11. json.dump(data, f, indent=2, ensure_ascii=False)
  12. def clean_and_move_files(data):
  13. """Remove filePath and years properties, and move files to new locations"""
  14. new_data = {}
  15. for country, country_data in data.items():
  16. country_slug = country_data['slug']
  17. new_country_data = country_data.copy()
  18. for city, city_data in country_data['cities'].items():
  19. city_slug = city_data['slug']
  20. for circuit_name, circuit_data in city_data['circuits'].items():
  21. circuit_slug = circuit_data['slug']
  22. if 'layouts' in circuit_data:
  23. for layout_years, layout_data in circuit_data['layouts'].items():
  24. # Get the old file path
  25. old_file_path = None
  26. if 'filePath' in layout_data:
  27. old_file_path = layout_data['filePath']
  28. del layout_data['filePath']
  29. # Remove years property
  30. if 'years' in layout_data:
  31. del layout_data['years']
  32. # Construct new path
  33. layout_slug = layout_data['slug']
  34. new_base_path = Path(f"circuits/{country_slug}/{city_slug}/{circuit_slug}")
  35. # Create directory if it doesn't exist
  36. new_base_path.mkdir(parents=True, exist_ok=True)
  37. # Move files if they exist
  38. if old_file_path:
  39. for ext in ['.svg', '.png', '.geo.json']:
  40. old_file = Path(f"circuits/{old_file_path}{ext}")
  41. new_file = new_base_path / f"{layout_slug}{ext}"
  42. if old_file.exists():
  43. print(f"Moving {old_file} to {new_file}")
  44. shutil.move(old_file, new_file)
  45. else:
  46. print(f"File not found: {old_file}")
  47. new_data[country] = new_country_data
  48. return new_data
  49. def main():
  50. # Load the data
  51. data = load_json_file("circuits.json")
  52. # Clean data and move files
  53. print("Cleaning data and moving files...")
  54. new_data = clean_and_move_files(data)
  55. # Save the modified data
  56. print("Saving modified data...")
  57. save_json_file(new_data, "circuits.json")
  58. print("Done!")
  59. if __name__ == "__main__":
  60. main()