o
    .&ßaž"  ã                   @   s0   d dl Z d dlZddlmZ G dd„ deƒZdS )é    Né   )ÚSectionNotFoundErrorc                   @   sb   e Zd Ze d¡Ze d¡Zdd„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zddd„Zdd„ ZdS )ÚConfigFileWriterz^\s*\[(?P<header>[^]]+)\]z7(?P<option>[^:=][^:=]*)\s*(?P<vi>[:=])\s*(?P<value>.*)$c                 C   sÜ   |  dd¡}tj |¡s|  |¡ |  |||¡ dS t|dƒ�}| ¡ }W d  ƒ n1 s.w   Y  z)|  |||¡ t|dƒ�}| 	d 
|¡¡ W d  ƒ W dS 1 sUw   Y  W dS  tym   |  |||¡ Y dS w )aþ  Update config file with new values.

        This method will update a section in a config file with
        new key value pairs.

        This method provides a few conveniences:

        * If the ``config_filename`` does not exist, it will
          be created.  Any parent directories will also be created
          if necessary.
        * If the section to update does not exist, it will be created.
        * Any existing lines that are specified by ``new_values``
          **will not be touched**.  This ensures that commented out
          values are left unaltered.

        :type new_values: dict
        :param new_values: The values to update.  There is a special
            key ``__section__``, that specifies what section in the INI
            file to update.  If this key is not present, then the
            ``default`` section will be updated with the new values.

        :type config_filename: str
        :param config_filename: The config filename where values will be
            written.

        Ú__section__ÚdefaultNÚrÚwÚ )ÚpopÚosÚpathÚisfileÚ_create_fileÚ_write_new_sectionÚopenÚ	readlinesÚ_update_section_contentsÚwriteÚjoinr   )ÚselfÚ
new_valuesÚconfig_filenameÚsection_nameÚfÚcontents© r   úH/usr/lib/python3/dist-packages/awscli/customizations/configure/writer.pyÚupdate_config   s    

ÿ&ÿÿzConfigFileWriter.update_configc                 C   sj   t j |¡d }t j |¡st  |¡ t  t  |t jt jB d¡d¡�	 W d   ƒ d S 1 s.w   Y  d S )Nr   i€  r   )	r   r   ÚsplitÚisdirÚmakedirsÚfdopenr   ÚO_WRONLYÚO_CREAT)r   r   Údirnamer   r   r   r   F   s   

ÿÿ"þzConfigFileWriter._create_filec                 C   sb   t |dƒ�"}| d| ¡ g }| jd||d� | d |¡¡ W d   ƒ d S 1 s*w   Y  d S )NÚaz[%s]
r   ©Úline_numberr   r   r	   )r   r   Ú_insert_new_valuesr   )r   r   r   r   r   r   r   r   r   r   O   s   þ"úz#ConfigFileWriter._write_new_sectionc                 C   sZ   t t|ƒƒD ]"}|| }| ¡  d¡rq| j |¡}|d ur(|  ||¡r(|  S qt|ƒ‚)N)ú#ú;)ÚrangeÚlenÚstripÚ
startswithÚSECTION_REGEXÚsearchÚ_matches_sectionr   )r   r   r   ÚiÚlineÚmatchr   r   r   Ú_find_section_startX   s   ÿ€z$ConfigFileWriter._find_section_startc              
   C   s8  |  ¡ }|  ||¡}|}|d }|t|ƒk r€|| }| j |¡d ur,| j|||d� d S | j |¡}|d urv|}| d¡ ¡ }	|	|v rvt	||	 t
ƒs\||	 }
d|	|
f }|||< ||	= n|  ||||	 t| d¡ƒt| d¡ ¡ ƒ ¡}d S |d7 }|t|ƒk s|rš|d  d¡sŽ| d¡ | j|d ||d� d S d S )Nr   r&   z%s = %s
éÿÿÿÿÚ
)Úcopyr5   r,   r/   r0   r(   ÚOPTION_REGEXÚgroupr-   Ú
isinstanceÚdictÚ_update_subattributesÚlstripÚendswithÚappend)r   r   r   r   Úsection_start_line_numÚlast_matching_lineÚjr3   r4   Úkey_nameÚoption_valueÚnew_liner   r   r   r   d   sN   ÿþ
þå


þýz)ConfigFileWriter._update_section_contentsc                 C   sä   |d7 }t |t|ƒƒD ]X}|| }| j |¡}|d urJt| d¡ƒt| d¡ ¡ ƒ }| d¡ ¡ }	|	|v rJ||	 }
dd| |	|
f }|||< ||	= ||ksV| j |¡d urc|  |d ||d¡  |S q||krp|  |||d¡ |S )Nr   ú
%s%s = %s
ú ú    )	r+   r,   r9   r0   r:   r>   r-   r/   r(   )r   Úindexr   ÚvaluesÚstarting_indentr2   r3   r4   Úcurrent_indentrD   rE   rF   r   r   r   r=   •   s6   ÿÿÿ÷z&ConfigFileWriter._update_subattributesr	   c                 C   sž   g }t | ¡ ƒD ]9\}}t|tƒr4|d }| d||f ¡ t | ¡ ƒD ]\}	}
| d||	|
f ¡ q$n
| d|||f ¡ ||= q| |d d |¡¡ d S )NrI   z%s%s =
rG   r   r	   )ÚlistÚitemsr;   r<   r@   Úinsertr   )r   r'   r   r   ÚindentÚnew_contentsÚkeyÚvalueÚ	subindentÚsubkeyÚsubvalr   r   r   r(   °   s   


ÿÿz#ConfigFileWriter._insert_new_valuesc                 C   s\   |  d¡}| d¡d| k}t|ƒdkr,| d¡d|d d |dd … ¡f k}|p+|S |S )NrH   r   z[%s]r   z	[%s "%s"])r   r:   r,   r   )r   r4   r   ÚpartsÚunquoted_matchÚquoted_matchr   r   r   r1   ¾   s   

ÿz!ConfigFileWriter._matches_sectionN)r	   )Ú__name__Ú
__module__Ú__qualname__ÚreÚcompiler/   r9   r   r   r   r5   r   r=   r(   r1   r   r   r   r   r      s    
ÿ+		1
r   )r   r^   r	   r   Úobjectr   r   r   r   r   Ú<module>   s   