|
- import json
-
- def add_file_paths(data):
- if not isinstance(data, dict):
- return data
-
- for country_slug, country_data in data.items():
- if 'cities' in country_data:
- for city_slug, city_data in country_data['cities'].items():
- if 'circuits' in city_data:
- for circuit_slug, circuit_data in city_data['circuits'].items():
- if 'layouts' in circuit_data:
- for layout_years, layout_data in circuit_data['layouts'].items():
- layout_slug = layout_data.get('slug', layout_years)
-
- # Generate filePaths dictionary
- base_path = f"/{country_slug}/{city_slug}/{circuit_slug}/{layout_slug}"
- layout_data['filePaths'] = {
- "png": f"{base_path}.png",
- "svg": f"{base_path}.svg",
- "geojson": f"{base_path}.geo.json"
- }
-
- return data
-
- def main():
- try:
- # Read the JSON file with UTF-8 encoding
- with open('./circuits.json', 'r', encoding='utf-8') as file:
- data = json.load(file)
-
- # Process the data
- updated_data = add_file_paths(data)
-
- # Write the updated data back to the file with UTF-8 encoding and ensure_ascii=False
- with open('./circuits-new.json', 'w', encoding='utf-8') as file:
- json.dump(updated_data, file, indent=2, ensure_ascii=False)
-
- print("Successfully added file paths to layouts")
-
- except FileNotFoundError:
- print("Error: circuits.json file not found")
- except json.JSONDecodeError:
- print("Error: Invalid JSON format in circuits.json")
- except Exception as e:
- print(f"An error occurred: {str(e)}")
-
- if __name__ == "__main__":
- main()
|