1. import yaml
  2. import os
  3. def inject_header_parameters(config_file, output_file):
  4. """
  5. Injects header parameters from a YAML configuration file into a local utility script.
  6. Args:
  7. config_file (str): Path to the YAML configuration file.
  8. output_file (str): Path to the utility script (where parameters will be injected).
  9. """
  10. try:
  11. with open(config_file, 'r') as f:
  12. config = yaml.safe_load(f)
  13. except FileNotFoundError:
  14. print(f"Error: Configuration file not found: {config_file}")
  15. return
  16. except yaml.YAMLError as e:
  17. print(f"Error: Invalid YAML format in {config_file}: {e}")
  18. return
  19. # Generate header parameters string
  20. header_params = ""
  21. for key, value in config.get('headers', {}).items():
  22. header_params += f"export {key}='{value}'; "
  23. # Inject parameters into the utility script
  24. try:
  25. with open(output_file, 'r') as f:
  26. script_content = f.read()
  27. except FileNotFoundError:
  28. print(f"Error: Utility script not found: {output_file}")
  29. return
  30. # Inject header parameters at the beginning of the script
  31. modified_script = header_params + script_content
  32. with open(output_file, 'w') as f:
  33. f.write(modified_script)
  34. print(f"Header parameters injected into {output_file}")
  35. if __name__ == '__main__':
  36. # Example Usage:
  37. config_file = 'config.yaml'
  38. output_file = 'my_utility.py'
  39. inject_header_parameters(config_file, output_file)

Add your comment