File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/serializers/__pycache__/yamlex.cpython-310.pyc
Back
o ;jq2 � @ s� d Z ddlZddlZddlZddlmZ ddlZddlmZ ddl m Z ddlmZ ddl mZmZ ddlmZmZmZ g d �Ze�e�Zd Zeedej�ZzejjZW n ey` ejZY nw dd iZ dd� Z!dd� Z"G dd� de�Z#e#�$de#j%� e#�$de#j&� e#�$de#j'� e#�$de#j(� e#�$de#j)� e#�*de#j+� e#�*de#j,� e#�*de#j-� e#�*de#j.� e#�*de#j/� e#�*de#j0� e#�*de#j1� e#�*d e#j2� e#�*d!e#j3� G d"d#� d#e�Z4G d$d%� d%e5�Z6G d&d'� d'e4e�Z7G d(d)� d)e�Z8G d*d+� d+e�Z9e9�:e;d�e9j<� e9�:e=e9j>� e9�:e5e9j?� e9�:e@e9jA� e9�:eBe9jC� e9�:eDe9jE� e9�:eFe9jG� e9�:eHe9jG� e9�:eIe9jJ� e9�:eKe9jL� e9�:ejMe9jN� e9�:eje9jO� e9�:de9jP� d/d-d.�ZQdS )0aR 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: .. code-block:: yaml foo: bar: 42 baz: [1, 2, 3] The file can be parsed into Python like this .. code-block:: python from salt.serializers import yamlex with open('state.sls', 'r') as stream: obj = yamlex.deserialize(stream) Check that ``obj`` is an OrderedDict .. code-block:: python from collections 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. .. code-block:: python print '{0}'.format(obj) returns: :: {foo: {bar: 42, baz: [1, 2, 3]}} and they are still valid YAML: .. code-block:: python 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: .. code-block:: yaml placeholder: !aggregate foo placeholder: !aggregate bar placeholder: !aggregate baz is rendered as .. code-block:: yaml placeholder: [foo, bar, baz] !reset this tag flushes the computing value. .. code-block:: yaml placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} !reset placeholder: {!aggregate foo: {baz: inga}} is roughly equivalent to .. code-block:: yaml placeholder: {!aggregate foo: {baz: inga}} Document is defacto an aggregate mapping. � N)�OrderedDict)�ConstructorError)�MappingNode)�ScannerError)�DeserializationError�SerializationError)�Map�Sequence� aggregate)�deserialize� serialize� availableTZCSafeLoaderz0found character '\t' that cannot start any tokenzIllegal tab characterc K s� |� dt� z tj| fi |��W S ty6 } zt�d� t�|j d�}|j jd }t|||j j ��d}~w tyJ } z t�d� t|��d}~w ty^ } z t�d� t|��d}~ww )z� Deserialize any string of stream like object into a Python data structure. :param stream_or_string: stream or string to deserialize. :param options: options given to lower yaml module. �Loaderz%Error encountered while deserializingzUnknown yaml render error� N)� setdefaultr �yaml�loadr �log� exception� ERROR_MAP�getZproblemZproblem_mark�liner �bufferr � Exception)Zstream_or_string�options�errorZerr_typeZline_num� r �K/opt/saltstack/salt/lib/python3.10/site-packages/salt/serializers/yamlex.pyr � s$ � � ��r c K s� |� dt� |� dd� z$tj| fi |��}|�d�r"|dd� W S |�d�r.|dd� W S |W S tyE } z t�d� t|��d}~ww ) z� Serialize Python data to YAML. :param obj: the data structure to serialize :param options: options given to lower yaml module. �Dumper�default_flow_styleNz ... ���� ���z#Error encountered while serializing) r r r �dump�endswithr r r r )�objr �responser r r r r � s ��r c @ sT e Zd ZdZdZdZdZdd� Zdd� Zd d � Z dd� Z d d� Zdd� Zdd� Z dS )r z� 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. �tag:yaml.org,2002:str�tag:yaml.org,2002:seq�tag:yaml.org,2002:omapc C s t �| �}d|_|S )N� !aggregate)� BaseLoader�compose_document�tag��self�noder r r r, � s zLoader.compose_documentc C s� t � }t|t�stddd|j� �|j��| �|� |jD ]V\}}|jdk}|jdkr;t � d� |j|_| �|�d |_| j|dd�}zt |� W n tyY td �|j|j���w | j|dd�}||v rn|snt|| |�}|||<