YAMLEX is a format that allows for things like sls files to be more intuitive.
It's an extension of YAML that implements all the salt magic: - it implies omap for any dict like. - it implies that string like data are str, not unicode - ...
For example, the file states.sls has this contents:
foo:
bar: 42
baz: [1, 2, 3]
The file can be parsed into Python like this
from salt.serializers import yamlex
with open('state.sls', 'r') as stream:
obj = yamlex.deserialize(stream)
Check that obj
is an OrderedDict
from salt.utils.odict import OrderedDict
assert isinstance(obj, dict)
assert isinstance(obj, OrderedDict)
yamlex __repr__ and __str__ objects' methods render YAML understandable string. It means that they are template friendly.
print '{0}'.format(obj)
returns:
{foo: {bar: 42, baz: [1, 2, 3]}}
and they are still valid YAML:
from salt.serializers import yaml
yml_obj = yaml.deserialize(str(obj))
assert yml_obj == obj
yamlex implements also custom tags:
!aggregate
this tag allows structures aggregation.For example:
placeholder: !aggregate foo placeholder: !aggregate bar placeholder: !aggregate bazis rendered as
placeholder: [foo, bar, baz]
!reset
this tag flushes the computing value.placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} !reset placeholder: {!aggregate foo: {baz: inga}}is roughly equivalent to
placeholder: {!aggregate foo: {baz: inga}}
Document is defacto an aggregate mapping.
salt.serializers.yamlex.
deserialize
(stream_or_string, **options)¶Deserialize any string of stream like object into a Python data structure.
参数: |
|
---|
salt.serializers.yamlex.
serialize
(obj, **options)¶Serialize Python data to YAML.
参数: |
|
---|