Content-Length: 362510 | pFad | http://github.com/saltstack/salt/pull/65004/commits/ca1495a0f0dcd6cd6b146f6bb66b68444526aedf

D0 [3006.x][WIP] Handle StrictUndefined for all salt added jinja filters by cmcmarrow · Pull Request #65004 · saltstack/salt · GitHub
Skip to content

[3006.x][WIP] Handle StrictUndefined for all salt added jinja filters #65004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: 3006.x
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
handle_strict_undefined
  • Loading branch information
cmcmarrow authored and twangboy committed May 16, 2025
commit ca1495a0f0dcd6cd6b146f6bb66b68444526aedf
34 changes: 27 additions & 7 deletions salt/utils/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,16 @@ def show_full_context(ctx):
)


def _handle_strict_undefined(function):
@wraps(function)
def __handle_strict_undefined(value, *args, **kwargs):
if isinstance(value, jinja2.StrictUndefined):
return value
return function(value, *args, **kwargs)

return __handle_strict_undefined


class SerializerExtension(Extension):
'''
Yaml and Json manipulation.
Expand Down Expand Up @@ -956,13 +966,15 @@ def __init__(self, environment):
"load_json": self.load_json,
"load_text": self.load_text,
"dict_to_sls_yaml_params": self.dict_to_sls_yaml_params,
"combinations": itertools.combinations,
"combinations_with_replacement": itertools.combinations_with_replacement,
"compress": itertools.compress,
"permutations": itertools.permutations,
"product": itertools.product,
"zip": zip,
"zip_longest": itertools.zip_longest,
"combinations": _handle_strict_undefined(itertools.combinations),
"combinations_with_replacement": _handle_strict_undefined(
itertools.combinations_with_replacement
),
"compress": _handle_strict_undefined(itertools.compress),
"permutations": _handle_strict_undefined(itertools.permutations),
"product": _handle_strict_undefined(itertools.product),
"zip": _handle_strict_undefined(zip),
"zip_longest": _handle_strict_undefined(itertools.zip_longest),
}
)

Expand Down Expand Up @@ -993,6 +1005,7 @@ def explore(data):

return explore(data)

@_handle_strict_undefined
def format_json(self, value, sort_keys=True, indent=None):
json_txt = salt.utils.json.dumps(
value, sort_keys=sort_keys, indent=indent
Expand All @@ -1002,6 +1015,7 @@ 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):
yaml_txt = salt.utils.yaml.safe_dump(
value, default_flow_style=flow_style
Expand All @@ -1013,6 +1027,7 @@ def format_yaml(self, value, flow_style=True):
except UnicodeDecodeError:
return Markup(salt.utils.stringutils.to_unicode(yaml_txt))

@_handle_strict_undefined
def format_xml(self, value):
"""Render a formatted multi-line XML string from a complex Python
data structure. Supports tag attributes and nested dicts/lists.
Expand Down Expand Up @@ -1069,9 +1084,11 @@ def recurse_tree(xmliter, element=None):
).toprettyxml(indent=" ")
)

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

@_handle_strict_undefined
def load_yaml(self, value):
if isinstance(value, TemplateModule):
value = str(value)
Expand All @@ -1097,6 +1114,7 @@ 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, TemplateModule):
value = str(value)
Expand All @@ -1105,6 +1123,7 @@ def load_json(self, 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, TemplateModule):
value = str(value)
Expand Down Expand Up @@ -1231,6 +1250,7 @@ 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 Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/saltstack/salt/pull/65004/commits/ca1495a0f0dcd6cd6b146f6bb66b68444526aedf

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy