diff --git a/continuous_integration/scripts/render-template.py b/continuous_integration/scripts/render-template.py index eebe8bf0..c5c52670 100755 --- a/continuous_integration/scripts/render-template.py +++ b/continuous_integration/scripts/render-template.py @@ -5,7 +5,6 @@ from jinja2 import Environment, FileSystemLoader import re - # TODO: make this work for arbitrary context. ie. implement replace_using_context() def replace_placeholder(source_str, variable_name, variable_value): # Escaping any regex special characters in variable_name @@ -14,41 +13,31 @@ def replace_placeholder(source_str, variable_name, variable_value): # Using regular expression to replace ${variable_name} with actual variable_value # \s* means any amount of whitespace (including none) # pattern = rf'\$\{{\s*\{{\s*{variable_name_escaped}\s*\}}\s*\}}' - pattern = rf"<<\s*{variable_name_escaped}\s*>>" + pattern = rf'<<\s*{variable_name_escaped}\s*>>' return re.sub(pattern, variable_value.strip(), source_str) - # Setup command-line argument parsing -parser = argparse.ArgumentParser( - description="Render a Jinja2 template using a JSON context." -) -parser.add_argument( - "template_file", - type=str, - help="Path to the Jinja2 template file (with .j2 extension).", -) -parser.add_argument( - "json_file", type=str, help="Path to the JSON file to use as the rendering context." -) -parser.add_argument("output_file", type=str, help="Path to the output file.") +parser = argparse.ArgumentParser(description='Render a Jinja2 template using a JSON context.') +parser.add_argument('template_file', type=str, help='Path to the Jinja2 template file (with .j2 extension).') +parser.add_argument('json_file', type=str, help='Path to the JSON file to use as the rendering context.') +parser.add_argument('output_file', type=str, help='Path to the output file.') args = parser.parse_args() # Load JSON file as the rendering context -with open(args.json_file, "r") as file: +with open(args.json_file, 'r') as file: context = json.load(file) # Setup Jinja2 environment and load the template env = Environment( - loader=FileSystemLoader(searchpath="./"), - variable_start_string="<<", - variable_end_string=">>", - block_start_string="<%", - block_end_string="%>", - comment_start_string="<#", - comment_end_string="#>", -) -env.filters["replace_placeholder"] = replace_placeholder + loader=FileSystemLoader(searchpath='./'), + variable_start_string='<<', + variable_end_string='>>', + block_start_string='<%', + block_end_string='%>', + comment_start_string='<#', + comment_end_string='#>') +env.filters['replace_placeholder'] = replace_placeholder template = env.get_template(args.template_file) @@ -56,7 +45,7 @@ def replace_placeholder(source_str, variable_name, variable_value): rendered_content = template.render(context) # print(rendered_content) -with open(args.output_file, "w") as file: +with open(args.output_file, 'w') as file: file.write(rendered_content) -print(f"Template rendered successfully. Output saved to {args.output_file}") +print(f'Template rendered successfully. Output saved to {args.output_file}')