salt.serializers.yamlex

salt.serializers.yamlex

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 baz

is 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.

class salt.serializers.yamlex.AggregatedMap
class salt.serializers.yamlex.AggregatedSequence(iterable=(), /)
salt.serializers.yamlex.BaseDumper

alias of yaml.dumper.SafeDumper

salt.serializers.yamlex.BaseLoader

alias of yaml.cyaml.CSafeLoader

exception salt.serializers.yamlex.ConstructorError(context=None, context_mark=None, problem=None, problem_mark=None, note=None)
exception salt.serializers.yamlex.DeserializationError(message, line_num=None, buf='', marker='    <======================', trace=None)

Raised when stream of string failed to be deserialized

class salt.serializers.yamlex.Dumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)

sls dumper.

represent_odict(data)
yaml_multi_representers = {<class 'NoneType'>: <function SafeRepresenter.represent_none at 0x4778de0>, <class 'bytes'>: <function SafeRepresenter.represent_binary at 0x4778f20>, <class 'str'>: <function SafeRepresenter.represent_str at 0x4778e80>, <class 'bool'>: <function SafeRepresenter.represent_bool at 0x4778fc0>, <class 'int'>: <function SafeRepresenter.represent_int at 0x4779060>, <class 'float'>: <function SafeRepresenter.represent_float at 0x4779180>, <class 'list'>: <function SafeRepresenter.represent_list at 0x4779220>, <class 'tuple'>: <function SafeRepresenter.represent_list at 0x4779220>, <class 'dict'>: <function Dumper.represent_odict at 0x85fa720>, <class 'set'>: <function SafeRepresenter.represent_set at 0x4779360>, <class 'datetime.date'>: <function SafeRepresenter.represent_date at 0x4779400>, <class 'datetime.datetime'>: <function SafeRepresenter.represent_datetime at 0x47794a0>, None: <function SafeRepresenter.represent_undefined at 0x47795e0>}
class salt.serializers.yamlex.Loader(stream)

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.

DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:omap'
DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str'
DEFAULT_SEQUENCE_TAG = 'tag:yaml.org,2002:seq'
compose_document()
construct_sls_aggregate(node)
construct_sls_int(node)

Verify integers and pass them in correctly is they are declared as octal

construct_sls_reset(node)
construct_sls_str(node)

Build the SLSString.

construct_yaml_omap(node)

Build the SLSMap

resolve_sls_tag(node)
yaml_constructors = {'tag:yaml.org,2002:null': <function SafeConstructor.construct_yaml_null at 0x494bb40>, 'tag:yaml.org,2002:bool': <function SafeConstructor.construct_yaml_bool at 0x494bd10>, 'tag:yaml.org,2002:int': <function Loader.construct_sls_int at 0x973dfe0>, 'tag:yaml.org,2002:float': <function SafeConstructor.construct_yaml_float at 0x494be50>, 'tag:yaml.org,2002:binary': <function SafeConstructor.construct_yaml_binary at 0x494bef0>, 'tag:yaml.org,2002:timestamp': <function SafeConstructor.construct_yaml_timestamp at 0x494c140>, 'tag:yaml.org,2002:omap': <function Loader.construct_yaml_omap at 0x9cb49c0>, 'tag:yaml.org,2002:pairs': <function SafeConstructor.construct_yaml_pairs at 0x47512c0>, 'tag:yaml.org,2002:set': <function SafeConstructor.construct_yaml_set at 0x4819560>, 'tag:yaml.org,2002:str': <function Loader.construct_sls_str at 0x96e8910>, 'tag:yaml.org,2002:seq': <function SafeConstructor.construct_yaml_seq at 0x4818e60>, 'tag:yaml.org,2002:map': <function SafeConstructor.construct_yaml_map at 0x481a620>, None: <function SafeConstructor.construct_undefined at 0x481a760>, '!aggregate': <function Loader.construct_sls_aggregate at 0x95baa80>, '!reset': <function Loader.construct_sls_reset at 0x954fc30>}
yaml_multi_constructors = {'tag:yaml.org,2002:binary': <function SafeConstructor.construct_yaml_binary at 0x494bef0>, 'tag:yaml.org,2002:bool': <function SafeConstructor.construct_yaml_bool at 0x494bd10>, 'tag:yaml.org,2002:float': <function SafeConstructor.construct_yaml_float at 0x494be50>, 'tag:yaml.org,2002:map': <function SafeConstructor.construct_yaml_map at 0x481a620>, 'tag:yaml.org,2002:null': <function SafeConstructor.construct_yaml_null at 0x494bb40>, 'tag:yaml.org,2002:pairs': <function SafeConstructor.construct_yaml_pairs at 0x47512c0>, 'tag:yaml.org,2002:seq': <function SafeConstructor.construct_yaml_seq at 0x4818e60>, 'tag:yaml.org,2002:set': <function SafeConstructor.construct_yaml_set at 0x4819560>, 'tag:yaml.org,2002:timestamp': <function SafeConstructor.construct_yaml_timestamp at 0x494c140>}
class salt.serializers.yamlex.Map

Map aggregation.

class salt.serializers.yamlex.MappingNode(tag, value, start_mark=None, end_mark=None, flow_style=None)
id = 'mapping'
class salt.serializers.yamlex.OrderedDict
class salt.serializers.yamlex.SLSMap

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}
class salt.serializers.yamlex.SLSString

Ensures that str str() and repr() are YAML friendly.

>>> scalar = str('foo')
>>> print 'foo'
foo

>>> sls_scalar = SLSString(scalar)
>>> print sls_scalar
"foo"
exception salt.serializers.yamlex.ScannerError(context=None, context_mark=None, problem=None, problem_mark=None, note=None)
class salt.serializers.yamlex.Sequence(iterable=(), /)

Sequence aggregation.

exception salt.serializers.yamlex.SerializationError(message='')

Raised when stream of string failed to be serialized

salt.serializers.yamlex.aggregate(obj_a, obj_b, level=False, map_class=<class 'salt.utils.aggregation.Map'>, sequence_class=<class 'salt.utils.aggregation.Sequence'>)

Merge obj_b into obj_a.

>>> aggregate('first', 'second', True) == ['first', 'second']
True
salt.serializers.yamlex.deserialize(stream_or_string, **options)

Deserialize any string of stream like object into a Python data structure.

Parameters
  • stream_or_string -- stream or string to deserialize.

  • options -- options given to lower yaml module.

salt.serializers.yamlex.merge_recursive(obj_a, obj_b, level=False)

Merge obj_b into obj_a.

salt.serializers.yamlex.serialize(obj, **options)

Serialize Python data to YAML.

Parameters
  • obj -- the data structure to serialize

  • options -- options given to lower yaml module.