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.
alias of SafeDumper
alias of CSafeLoader
Raised when stream of string failed to be deserialized
sls dumper.
Create a custom YAML loader that uses the custom constructor. This allows for the YAML loading defaults to be manipulated based on needs within salt to make things like sls file more intuitive.
Verify integers and pass them in correctly is they are declared as octal
Build the SLSString.
Build the SLSMap
Map aggregation.
Dictionary that remembers insertion order
Create a new ordered dictionary with keys from iterable and values set to value.
Move an existing element to the end (or beginning if last is false).
Raise KeyError if the element does not exist.
If the key is not found, return the default if given; otherwise, raise a KeyError.
Remove and return a (key, value) pair from the dictionary.
Pairs are returned in LIFO order if last is true or FIFO order if false.
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
Ensures that dict str() and repr() are YAML friendly.
>>> mapping = OrderedDict([('a', 'b'), ('c', None)])
>>> print mapping
OrderedDict([('a', 'b'), ('c', None)])
>>> sls_map = SLSMap(mapping)
>>> print sls_map.__str__()
{a: b, c: null}
Ensures that str str() and repr() are YAML friendly.
>>> scalar = str('foo')
>>> print 'foo'
foo
>>> sls_scalar = SLSString(scalar)
>>> print sls_scalar
"foo"
Sequence aggregation.
Raised when stream of string failed to be serialized
Merge obj_b into obj_a.
>>> aggregate('first', 'second', True) == ['first', 'second']
True
Deserialize any string of stream like object into a Python data structure.
stream_or_string -- stream or string to deserialize.
options -- options given to lower yaml module.
Merge obj_b into obj_a.
Serialize Python data to YAML.
obj -- the data structure to serialize
options -- options given to lower yaml module.