B
    b                 @   s   d Z 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ZdZdd	 Zd
d Zdd Zdd Zdd ZddgZeddg ZejjejjeeedeedddZeddg ZejjejjeeedejjjedddgdZd d! Zd"d# ZdS )$a  
Textfile/Substfile builder for SCons.

    Create file 'target' which typically is a textfile.  The 'source'
    may be any combination of strings, Nodes, or lists of same.  A
    'linesep' will be put between any part written and defaults to
    os.linesep.

    The only difference between the Textfile builder and the Substfile
    builder is that strings are converted to Value() nodes for the
    former and File() nodes for the latter.  To insert files in the
    former or strings in the latter, wrap them in a File() or Value(),
    respectively.

    The values of SUBST_DICT first have any construction variables
    expanded (its keys are not expanded).  If a value of SUBST_DICT is
    a python callable function, it is called and the result is expanded
    as the value.  Values are substituted in a "random" order; if any
    substitution could be further expanded by another substitution, it
    is unpredictable whether the expansion will occur.
    N)Node)Value)	is_Stringis_Sequenceis_Dictto_bytesw
c             C   sb   |   }|r*x|D ]\}}|||}qW dtkr^yt|d}W n tk
r\   t|}Y nX |S )a  
    Fetch the node contents and replace all instances of the keys with
    their values.  For example, if subs is
        {'%VERSION%': '1.2345', '%BASE%': 'MyProg', '%prefix%': '/bin'},
    then all instances of %VERSION% in the file will be replaced with
    1.2345 and so forth.
    bzutf-8)get_text_contentsreplaceTEXTFILE_FILE_WRITE_MODE	bytearray	TypeError)Znodesubscontentskval r   2lib/python3.7/site-packages/SCons/Tool/textfile.py	_do_subst;   s    r   c          
   C   s  |d }|d krt }n4t|r n*t|tr4| }ntjdt| d dt	krZt
|}d|krhd }n|d }t|rt| }nt|rntjdg }xL|D ]D\}}t|r| }t|r|j|dd}nt|}|||f qW yt| d  t	d	d
}W n@ ttfk
rH }	 ztjd| d |	f W d d }	~	X Y nX d }
x2|D ]*}|
rh||
 |t|| |}
qTW |  d S )NLINESEPARATORz+unexpected type/class for LINESEPARATOR: %sr
   
SUBST_DICTz#SUBST_DICT must be dict or sequence   )rawr    )newlinezCan't write target file %s [%s])LINESEPr   
isinstancer   r   SConsErrors	UserErrorreprr   r   r   listitemsr   callableZsubststrappendopenZget_pathOSErrorIOErrorwriter   close)targetsourceenvlinesepr   Z
subst_dictr   valueZtarget_fileeZlsepliner   r   r   _actionR   sL    

*


r4   c             C   s   d| d  S )NzCreating '%s'r   r   )r-   r.   r/   r   r   r   _strfunc   s    r5   c             C   sJ   xD|D ]<}t |rt| | qt|tr4| | q| t| qW d S )N)r   _convert_list_Rr   r   r'   r   )newlistZsourceselemr   r   r   r6      s    

r6   c             C   s.   t | dkrtjdg }t|| | |fS )Nr   zOnly one target file allowed)lenr   r    r!   r6   )r-   r.   r/   r7   r   r   r   _convert_list   s
    
r:   r   r   TEXTFILEPREFIXTEXTFILESUFFIX)Zvarlistz$TEXTFILEPREFIXz$TEXTFILESUFFIX)actionsource_factoryemitterprefixsuffixSUBSTFILEPREFIXSUBSTFILESUFFIXz$SUBSTFILEPREFIXz$SUBSTFILESUFFIXz.in)r=   r>   r?   r@   rA   Z
src_suffixc             C   sD   t | d< t| d d< d| d< d| d< t| d d< d| d	< d| d
< d S )Nr   ZBUILDERSZTextfiler   r;   z.txtr<   Z	SubstfilerB   rC   )r   _text_builder_subst_builder)r/   r   r   r   generate   s    rF   c             C   s   dS )Nr   r   )r/   r   r   r   exists   s    rG   )__doc__r   Z
SCons.Noder   ZSCons.Node.Pythonr   Z
SCons.Utilr   r   r   r   r   r   r   r4   r5   r6   r:   Z_common_varlistZ_text_varlistZBuilderZActionrD   Z_subst_varlistZFSZFilerE   rF   rG   r   r   r   r   <module>,   s:   7


