1. import os
  2. import json
  3. def split_config(config_file, output_dir="config_splits"):
  4. """
  5. Splits a configuration file into development-only minimal configurations.
  6. Args:
  7. config_file (str): Path to the configuration file.
  8. output_dir (str): Directory to store the split configurations.
  9. """
  10. if not os.path.exists(output_dir):
  11. os.makedirs(output_dir)
  12. try:
  13. with open(config_file, 'r') as f:
  14. config = json.load(f)
  15. except FileNotFoundError:
  16. print(f"Error: Configuration file not found: {config_file}")
  17. return
  18. except json.JSONDecodeError:
  19. print(f"Error: Invalid JSON in configuration file: {config_file}")
  20. return
  21. development_config = {}
  22. minimal_config = {}
  23. for key, value in config.items():
  24. if key.startswith("development_only_"):
  25. development_config[key] = value
  26. elif key.startswith("minimal_"):
  27. minimal_config[key] = value
  28. else:
  29. #Consider all other keys as essential
  30. pass
  31. development_file = os.path.join(output_dir, "development.json")
  32. minimal_file = os.path.join(output_dir, "minimal.json")
  33. try:
  34. with open(development_file, 'w') as f:
  35. json.dump(development_config, f, indent=4)
  36. with open(minimal_file, 'w') as f:
  37. json.dump(minimal_config, f, indent=4)
  38. print(f"Split configurations saved to: {output_dir}")
  39. except Exception as e:
  40. print(f"Error writing to file: {e}")
  41. if __name__ == '__main__':
  42. # Example Usage:
  43. # Assuming you have a config file named 'config.json'
  44. split_config("config.json")

Add your comment