B
    ×‹d°(  ã               @   s2   d Z ddlZddlmZ G dd„ dƒZdd„ ZdS )z<utilities for generating and formatting literal Python code.é    N)Ú
exceptionsc               @   sx   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zdd
d„Zdd„ Zdd„ Z	dd„ Z
dd„ Zddd„Zdd„ Zdd„ Zdd„ Zd	S )ÚPythonPrinterc             C   s„   d| _ g | _d| _|| _d| _g | _d| _|  ¡  i | _t	 
d¡| _t	 
d¡| _t	 
d¡| _t	 
d¡| _t	 
d	¡| _t	 
d
¡| _d S )Nr   z    é   Fz^\s*#z^\s*$z:[ \t]*(?:#.*)?$z ^\s*(if|try|elif|while|for|with)z(^\s*(def|class|else|elif|except|finally)z"^\s*(else|elif|except|finally).*\:)ÚindentÚindent_detailÚindentstringÚstreamÚlinenoÚline_bufferÚin_indent_linesÚ_reset_multi_line_flagsÚ
source_mapÚreÚcompileÚ_re_space_commentÚ	_re_spaceÚ
_re_indentÚ_re_compoundÚ_re_indent_keywordÚ_re_unindentor)Úselfr   © r   úW/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/mako/pygen.pyÚ__init__   s     zPythonPrinter.__init__c             C   s   |  j |7  _ d S )N)r	   )r   Únumr   r   r   Ú_update_lineno7   s    zPythonPrinter._update_linenoc             C   s   | j | jkr|| j| j < d S )N)r	   r   )r   r	   r   r   r   Ústart_source:   s    zPythonPrinter.start_sourcec             C   s   | j  d| ¡ |  |¡ d S )NÚ
)r   Úwriter   )r   r   r   r   r   Úwrite_blanks>   s    zPythonPrinter.write_blanksNc             C   sT   d| _ xHtt d|¡ƒD ]4\}}| j |¡ |dk	rB|  || ¡ |  d¡ qW dS )zµprint a line or lines of python which already contain indentation.

        The indentation of the total block of lines will be adjusted to that of
        the current indent level.Fz\r?\nNr   )r   Ú	enumerater   Úsplitr
   Úappendr   r   )r   ÚblockZstarting_linenoÚiÚlr   r   r   Úwrite_indented_blockB   s    z"PythonPrinter.write_indented_blockc             G   s   x|D ]}|   |¡ qW dS )z"print a series of lines of python.N)Ú	writeline)r   ÚlinesÚliner   r   r   Ú
writelinesN   s    
zPythonPrinter.writelinesc             C   sR  | j s|  ¡  d| _ |dks4| j |¡s4| j |¡r:d}nd}|oTt|ƒoT|d dk}|s¢|rh|  |¡r¢| jdkr¢|  jd8  _t| jƒdkr˜t	 
d¡‚| j ¡  |dkr®dS | j |  |¡d ¡ |  t| d¡ƒ¡ | j |¡rN| j |¡}|r| d¡}|  jd7  _| j |¡ n0d}| j |¡}|rN|  jd7  _| j |¡ dS )	z¾print a line of python, indenting it according to the current
        indent level.

        this also adjusts the indentation counter according to the
        content of the line.

        TNFr   ú#r   zToo many whitespace closuresr   )r   Ú_flush_adjusted_linesr   Úmatchr   ÚlenÚ_is_unindentorr   r   r   ZMakoExceptionÚpopr   r   Ú_indent_liner   r!   r   Úsearchr   Úgroupr"   r   )r   r)   ZhastextZ
is_commentr-   ÚindentorÚm2r   r   r   r'   S   s>    	



zPythonPrinter.writelinec             C   s   |   ¡  dS )z1close this printer, flushing any remaining lines.N)r,   )r   r   r   r   Úcloseš   s    zPythonPrinter.closec             C   s<   t | jƒdkrdS | jd }|dkr(dS | j |¡}t|ƒS )zqreturn true if the given line is an 'unindentor',
        relative to the last 'indent' event received.

        r   FéÿÿÿÿN)r.   r   r   r-   Úbool)r   r)   r4   r-   r   r   r   r/   ž   s    
zPythonPrinter._is_unindentorÚ c             C   s2   |dkr| j | j | S t d| | j | j |¡S )z±indent the given line according to the current indent level.

        stripspace is a string of space that will be truncated from the
        start of the line before indenting.r9   z^%s)r   r   r   Úsub)r   r)   Ú
stripspacer   r   r   r1   Æ   s    zPythonPrinter._indent_linec             C   s   d\| _ | _dS )z^reset the flags which would indicate we are in a backslashed
        or triple-quoted section.)FFN)ÚbackslashedÚtriplequoted)r   r   r   r   r   Ó   s    z%PythonPrinter._reset_multi_line_flagsc             C   sP   | j p
| j}tt d|¡ƒ| _ tt d|¡ƒ}|dksB|d dkrL| j | _|S )zcreturn true if the given line is part of a multi-line block,
        via backslash or triple-quote.z\\$z\"\"\"|\'\'\'r   é   r   )r<   r=   r8   r   r2   r.   Úfindall)r   r)   Zcurrent_stateZtriplesr   r   r   Ú_in_multi_lineÙ   s    

zPythonPrinter._in_multi_linec             C   s   d }|   ¡  xp| jD ]f}|  |¡r4| j |d ¡ q| ¡ }|d krbt d|¡rbt d|¡ 	d¡}| j |  
||¡d ¡ qW g | _|   ¡  d S )Nr   z^[ \t]*[^# \t]z	^([ \t]*)r   )r   r
   r@   r   r   Ú
expandtabsr   r2   r-   r3   r1   )r   r;   Úentryr   r   r   r,   ì   s    
z#PythonPrinter._flush_adjusted_lines)N)r9   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r&   r*   r'   r6   r/   r1   r   r@   r,   r   r   r   r   r      s   (
G(
r   c                s¤   ddg‰d\‰ ‰‡ ‡‡fdd„}ddd„}g }d}xft  d	| ¡D ]V}||ƒrX| |¡ q@| ¡ }|dkr†t  d
|¡r†t  d|¡ d¡}| |||ƒ¡ q@W d |¡S )z<remove the left-whitespace margin of a block of Python code.F)r   r   c                sÎ   ˆˆ  pˆˆ }t  d| ¡r&dˆˆ < ndˆˆ < dd„ }x’| rÈˆˆ r€|dˆˆ  | ƒ\}} |rhdˆˆ< qÆ|dˆˆ  | ƒ\}} q8|d| ƒ\}} |r–|S |d	| ƒ\}} |r¸| d
¡ˆˆ< q8|d| ƒ\}} q8W |S )Nz\\$TFc             S   s6   t  | |¡}|r*||t| d¡ƒd … fS d |fS d S )Nr   )r   r-   r.   r3   )ÚregÚtÚmr   r   r   r-     s    z7adjust_whitespace.<locals>.in_multi_line.<locals>.matchz%sz.*?(?=%s|$)r+   z\"\"\"|\'\'\'r   z.*?(?=\"\"\"|\'\'\'|#|$))r   r2   r3   )r)   Zstart_stater-   rH   )r<   Ústater=   r   r   Úin_multi_line  s(    

z(adjust_whitespace.<locals>.in_multi_liner9   c             S   s   t  d| d| ¡S )Nz^%sr9   )r   r:   )r)   r;   r   r   r   r1   '  s    z'adjust_whitespace.<locals>._indent_lineNz\r?\nz^[ \t]*[^# \t]z	^([ \t]*)r   r   )r9   )r   r!   r"   rA   r2   r-   r3   Újoin)ÚtextrJ   r1   r(   r;   r)   r   )r<   rI   r=   r   Úadjust_whitespaceý   s    $
rM   )Ú__doc__r   Úmakor   r   rM   r   r   r   r   Ú<module>   s
    p