Skip to content

Commit

Permalink
fix dec
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcmarrow committed Aug 17, 2023
1 parent 25e5d64 commit 3a5a3ce
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions salt/utils/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,8 @@ def explore(data):

@_handle_strict_undefined
def format_json(self, value, sort_keys=True, indent=None):
if isinstance(value, jinja2.StrictUndefined):
return value
json_txt = salt.utils.json.dumps(
value, sort_keys=sort_keys, indent=indent
).strip()
Expand All @@ -1020,8 +1022,9 @@ def format_json(self, value, sort_keys=True, indent=None):
except UnicodeDecodeError:
return Markup(salt.utils.stringutils.to_unicode(json_txt))

@_handle_strict_undefined
def format_yaml(self, value, flow_style=True):
if isinstance(value, jinja2.StrictUndefined):
return value
yaml_txt = salt.utils.yaml.safe_dump(
value, default_flow_style=flow_style
).strip()
Expand All @@ -1041,6 +1044,8 @@ def format_xml(self, value):
:returns: Formatted XML string rendered with newlines and indentation
:rtype: str
"""
if isinstance(value, jinja2.StrictUndefined):
return value

def normalize_iter(value):
if isinstance(value, (list, tuple)):
Expand Down Expand Up @@ -1089,12 +1094,14 @@ def recurse_tree(xmliter, element=None):
).toprettyxml(indent=" ")
)

@_handle_strict_undefined
def format_python(self, value):
if isinstance(value, jinja2.StrictUndefined):
return value
return Markup(pprint.pformat(value).strip())

@_handle_strict_undefined
def load_yaml(self, value):
if isinstance(value, jinja2.StrictUndefined):
return value
if isinstance(value, TemplateModule):
value = str(value)
try:
Expand All @@ -1119,17 +1126,19 @@ def load_yaml(self, value):
except AttributeError:
raise TemplateRuntimeError(f"Unable to load yaml from {value}")

@_handle_strict_undefined
def load_json(self, value):
if isinstance(value, jinja2.StrictUndefined):
return value
if isinstance(value, TemplateModule):
value = str(value)
try:
return salt.utils.json.loads(value)
except (ValueError, TypeError, AttributeError):
raise TemplateRuntimeError(f"Unable to load json from {value}")

@_handle_strict_undefined
def load_text(self, value):
if isinstance(value, jinja2.StrictUndefined):
return value
if isinstance(value, TemplateModule):
value = str(value)

Expand Down Expand Up @@ -1255,7 +1264,6 @@ def parse_import(self, parser, converter):
parser, import_node.template, f"import_{converter}", body, lineno
)

@_handle_strict_undefined
def dict_to_sls_yaml_params(self, value, flow_style=False):
"""
.. versionadded:: 3005
Expand All @@ -1272,6 +1280,8 @@ def dict_to_sls_yaml_params(self, value, flow_style=False):
:returns: Formatted SLS YAML string rendered with newlines and
indentation
"""
if isinstance(value, jinja2.StrictUndefined):
return value
return self.format_yaml(
[{key: val} for key, val in value.items()], flow_style=flow_style
)

0 comments on commit 3a5a3ce

Please sign in to comment.