U
    e                     @   sr   d Z ddlmZmZ ddlmZ ddlZddlmZm	Z	 G dd deZeeejZd	d
 Zdd ZdddZdS )z6
some helper functions that might be generally useful
    )absolute_importprint_function)partialN   )	text_typebinary_typec                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	LazyEvala  
    Lightweight wrapper around lazily evaluated func(*args, **kwargs).

    func is only evaluated when any attribute of its return value is accessed.
    Every attribute access is passed through to the wrapped value.
    (This only excludes special cases like method-wrappers, e.g., __hash__.)
    The sole additional attribute is the lazy_self function which holds the
    return value (or, prior to evaluation, func and arguments), in its closure.
    c                    s$    fdd}t d| d S )Nc                      s$    t d fdd  S )N	lazy_selfc                      s    S N r   Zreturn_valuer   /lib/python3.8/site-packages/ruamel_yaml/util.py<lambda>#       z6LazyEval.__init__.<locals>.lazy_self.<locals>.<lambda>object__setattr__r   argsfunckwargsselfr   r   r	       s    
z$LazyEval.__init__.<locals>.lazy_selfr	   r   )r   r   r   r   r	   r   r   r   __init__   s    zLazyEval.__init__c                 C   s$   t | d}|dkr|S t| |S )Nr	   )r   __getattribute__getattr)r   namer	   r   r   r   r   (   s    zLazyEval.__getattribute__c                 C   s   t |  || d S r
   )setattrr	   )r   r   valuer   r   r   r   /   s    zLazyEval.__setattr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   

r   c                 K   st  ddl m} dd }t| tr$| }nt| tr:| d}n|  }d}d}d}d}d}	| D ]}
|
 }|	 }|
dr||
}||	 }|d }|
| d	kr|d7 }q|
| d
krq^||	 } qJ|dkr|dk	r|rd}|
| dkr|d7 }q||kr|| }|drD||
}	d}|
| d	kr>|d7 }q$|}q^d}q^|dkrb|dk	rb|}||f|||fS )at  guess the indent and block sequence indent of yaml stream/string

    returns round_trip_loaded stream, indent level, block sequence indent
    - block sequence indent is the number of spaces before a dash relative to previous indent
    - if there are no block sequences, indent is taken from nested mappings, block sequence
      indent is unset (None) in that case
    r   )round_trip_loadc                 S   s*   d}|t | k r&| | dkr&|d7 }q|S )Nr    r   )len)lidxr   r   r   leading_spacesH   s    
z.load_yaml_guess_indent.<locals>.leading_spaceszutf-8Nr   z- r#   #z -:)mainr"   
isinstancer   r   decoderead
splitlinesrstriplstrip
startswithendswith)streamkwr"   r'   Zyaml_strZ
map_indentindentZblock_seq_indentZprev_line_key_onlyZ
key_indentlineZrlineZllineZl_sr&   r   r   r   load_yaml_guess_indent<   sT    	





r7   c                 c   sn   ddl m} t| |st| jD ]}| r |V  q t| D ]}| r<|V  q<| jD ]}| rV|V  qVdS )zv
    walks over a ConfigObj (INI file with comments) generating
    corresponding YAML output (including comments
    r   )	ConfigObjN)	configobjr8   r+   AssertionErrorZinitial_commentstrip_walk_sectionZfinal_comment)Zcfgr8   csr   r   r   configobj_walkerz   s    

r?   c           
      c   sJ  ddl m} t| |std| }| jD ]}| j| D ]}||  V  q6| | }d|kr|d }d| | dd|  }nd|krd|dd d }d	|||}| j	| }|r|d
| 7 }|V  q(| j
D ]r}| j| D ]}||  V  qd||}| j	| }|r|d
| 7 }|V  t| | |d dD ]}	|	V  q6qd S )Nr   )Sectionz  
z|
r)   'z''z{0}{1}: {2}r#   z{0}{1}:r   )level)r9   r@   r+   r:   ZscalarsZcommentsr;   replaceformatZinline_commentsZsectionsr<   )
r>   rC   r@   r5   r   r=   xir6   valr   r   r   r<      s6    



r<   )r   )r!   Z
__future__r   r   	functoolsr   recompatr   r   ZAnyZDictZOptionalZListZTextZStreamTextTyper   r   compileZRegExpr7   r?   r<   r   r   r   r   <module>   s   !>