import yaml
import os
def inject_header_parameters(config_file, output_file):
"""
Injects header parameters from a YAML configuration file into a local utility script.
Args:
config_file (str): Path to the YAML configuration file.
output_file (str): Path to the utility script (where parameters will be injected).
"""
try:
with open(config_file, 'r') as f:
config = yaml.safe_load(f)
except FileNotFoundError:
print(f"Error: Configuration file not found: {config_file}")
return
except yaml.YAMLError as e:
print(f"Error: Invalid YAML format in {config_file}: {e}")
return
# Generate header parameters string
header_params = ""
for key, value in config.get('headers', {}).items():
header_params += f"export {key}='{value}'; "
# Inject parameters into the utility script
try:
with open(output_file, 'r') as f:
script_content = f.read()
except FileNotFoundError:
print(f"Error: Utility script not found: {output_file}")
return
# Inject header parameters at the beginning of the script
modified_script = header_params + script_content
with open(output_file, 'w') as f:
f.write(modified_script)
print(f"Header parameters injected into {output_file}")
if __name__ == '__main__':
# Example Usage:
config_file = 'config.yaml'
output_file = 'my_utility.py'
inject_header_parameters(config_file, output_file)
Add your comment