Skip to content

Commit

Permalink
Render only unquoted jinja templates
Browse files Browse the repository at this point in the history
  • Loading branch information
danielabdi-noaa committed Apr 1, 2023
1 parent 987fce1 commit c21ae6b
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions ush/python_utils/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,18 +660,38 @@ def filter_dict(dict_o, keys_regex):
##################
# CONFIG loader
##################
def load_config_file_(config_file_or_string, return_string=0, rendered=None):
def render_jinja_template(config_file_or_string, context):
""" Render a jinja templated config file. """

with open(config_file_or_string) as f:
content = f.read()
# Hack: ignore quoted templates (values) from rendering
# Those are taken care of by extend_yaml
content.replace("'{{","'{[")
content.replace('"{{','"{[')
template = jinja2.Template(content)
content = template.render(context)
content.replace("'{[","'{{")
content.replace('"{[','"{{')
return content

return None


def load_config_file(config_file_or_string, return_string=0, context=None):
"""Load config file based on file name extension
Args:
config_file_or_string: path to config file or string of contents
return_string: can be [0, 1, 2]
rendered: if config_file_or_string is jinja templated, this var contains the
rendered content of the file
context: file or dictionary to be used for rendering jinja templated file
"""

ext = os.path.splitext(config_file_or_string)[1][1:]
config_file_or_string = (config_file_or_string if rendered is None else rendered)
if context is not None:
if not isinstance(context, dict):
context = load_config_file(context, return_string)
config_file_or_string = render_jinja_template(config_file_or_string, context)

if ext == "sh":
return load_shell_config(config_file_or_string, return_string)
Expand All @@ -685,29 +705,6 @@ def load_config_file_(config_file_or_string, return_string=0, rendered=None):
return load_xml_config(config_file_or_string, return_string)
return None

def render_jinja_template(config_file_or_string, context):
""" Render a jinja templated config file. """

with open(config_file_or_string) as f:
template = jinja2.Template(f.read())
return template.render(context)

return None


def load_config_file(config_file_or_string, return_string=0, context=None):
""" Load jinja templated config file. """

if context is not None:
if not isinstance(context, dict):
context = load_config_file_(context)
rendered = render_jinja_template(config_file_or_string, context)

return load_config_file_(config_file_or_string, return_string, rendered)
else:
return load_config_file_(config_file_or_string, return_string)


##################
# CONFIG main
##################
Expand Down

0 comments on commit c21ae6b

Please sign in to comment.