Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
ksimpson-work committed Nov 25, 2024
1 parent 5e950f7 commit 8dc3eaf
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions continuous_integration/scripts/render-template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,49 +13,39 @@ 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)

# Render the template with the context
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}')

0 comments on commit 8dc3eaf

Please sign in to comment.