o
                         @   st   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_typeF)AnyDictOptionalListText)StreamTextTypec                   @   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(   i  t d fdd  S )N	lazy_selfc                      s    S N r   return_valuer   9/tmp/google-cloud-sdk/lib/third_party/ruamel/yaml/util.py<lambda>#   s    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   s  ddl m} dd }t| tr| }nt| tr| d}n|  }d}d}d}d}d}	| D ]|}
|
 }|	 }|
drg||
}||	 }|d }|
| d	krZ|d7 }|
| d	ksP|
| d
kraq/||	 } nE|du r|dur|rd}|
| dv r|d7 }|
| dv sy||kr|| }|dr||
}	d}|
| d	kr|d7 }|
| d	ks|}q/d}q/|du r|dur|}||fi |||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 }|t | k r| | dks|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-   yaml_str
map_indentindentblock_seq_indentprev_line_key_only
key_indentlinerlinellinel_sr,   r   r   r   load_yaml_guess_indent<   sZ   	



rE   c                 c   sp    ddl m} t| |sJ | jD ]	}| r|V  qt| D ]	}| r(|V  q| jD ]	}| r5|V  q,dS )zv
    walks over a ConfigObj (INI file with comments) generating
    corresponding YAML output (including comments
    r   )	ConfigObjN)	configobjrF   r1   initial_commentstrip_walk_sectionfinal_comment)cfgrF   csr   r   r   configobj_walkerz   s    

rO   c           
      c   sH   ddl m} t| |sJ d| }| jD ]Q}| j| D ]	}||  V  q| | }d|v rA|d }d| | dd|  }nd|v rOd|dd d }d	|||}| j| }|rc|d
| 7 }|V  q| j	D ]7}| j| D ]	}||  V  qqd||}| j| }|r|d
| 7 }|V  t
| | |d dD ]}	|	V  qqjd S )Nr   )Sectionz  
z|
r/   'z''z{0}{1}: {2}r)   z{0}{1}:r   )level)rG   rP   r1   scalarscommentsrI   replaceformatinline_commentssectionsrJ   )
rN   rS   rP   r=   r!   rM   xirA   valr   r   r   rJ      s<   



rJ   )r   )r'   
__future__r   r   	functoolsr   recompatr   r   typingr   r	   r
   r   r   r   r   r   compileRegExprE   rO   rJ   r   r   r   r   <module>   s   !>