forked from saltstack/salt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jinja.py
1171 lines (905 loc) · 31.4 KB
/
jinja.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Jinja loading utils to enable a more powerful backend for jinja templates
"""
import atexit
import logging
import os.path
import pipes
import pprint
import re
import time
import uuid
import warnings
from collections.abc import Hashable
from functools import wraps
from xml.dom import minidom
from xml.etree.ElementTree import Element, SubElement, tostring
import jinja2
import salt.fileclient
import salt.utils.data
import salt.utils.files
import salt.utils.json
import salt.utils.stringutils
import salt.utils.url
import salt.utils.yaml
from jinja2 import BaseLoader, Markup, TemplateNotFound, nodes
from jinja2.environment import TemplateModule
from jinja2.exceptions import TemplateRuntimeError
from jinja2.ext import Extension
from salt.exceptions import TemplateError
from salt.utils.decorators.jinja import jinja_filter, jinja_global, jinja_test
from salt.utils.odict import OrderedDict
from salt.utils.versions import LooseVersion
log = logging.getLogger(__name__)
__all__ = ["SaltCacheLoader", "SerializerExtension"]
GLOBAL_UUID = uuid.UUID("91633EBF-1C86-5E33-935A-28061F4B480E")
JINJA_VERSION = LooseVersion(jinja2.__version__)
class SaltCacheLoader(BaseLoader):
"""
A special jinja Template Loader for salt.
Requested templates are always fetched from the server
to guarantee that the file is up to date.
Templates are cached like regular salt states
and only loaded once per loader instance.
"""
_cached_pillar_client = None
_cached_client = None
@classmethod
def shutdown(cls):
for attr in ("_cached_client", "_cached_pillar_client"):
client = getattr(cls, attr, None)
if client is not None:
# PillarClient and LocalClient objects do not have a destroy method
if hasattr(client, "destroy"):
client.destroy()
setattr(cls, attr, None)
def __init__(
self,
opts,
saltenv="base",
encoding="utf-8",
pillar_rend=False,
_file_client=None,
):
self.opts = opts
self.saltenv = saltenv
self.encoding = encoding
self.pillar_rend = pillar_rend
if self.pillar_rend:
if saltenv not in self.opts["pillar_roots"]:
self.searchpath = []
else:
self.searchpath = opts["pillar_roots"][saltenv]
else:
self.searchpath = [os.path.join(opts["cachedir"], "files", saltenv)]
log.debug("Jinja search path: %s", self.searchpath)
self.cached = []
self._file_client = _file_client
# Instantiate the fileclient
self.file_client()
def file_client(self):
"""
Return a file client. Instantiates on first call.
"""
# If there was no file_client passed to the class, create a cache_client
# and use that. This avoids opening a new file_client every time this
# class is instantiated
if self._file_client is None:
attr = "_cached_pillar_client" if self.pillar_rend else "_cached_client"
cached_client = getattr(self, attr, None)
if cached_client is None:
cached_client = salt.fileclient.get_file_client(
self.opts, self.pillar_rend
)
setattr(SaltCacheLoader, attr, cached_client)
self._file_client = cached_client
return self._file_client
def cache_file(self, template):
"""
Cache a file from the salt master
"""
saltpath = salt.utils.url.create(template)
self.file_client().get_file(saltpath, "", True, self.saltenv)
def check_cache(self, template):
"""
Cache a file only once
"""
if template not in self.cached:
self.cache_file(template)
self.cached.append(template)
def get_source(self, environment, template):
"""
Salt-specific loader to find imported jinja files.
Jinja imports will be interpreted as originating from the top
of each of the directories in the searchpath when the template
name does not begin with './' or '../'. When a template name
begins with './' or '../' then the import will be relative to
the importing file.
"""
# FIXME: somewhere do seprataor replacement: '\\' => '/'
_template = template
if template.split("/", 1)[0] in ("..", "."):
is_relative = True
else:
is_relative = False
# checks for relative '..' paths that step-out of file_roots
if is_relative:
# Starts with a relative path indicator
if not environment or "tpldir" not in environment.globals:
log.warning(
'Relative path "%s" cannot be resolved without an environment',
template,
)
raise TemplateNotFound
base_path = environment.globals["tpldir"]
_template = os.path.normpath("/".join((base_path, _template)))
if _template.split("/", 1)[0] == "..":
log.warning(
'Discarded template path "%s": attempts to'
" ascend outside of salt://",
template,
)
raise TemplateNotFound(template)
self.check_cache(_template)
if environment and template:
tpldir = os.path.dirname(_template).replace("\\", "/")
tplfile = _template
if is_relative:
tpldir = environment.globals.get("tpldir", tpldir)
tplfile = template
tpldata = {
"tplfile": tplfile,
"tpldir": "." if tpldir == "" else tpldir,
"tpldot": tpldir.replace("/", "."),
}
environment.globals.update(tpldata)
# pylint: disable=cell-var-from-loop
for spath in self.searchpath:
filepath = os.path.join(spath, _template)
try:
with salt.utils.files.fopen(filepath, "rb") as ifile:
contents = ifile.read().decode(self.encoding)
mtime = os.path.getmtime(filepath)
def uptodate():
try:
return os.path.getmtime(filepath) == mtime
except OSError:
return False
return contents, filepath, uptodate
except OSError:
# there is no file under current path
continue
# pylint: enable=cell-var-from-loop
# there is no template file within searchpaths
raise TemplateNotFound(template)
atexit.register(SaltCacheLoader.shutdown)
class PrintableDict(OrderedDict):
"""
Ensures that dict str() and repr() are YAML friendly.
.. code-block:: python
mapping = OrderedDict([('a', 'b'), ('c', None)])
print mapping
# OrderedDict([('a', 'b'), ('c', None)])
decorated = PrintableDict(mapping)
print decorated
# {'a': 'b', 'c': None}
"""
def __str__(self):
output = []
for key, value in self.items():
if isinstance(value, str):
# keeps quotes around strings
# pylint: disable=repr-flag-used-in-string
output.append("{!r}: {!r}".format(key, value))
# pylint: enable=repr-flag-used-in-string
else:
# let default output
output.append("{!r}: {!s}".format(key, value))
return "{" + ", ".join(output) + "}"
def __repr__(self): # pylint: disable=W0221
output = []
for key, value in self.items():
# Raw string formatter required here because this is a repr
# function.
# pylint: disable=repr-flag-used-in-string
output.append("{!r}: {!r}".format(key, value))
# pylint: enable=repr-flag-used-in-string
return "{" + ", ".join(output) + "}"
# Additional globals
@jinja_global("raise")
def jinja_raise(msg):
raise TemplateError(msg)
# Additional tests
@jinja_test("match")
def test_match(txt, rgx, ignorecase=False, multiline=False):
"""Returns true if a sequence of chars matches a pattern."""
flag = 0
if ignorecase:
flag |= re.I
if multiline:
flag |= re.M
compiled_rgx = re.compile(rgx, flag)
return True if compiled_rgx.match(txt) else False
@jinja_test("equalto")
def test_equalto(value, other):
"""Returns true if two values are equal."""
return value == other
# Additional filters
@jinja_filter("skip")
def skip_filter(data):
"""
Suppress data output
.. code-block:: yaml
{% my_string = "foo" %}
{{ my_string|skip }}
will be rendered as empty string,
"""
return ""
@jinja_filter("sequence")
def ensure_sequence_filter(data):
"""
Ensure sequenced data.
**sequence**
ensure that parsed data is a sequence
.. code-block:: jinja
{% set my_string = "foo" %}
{% set my_list = ["bar", ] %}
{% set my_dict = {"baz": "qux"} %}
{{ my_string|sequence|first }}
{{ my_list|sequence|first }}
{{ my_dict|sequence|first }}
will be rendered as:
.. code-block:: yaml
foo
bar
baz
"""
if not isinstance(data, (list, tuple, set, dict)):
return [data]
return data
@jinja_filter("to_bool")
def to_bool(val):
"""
Returns the logical value.
.. code-block:: jinja
{{ 'yes' | to_bool }}
will be rendered as:
.. code-block:: text
True
"""
if val is None:
return False
if isinstance(val, bool):
return val
if isinstance(val, (str, (str,))):
return val.lower() in ("yes", "1", "true")
if isinstance(val, int):
return val > 0
if not isinstance(val, Hashable):
return len(val) > 0
return False
@jinja_filter("indent")
def indent(s, width=4, first=False, blank=False, indentfirst=None):
"""
A ported version of the "indent" filter containing a fix for indenting Markup
objects. If the minion has Jinja version 2.11 or newer, the "indent" filter
from upstream will be used, and this one will be ignored.
"""
if indentfirst is not None:
warnings.warn(
"The 'indentfirst' argument is renamed to 'first' and will"
" be removed in Jinja 3.0.",
DeprecationWarning,
stacklevel=2,
)
first = indentfirst
indention = " " * width
newline = "\n"
if isinstance(s, Markup):
indention = Markup(indention)
newline = Markup(newline)
s += newline # this quirk is necessary for splitlines method
if blank:
rv = (newline + indention).join(s.splitlines())
else:
lines = s.splitlines()
rv = lines.pop(0)
if lines:
rv += newline + newline.join(
indention + line if line else line for line in lines
)
if first:
rv = indention + rv
return rv
@jinja_filter("tojson")
def tojson(val, indent=None, **options):
"""
Implementation of tojson filter (only present in Jinja 2.9 and later).
Unlike the Jinja built-in filter, this allows arbitrary options to be
passed in to the underlying JSON library.
"""
options.setdefault("ensure_ascii", True)
if indent is not None:
options["indent"] = indent
return (
salt.utils.json.dumps(val, **options)
.replace("<", "\\u003c")
.replace(">", "\\u003e")
.replace("&", "\\u0026")
.replace("'", "\\u0027")
)
@jinja_filter("quote")
def quote(txt):
"""
Wraps a text around quotes.
.. code-block:: jinja
{% set my_text = 'my_text' %}
{{ my_text | quote }}
will be rendered as:
.. code-block:: text
'my_text'
"""
return pipes.quote(txt)
@jinja_filter()
def regex_escape(value):
return re.escape(value)
@jinja_filter("regex_search")
def regex_search(txt, rgx, ignorecase=False, multiline=False):
"""
Searches for a pattern in the text.
.. code-block:: jinja
{% set my_text = 'abcd' %}
{{ my_text | regex_search('^(.*)BC(.*)$', ignorecase=True) }}
will be rendered as:
.. code-block:: text
('a', 'd')
"""
flag = 0
if ignorecase:
flag |= re.I
if multiline:
flag |= re.M
obj = re.search(rgx, txt, flag)
if not obj:
return
return obj.groups()
@jinja_filter("regex_match")
def regex_match(txt, rgx, ignorecase=False, multiline=False):
"""
Searches for a pattern in the text.
.. code-block:: jinja
{% set my_text = 'abcd' %}
{{ my_text | regex_match('^(.*)BC(.*)$', ignorecase=True) }}
will be rendered as:
.. code-block:: text
('a', 'd')
"""
flag = 0
if ignorecase:
flag |= re.I
if multiline:
flag |= re.M
obj = re.match(rgx, txt, flag)
if not obj:
return
return obj.groups()
@jinja_filter("regex_replace")
def regex_replace(txt, rgx, val, ignorecase=False, multiline=False):
r"""
Searches for a pattern and replaces with a sequence of characters.
.. code-block:: jinja
{% set my_text = 'lets replace spaces' %}
{{ my_text | regex_replace('\s+', '__') }}
will be rendered as:
.. code-block:: text
lets__replace__spaces
"""
flag = 0
if ignorecase:
flag |= re.I
if multiline:
flag |= re.M
compiled_rgx = re.compile(rgx, flag)
return compiled_rgx.sub(val, txt)
@jinja_filter("uuid")
def uuid_(val):
"""
Returns a UUID corresponding to the value passed as argument.
.. code-block:: jinja
{{ 'example' | uuid }}
will be rendered as:
.. code-block:: text
f4efeff8-c219-578a-bad7-3dc280612ec8
"""
return str(uuid.uuid5(GLOBAL_UUID, salt.utils.stringutils.to_str(val)))
### List-related filters
@jinja_filter()
def unique(values):
"""
Removes duplicates from a list.
.. code-block:: jinja
{% set my_list = ['a', 'b', 'c', 'a', 'b'] -%}
{{ my_list | unique }}
will be rendered as:
.. code-block:: text
['a', 'b', 'c']
"""
ret = None
if isinstance(values, Hashable):
ret = set(values)
else:
ret = []
for value in values:
if value not in ret:
ret.append(value)
return ret
@jinja_filter("min")
def lst_min(obj):
"""
Returns the min value.
.. code-block:: jinja
{% set my_list = [1,2,3,4] -%}
{{ my_list | min }}
will be rendered as:
.. code-block:: text
1
"""
return min(obj)
@jinja_filter("max")
def lst_max(obj):
"""
Returns the max value.
.. code-block:: jinja
{% my_list = [1,2,3,4] -%}
{{ set my_list | max }}
will be rendered as:
.. code-block:: text
4
"""
return max(obj)
@jinja_filter("avg")
def lst_avg(lst):
"""
Returns the average value of a list.
.. code-block:: jinja
{% my_list = [1,2,3,4] -%}
{{ set my_list | avg }}
will be rendered as:
.. code-block:: yaml
2.5
"""
if not isinstance(lst, Hashable):
return float(sum(lst) / len(lst))
return float(lst)
@jinja_filter("union")
def union(lst1, lst2):
"""
Returns the union of two lists.
.. code-block:: jinja
{% my_list = [1,2,3,4] -%}
{{ set my_list | union([2, 4, 6]) }}
will be rendered as:
.. code-block:: text
[1, 2, 3, 4, 6]
"""
if isinstance(lst1, Hashable) and isinstance(lst2, Hashable):
return set(lst1) | set(lst2)
return unique(lst1 + lst2)
@jinja_filter("intersect")
def intersect(lst1, lst2):
"""
Returns the intersection of two lists.
.. code-block:: jinja
{% my_list = [1,2,3,4] -%}
{{ set my_list | intersect([2, 4, 6]) }}
will be rendered as:
.. code-block:: text
[2, 4]
"""
if isinstance(lst1, Hashable) and isinstance(lst2, Hashable):
return set(lst1) & set(lst2)
return unique([ele for ele in lst1 if ele in lst2])
@jinja_filter("difference")
def difference(lst1, lst2):
"""
Returns the difference of two lists.
.. code-block:: jinja
{% my_list = [1,2,3,4] -%}
{{ set my_list | difference([2, 4, 6]) }}
will be rendered as:
.. code-block:: text
[1, 3, 6]
"""
if isinstance(lst1, Hashable) and isinstance(lst2, Hashable):
return set(lst1) - set(lst2)
return unique([ele for ele in lst1 if ele not in lst2])
@jinja_filter("symmetric_difference")
def symmetric_difference(lst1, lst2):
"""
Returns the symmetric difference of two lists.
.. code-block:: jinja
{% my_list = [1,2,3,4] -%}
{{ set my_list | symmetric_difference([2, 4, 6]) }}
will be rendered as:
.. code-block:: text
[1, 3]
"""
if isinstance(lst1, Hashable) and isinstance(lst2, Hashable):
return set(lst1) ^ set(lst2)
return unique(
[ele for ele in union(lst1, lst2) if ele not in intersect(lst1, lst2)]
)
@jinja_filter("method_call")
def method_call(obj, f_name, *f_args, **f_kwargs):
return getattr(obj, f_name, lambda *args, **kwargs: None)(*f_args, **f_kwargs)
@jinja2.contextfunction
def show_full_context(ctx):
return salt.utils.data.simple_types_filter(
{key: value for key, value in ctx.items()}
)
class SerializerExtension(Extension):
'''
Yaml and Json manipulation.
**Format filters**
Allows jsonifying or yamlifying any data structure. For example, this dataset:
.. code-block:: python
data = {
'foo': True,
'bar': 42,
'baz': [1, 2, 3],
'qux': 2.0
}
.. code-block:: jinja
yaml = {{ data|yaml }}
json = {{ data|json }}
python = {{ data|python }}
xml = {{ {'root_node': data}|xml }}
will be rendered as::
yaml = {bar: 42, baz: [1, 2, 3], foo: true, qux: 2.0}
json = {"baz": [1, 2, 3], "foo": true, "bar": 42, "qux": 2.0}
python = {'bar': 42, 'baz': [1, 2, 3], 'foo': True, 'qux': 2.0}
xml = """<<?xml version="1.0" ?>
<root_node bar="42" foo="True" qux="2.0">
<baz>1</baz>
<baz>2</baz>
<baz>3</baz>
</root_node>"""
The yaml filter takes an optional flow_style parameter to control the
default-flow-style parameter of the YAML dumper.
.. code-block:: jinja
{{ data|yaml(False) }}
will be rendered as:
.. code-block:: yaml
bar: 42
baz:
- 1
- 2
- 3
foo: true
qux: 2.0
**Load filters**
Strings and variables can be deserialized with **load_yaml** and
**load_json** tags and filters. It allows one to manipulate data directly
in templates, easily:
.. code-block:: jinja
{%- set yaml_src = "{foo: it works}"|load_yaml %}
{%- set json_src = "{'bar': 'for real'}"|load_json %}
Dude, {{ yaml_src.foo }} {{ json_src.bar }}!
will be rendered as::
Dude, it works for real!
**Load tags**
Salt implements ``load_yaml`` and ``load_json`` tags. They work like
the `import tag`_, except that the document is also deserialized.
Syntaxes are ``{% load_yaml as [VARIABLE] %}[YOUR DATA]{% endload %}``
and ``{% load_json as [VARIABLE] %}[YOUR DATA]{% endload %}``
For example:
.. code-block:: jinja
{% load_yaml as yaml_src %}
foo: it works
{% endload %}
{% load_json as json_src %}
{
"bar": "for real"
}
{% endload %}
Dude, {{ yaml_src.foo }} {{ json_src.bar }}!
will be rendered as::
Dude, it works for real!
**Import tags**
External files can be imported and made available as a Jinja variable.
.. code-block:: jinja
{% import_yaml "myfile.yml" as myfile %}
{% import_json "defaults.json" as defaults %}
{% import_text "completeworksofshakespeare.txt" as poems %}
**Catalog**
``import_*`` and ``load_*`` tags will automatically expose their
target variable to import. This feature makes catalog of data to
handle.
for example:
.. code-block:: jinja
# doc1.sls
{% load_yaml as var1 %}
foo: it works
{% endload %}
{% load_yaml as var2 %}
bar: for real
{% endload %}
.. code-block:: jinja
# doc2.sls
{% from "doc1.sls" import var1, var2 as local2 %}
{{ var1.foo }} {{ local2.bar }}
** Escape Filters **
.. versionadded:: 2017.7.0
Allows escaping of strings so they can be interpreted literally by another
function.
For example:
.. code-block:: jinja
regex_escape = {{ 'https://example.com?foo=bar%20baz' | regex_escape }}
will be rendered as::
regex_escape = https\\:\\/\\/example\\.com\\?foo\\=bar\\%20baz
** Set Theory Filters **
.. versionadded:: 2017.7.0
Performs set math using Jinja filters.
For example:
.. code-block:: jinja
unique = {{ ['foo', 'foo', 'bar'] | unique }}
will be rendered as::
unique = ['foo', 'bar']
.. _`import tag`: https://jinja.palletsprojects.com/en/2.11.x/templates/#import
'''
tags = {
"load_yaml",
"load_json",
"import_yaml",
"import_json",
"load_text",
"import_text",
"profile",
}
def __init__(self, environment):
super().__init__(environment)
self.environment.filters.update(
{
"yaml": self.format_yaml,
"json": self.format_json,
"xml": self.format_xml,
"python": self.format_python,
"load_yaml": self.load_yaml,
"load_json": self.load_json,
"load_text": self.load_text,
}
)
if self.environment.finalize is None:
self.environment.finalize = self.finalizer
else:
finalizer = self.environment.finalize
@wraps(finalizer)
def wrapper(self, data):
return finalizer(self.finalizer(data))
self.environment.finalize = wrapper
def finalizer(self, data):
"""
Ensure that printed mappings are YAML friendly.
"""
def explore(data):
if isinstance(data, (dict, OrderedDict)):
return PrintableDict(
[(key, explore(value)) for key, value in data.items()]
)
elif isinstance(data, (list, tuple, set)):
return data.__class__([explore(value) for value in data])
return data
return explore(data)
def format_json(self, value, sort_keys=True, indent=None):
json_txt = salt.utils.json.dumps(
value, sort_keys=sort_keys, indent=indent
).strip()
try:
return Markup(json_txt)
except UnicodeDecodeError:
return Markup(salt.utils.stringutils.to_unicode(json_txt))
def format_yaml(self, value, flow_style=True):
yaml_txt = salt.utils.yaml.safe_dump(
value, default_flow_style=flow_style
).strip()
if yaml_txt.endswith("\n..."): # future lint: disable=blacklisted-function
yaml_txt = yaml_txt[: len(yaml_txt) - 4]
try:
return Markup(yaml_txt)
except UnicodeDecodeError:
return Markup(salt.utils.stringutils.to_unicode(yaml_txt))
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.
:param value: Complex data structure representing XML contents
:returns: Formatted XML string rendered with newlines and indentation
:rtype: str
"""
def normalize_iter(value):
if isinstance(value, (list, tuple)):
if isinstance(value[0], str):
xmlval = value
else:
xmlval = []
elif isinstance(value, dict):
xmlval = list(value.items())
else:
raise TemplateRuntimeError(
"Value is not a dict or list. Cannot render as XML"
)
return xmlval
def recurse_tree(xmliter, element=None):
sub = None
for tag, attrs in xmliter:
if isinstance(attrs, list):
for attr in attrs:
recurse_tree(((tag, attr),), element)
elif element is not None:
sub = SubElement(element, tag)
else:
sub = Element(tag)
if isinstance(attrs, (str, int, bool, float)):
sub.text = str(attrs)
continue
if isinstance(attrs, dict):
sub.attrib = {
attr: str(val)
for attr, val in attrs.items()
if not isinstance(val, (dict, list))
}
for tag, val in [
item
for item in normalize_iter(attrs)
if isinstance(item[1], (dict, list))
]: