o
    !d$                     @   s0   d dl Z d dlZddlmZ G dd deZdS )    N   )SectionNotFoundErrorc                   @   sj   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 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.

        Z__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   Jusr/lib/python3.10/site-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   st   t |d+}|dtj | s	 W d    dS |dtj | }|dkW  d    S 1 s3w   Y  d S )Nrbr   F   
)r   seekr
   SEEK_ENDtellread)r   filenamer   lastr   r   r   _check_file_needs_newlineO   s   $z*ConfigFileWriter._check_file_needs_newlinec                 C   sz   |  |}t|d)}|r|d |d|  g }| jd||d |d| W d    d S 1 s6w   Y  d S )Na
z[%s]
r   line_numberr   r   r   )r-   r   r   _insert_new_valuesr   )r   r   r   r   Zneeds_newliner   r   r   r   r   r   Z   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_startf   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   r0   z%s = %s
r%   r/   )copyr?   r6   r9   r:   r2   OPTION_REGEXgroupr7   
isinstancedict_update_subattributeslstripendswithappend)r   r   r   r   Zsection_start_line_numZlast_matching_linejr=   r>   key_nameoption_valuenew_liner   r   r   r   r   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
     )	r5   r6   rA   r:   rB   rF   r7   r9   r2   )r   indexr   valuesZstarting_indentr<   r=   r>   Zcurrent_indentrJ   rK   rL   r   r   r   rE      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 )NrO   z%s%s =
rM   r   r   )listitemsrC   rD   rH   insertr   )r   r1   r   r   indentZnew_contentskeyvalueZ	subindentsubkeyZsubvalr   r   r   r2      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 )NrN   r   z[%s]r   z	[%s "%s"])r   rB   r6   r   )r   r>   r   partsZunquoted_matchZquoted_matchr   r   r   r;      s   

z!ConfigFileWriter._matches_sectionN)r   )__name__
__module____qualname__recompiler9   rA   r   r   r-   r   r?   r   rE   r2   r;   r   r   r   r   r      s    
+	1
r   )r
   r]   r   r   objectr   r   r   r   r   <module>   s   