42 lines
1.0 KiB
Python
Executable File
42 lines
1.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sys
|
|
import configparser
|
|
from pathlib import Path
|
|
|
|
|
|
def replace_params(param_file: Path, template_file: Path, output_file: Path):
|
|
"""Replace placeholders in template_file with values from an INI file."""
|
|
config = configparser.ConfigParser()
|
|
config.optionxform = str # keep case sensitivity
|
|
config.read(param_file)
|
|
|
|
# Flatten into a simple dict {KEY: VALUE}
|
|
params = {}
|
|
for section in config.sections():
|
|
for key, value in config.items(section):
|
|
params[key] = value
|
|
|
|
content = template_file.read_text()
|
|
|
|
for key, value in params.items():
|
|
content = content.replace(key, value)
|
|
|
|
output_file.write_text(content)
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) != 4:
|
|
print(f"Usage: {sys.argv[0]} PARAM_FILE TEMPLATE_FILE OUTPUT_FILE")
|
|
sys.exit(1)
|
|
|
|
param_file = Path(sys.argv[1])
|
|
template_file = Path(sys.argv[2])
|
|
output_file = Path(sys.argv[3])
|
|
|
|
replace_params(param_file, template_file, output_file)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|