U
    Ӈgm(                     @   s   d dl Z 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
d dlmZ e eZG dd dZdd	 Zee d
ddZeee ee dddZG dd de
jZG dd de
jjZede	eeeef f dddZeddddZdS )    N)defaultdict)chain)AnyDictListTuple)performancec                   @   s2   e Zd ZeejejdddZdd Zdd ZdS )	SchemaPathMarkspath
start_markend_markc                 C   s   || _ || _|| _d S Nr
   )selfr   r   r    r   4/usr/lib/python3/dist-packages/cloudinit/safeyaml.py__init__   s    zSchemaPathMarks.__init__c                 C   sp   |j j| j jk s |jj| jjkr$dS |j j| j jkrH|j j| j jk rHdS |jj| jjkrl|jj| jjkrldS dS )z;Return whether other start/end marks are within self marks.FT)r   liner   columnr   otherr   r   r   __contains__   s     zSchemaPathMarks.__contains__c                 C   s@   | j j|j jko>| j j|j jko>| jj|jjko>| jj|jjkS r   )r   r   r   r   r   r   r   r   __eq__/   s    zSchemaPathMarks.__eq__N)	__name__
__module____qualname__stryamlZMarkr   r   r   r   r   r   r   r	      s   r	   c                 C   s0   |d d d D ]}| |kr| |ks|  S qd S )Nr   )Z
child_markmarksmarkr   r   r   _find_closest_parent8   s    
r!   )
line_marksc                 C   s\   | D ]R}t || }|r|jdd\}}|j|jksB|j|js|j||jd|_qdS )zP
    Update any SchemaPathMarks.path for items not under the proper parent.
    .   N)r!   r   rsplit
startswithreplace)r"   r    parentZpath_prefixZ	_path_idxr   r   r   _reparent_schema_mark_children?   s    
r)   )new_markr   returnc                 C   sb   g }d}|D ]8}|| kr$| | q| |kr:d}| |  | | q|rTt| n
| |  |S )a=  Insert new_mark into marks, ordering ancestors first.

    Reparent existing SchemaPathMarks.path when new_mark is a parent of
    an existing mark item.

    Because schema processing is depth first, leaf/child mappings and
    sequences may be processed for SchemaPathMarks before their parents.
    This leads to SchemaPathMarks.path of 'grandchildren' being incorrectly
    parented by the root dictionary instead of an intermediary parents below
    root.

    Walk through the list of existing marks and reparent marks that are
    contained within the new_mark.
    FT)appendr)   )r*   r   	new_marksZreparent_pathsr    r   r   r   _add_mark_and_reparent_marksN   s    



r.   c                       sP   e Zd ZdZ fddZdd Zd 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  | tt| _d S r   )superr   r   listschemamarks_by_line)r   stream	__class__r   r   r      s    z#_CustomSafeLoaderWithMarks.__init__c                 C   s   |j j| jkrb| j|j j d }| j|j j dd  D ]}||kr6||kr6|}q6||krb|jd S t| j ddD ]8\}}|d d d D ] }||kr|j d    S qqtdS )Nr   r$   r#   T)reverser    )r   r   r2   r   sorteditems)r   nodeZmost_specific_markZ	path_markZ	_line_numschema_marksr    r   r   r   _get_nested_path_prefix   s*    
 z2_CustomSafeLoaderWithMarks._get_nested_path_prefixFc                    sr   t  j||d}| |}|jD ]L\}}| |j }|jj}t||j|j}	| j| }
t	|	|
}|| j|< q |S )Ndeep)
r0   construct_mappingr<   valuer   r   r	   r   r2   r.   )r   r:   r>   mappingnested_path_prefixZkey_nodeZ
value_nodenode_key_pathline_numr*   r;   r-   r4   r   r   r?      s    
  

z,_CustomSafeLoaderWithMarks.construct_mappingc              
      s  t  j|dd}| |}t|jD ]\}}|jj}| | }t||j|j}	|| j	krh|	g| j	|< q$||jjkr| j	| }
t
|	|
}|| j	|< q$t||jjD ]b}|| j	kr| j	| }
t
|	|
}||kr|
d j|kr|dt||
d j|
d j || j	|< qq$|S )NTr=   r   r   )r0   construct_sequencer<   	enumerater@   r   r   r	   r   r2   r.   ranger   insert)r   r:   r>   ZsequencerB   indexZsequence_itemrD   rC   r*   r;   r-   Z
inner_liner4   r   r   rE      sV    
  

  

 z-_CustomSafeLoaderWithMarks.construct_sequencec                    s8   t   }t|tr4t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)r0   get_single_data
isinstancedictr   r2   valuesr   datar4   r   r   rN      s    

z*_CustomSafeLoaderWithMarks.get_single_data)F)F)
r   r   r   __doc__r   r<   r?   rE   rN   __classcell__r   r   r4   r   r/   q   s   *r/   c                   @   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   rR   r   r   r   ignore_aliases   s    z NoAliasSafeDumper.ignore_aliasesN)r   r   r   rT   rW   r   r   r   r   rV      s   rV   zLoading yaml)r+   c                 C   s0   t j| td}t|tsi }n
|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


    )LoaderrM   )r   loadr/   rO   rP   pop)ZblobresultrM   r   r   r   load_with_marks   s
    

r\   zDumping yamlTFc              	   C   s$   t j| dd||d|rtnt jjdS )z%Return data in nicely formatted yaml.
   F)Z
line_breakindentexplicit_startexplicit_endZdefault_flow_styleZDumper)r   dumprV   dumper
SafeDumper)objr`   ra   Znoaliasr   r   r   dumps  s    rf   )TTF)Zloggingcollectionsr   	itertoolsr   typingr   r   r   r   r   Z	cloudinitr   Z	getLoggerr   ZLOGr	   r!   r)   r.   Z
SafeLoaderr/   rc   rd   rV   Ztimedr   intr\   rf   r   r   r   r   <module>   s&   
" #} 