Skip to content

Commit 8dc3eaf

Browse files
committed
merge main
1 parent 5e950f7 commit 8dc3eaf

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

continuous_integration/scripts/render-template.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from jinja2 import Environment, FileSystemLoader
66
import re
77

8-
98
# TODO: make this work for arbitrary context. ie. implement replace_using_context()
109
def replace_placeholder(source_str, variable_name, variable_value):
1110
# Escaping any regex special characters in variable_name
@@ -14,49 +13,39 @@ def replace_placeholder(source_str, variable_name, variable_value):
1413
# Using regular expression to replace ${variable_name} with actual variable_value
1514
# \s* means any amount of whitespace (including none)
1615
# pattern = rf'\$\{{\s*\{{\s*{variable_name_escaped}\s*\}}\s*\}}'
17-
pattern = rf"<<\s*{variable_name_escaped}\s*>>"
16+
pattern = rf'<<\s*{variable_name_escaped}\s*>>'
1817
return re.sub(pattern, variable_value.strip(), source_str)
1918

20-
2119
# Setup command-line argument parsing
22-
parser = argparse.ArgumentParser(
23-
description="Render a Jinja2 template using a JSON context."
24-
)
25-
parser.add_argument(
26-
"template_file",
27-
type=str,
28-
help="Path to the Jinja2 template file (with .j2 extension).",
29-
)
30-
parser.add_argument(
31-
"json_file", type=str, help="Path to the JSON file to use as the rendering context."
32-
)
33-
parser.add_argument("output_file", type=str, help="Path to the output file.")
20+
parser = argparse.ArgumentParser(description='Render a Jinja2 template using a JSON context.')
21+
parser.add_argument('template_file', type=str, help='Path to the Jinja2 template file (with .j2 extension).')
22+
parser.add_argument('json_file', type=str, help='Path to the JSON file to use as the rendering context.')
23+
parser.add_argument('output_file', type=str, help='Path to the output file.')
3424

3525
args = parser.parse_args()
3626

3727
# Load JSON file as the rendering context
38-
with open(args.json_file, "r") as file:
28+
with open(args.json_file, 'r') as file:
3929
context = json.load(file)
4030

4131
# Setup Jinja2 environment and load the template
4232
env = Environment(
43-
loader=FileSystemLoader(searchpath="./"),
44-
variable_start_string="<<",
45-
variable_end_string=">>",
46-
block_start_string="<%",
47-
block_end_string="%>",
48-
comment_start_string="<#",
49-
comment_end_string="#>",
50-
)
51-
env.filters["replace_placeholder"] = replace_placeholder
33+
loader=FileSystemLoader(searchpath='./'),
34+
variable_start_string='<<',
35+
variable_end_string='>>',
36+
block_start_string='<%',
37+
block_end_string='%>',
38+
comment_start_string='<#',
39+
comment_end_string='#>')
40+
env.filters['replace_placeholder'] = replace_placeholder
5241

5342
template = env.get_template(args.template_file)
5443

5544
# Render the template with the context
5645
rendered_content = template.render(context)
5746
# print(rendered_content)
5847

59-
with open(args.output_file, "w") as file:
48+
with open(args.output_file, 'w') as file:
6049
file.write(rendered_content)
6150

62-
print(f"Template rendered successfully. Output saved to {args.output_file}")
51+
print(f'Template rendered successfully. Output saved to {args.output_file}')

0 commit comments

Comments
 (0)