o
    w7e                     @   s   d dl mZ d dlmZ d dlmZmZmZmZ d dl	Z	e	j
Z
eddZG dd de	jZd	d
 ZG dd de	jZedej G dd de	jjZdeeeeef f fddZdd ZdddZdS )    )
namedtuple)chain)AnyDictListTupleNSchemaPathMarks)path
start_markend_markc                       s   e Zd Z fddZ  ZS )_CustomSafeLoaderc                    s   t  |S N)superconstruct_scalar)selfnode	__class__ 4/usr/lib/python3/dist-packages/cloudinit/safeyaml.pyconstruct_python_unicode   s   z*_CustomSafeLoader.construct_python_unicode)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c              	   C   s|   g }|D ]7}d|j vr|| q|j dd\}}| |j vr6||j v r6|t|j || d|j|j q|| q|S )N.   )r	   appendrsplitr   replacer
   r   )new_key_pathmarks	new_marksmarkpath_prefix	_path_idxr   r   r   _fix_nested_map_index   s    

	r&   c                       sN   e Zd ZdZ fddZdd Z fddZd fd	d
	Z fddZ  Z	S )_CustomSafeLoaderWithMarksa  A loader which provides line and column start and end marks for YAML.

    If the YAML loaded represents a dictionary, get_single_data will inject
    a top-level "schemamarks" key in that dictionary which can be used at
    call-sites to process YAML paths schemamark metadata when annotating
    YAML files for errors.

    The schemamarks key is dictionary where each key is a dot-delimited path
    into the YAML object. Each dot represents an element that is nested under
    a parent and list items are represented with the format
    `<parent>.<list-index>`.

    The values in schemamarks will be the line number in the original content
    where YAML element begins to aid in annotation when encountering schema
    errors.

    The example YAML shows expected schemamarks for both dicts and lists:

      one: val1
      two:
        subtwo: val2
      three: [val3, val4]

    schemamarks == {
        "one": 1, "two": 2, "two.subtwo": 3, "three": 4, "three.0": 4,
        "three.1": 4
    }
    c                    s   t  | i | _d S r   )r   __init__schemamarks_by_line)r   streamr   r   r   r(   O   s   
z#_CustomSafeLoaderWithMarks.__init__c                 C   s   |j j| jv r| j|j j d d  dS t| j ddD ]8\}}|d d d D ],}|j j|j jkrU|j j|j jkrU|jj|jjkrU|jj|jjkrU|j d    S q)qdS )Nr   r   T)reverse )r
   liner)   sorteditemscolumnr   r	   )r   r   	_line_numschema_marksr#   r   r   r   _get_nested_path_prefixS   s   
z2_CustomSafeLoaderWithMarks._get_nested_path_prefixc           	         sx   t  |}| |}|jD ]+\}}| |j }|jj}t||j|j}|| jvr1|g| j|< q| j| 	| q|S r   )
r   construct_mappingr4   valuer
   r.   r   r   r)   r   )	r   r   mappingnested_path_prefixkey_node
value_nodenode_key_pathline_numr#   r   r   r   r5   c   s   


z,_CustomSafeLoaderWithMarks.construct_mappingFc              
      s   t  j|dd}| |}t|jD ]j\}}|jj}| | }t||j|j}	|| j	vr4|	g| j	|< q||jjkrC| j	| 
|	 qt||jjD ]1}
|
| j	v r{| j	|
 }t||}|
|krv|d j|krv|dt||d j|d j || j	|
< qJq|S )NT)deepr   r,   )r   construct_sequencer4   	enumerater6   r
   r.   r   r   r)   r   ranger&   r	   insert)r   r   r=   sequencer8   indexsequence_itemr<   r;   r!   
inner_liner3   r"   r   r   r   r>   r   sD   





z-_CustomSafeLoaderWithMarks.construct_sequencec                    s8   t   }t|trtdd t| j  D |d< |S )Nc                 S   s   g | ]}|j |jjd  fqS )r   )r	   r
   r.   ).0vr   r   r   
<listcomp>   s    z>_CustomSafeLoaderWithMarks.get_single_data.<locals>.<listcomp>schemamarks)r   get_single_data
isinstancedictr   r)   valuesr   datar   r   r   rJ      s   

z*_CustomSafeLoaderWithMarks.get_single_data)F)
r   r   r   __doc__r(   r4   r5   r>   rJ   r   r   r   r   r   r'   1   s    &r'   z tag:yaml.org,2002:python/unicodec                   @   s   e Zd ZdZdd ZdS )NoAliasSafeDumperz>A class which avoids constructing anchors/aliases on yaml dumpc                 C   s   dS )NTr   rN   r   r   r   ignore_aliases   s   z NoAliasSafeDumper.ignore_aliasesN)r   r   r   rP   rR   r   r   r   r   rQ      s    rQ   returnc                 C   s6   t j| td}t|tsi }||fS |d}||fS )a  Perform YAML SafeLoad and track start and end marks during parse.

    JSON schema errors come with an encoded object path such as:
        <key1>.<key2>.<list_item_index>

    YAML loader needs to preserve a mapping of schema path to line and column
    marks to annotate original content with JSON schema error marks for the
    command:
        cloud-init devel schema --annotate


    LoaderrI   )yamlloadr'   rK   rL   pop)blobresultrI   r   r   r   load_with_marks   s   

r[   c                 C   s   t j| tdS )NrT   )rV   rW   r   )rY   r   r   r   rW      s   rW   TFc              	   C   s(   t j| dd||d|rtdS t jjdS )z%Return data in nicely formatted yaml.
   F)
line_breakindentexplicit_startexplicit_enddefault_flow_styleDumper)rV   dumprQ   dumper
SafeDumper)objr`   ra   noaliasr   r   r   dumps   s   ri   )TTF)collectionsr   	itertoolsr   typingr   r   r   r   rV   	YAMLErrorr   
SafeLoaderr   r&   r'   add_constructorr   re   rf   rQ   strintr[   rW   ri   r   r   r   r   <module>   s&   s