B
    ¿raY'  ã               @   s4   d Z ddlZddlmZ G dd„ deƒ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 | _d S )Nr   z    é   F)	ÚindentÚindent_detailÚindentstringÚstreamÚlinenoÚline_bufferÚin_indent_linesÚ_reset_multi_line_flagsÚ
source_map)Úselfr   © r   ú,/tmp/pip-install-0_xzzbcd/Mako/mako/pygen.pyÚ__init__   s    zPythonPrinter.__init__c             C   s   |  j |7  _ d S )N)r	   )r   Únumr   r   r   Ú_update_lineno.   s    zPythonPrinter._update_linenoc             C   s   | j | jkr|| j| j < d S )N)r	   r   )r   r	   r   r   r   Ústart_source1   s    zPythonPrinter.start_sourcec             C   s   | j  d| ¡ |  |¡ d S )NÚ
)r   Úwriter   )r   r   r   r   r   Úwrite_blanks5   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   Ú	enumerateÚreÚsplitr
   Úappendr   r   )r   ÚblockZstarting_linenoÚiÚlr   r   r   Úwrite_indented_block9   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   Ú
writelinesE   s    
zPythonPrinter.writelinesc             C   sR  | j s|  ¡  d| _ |dks4t d|¡s4t d|¡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
¡ƒ¡ t d|¡rNt d|¡}|r| d¡}|  jd7  _| j |¡ n0d}t d|¡}|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.

        TNz^\s*#z^\s*$Fr   ú#r   zToo many whitespace closuresr   z:[ \t]*(?:#.*)?$z ^\s*(if|try|elif|while|for|with)z(^\s*(def|class|else|elif|except|finally))r   Ú_flush_adjusted_linesr   ÚmatchÚlenÚ_is_unindentorr   r   r   ZSyntaxExceptionÚpopr   r   Ú_indent_liner   r   ÚsearchÚgroupr   )r   r"   ZhastextZ
is_commentr&   ÚindentorÚm2r   r   r   r    J   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 t d|¡}|s<dS dS )zqreturn true if the given line is an 'unindentor',
        relative to the last 'indent' event received.

        r   FéÿÿÿÿNz"^\s*(else|elif|except|finally).*\:T)r'   r   r   r&   )r   r"   r-   r&   r   r   r   r(   –   s    
zPythonPrinter._is_unindentorÚ c             C   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.z^%s)r   Úsubr   r   )r   r"   Ú
stripspacer   r   r   r*   Á   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   sX   | j p
| j}t d|¡r d| _ nd| _ tt d|¡ƒ}|dksJ|d dkrT| j | _|S )zcreturn true if the given line is part of a multi-line block,
        via backslash or triple-quote.z\\$TFz\"\"\"|\'\'\'r   é   r   )r4   r5   r   r+   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
   r8   r   r   Ú
expandtabsr   r+   r&   r,   r*   )r   r3   Úentryr   r   r   r%   è   s    
z#PythonPrinter._flush_adjusted_lines)N)r1   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r#   r    r/   r(   r*   r   r8   r%   r   r   r   r   r      s   
H+

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'   r,   )ÚregÚtÚmr   r   r   r&     s    z7adjust_whitespace.<locals>.in_multi_line.<locals>.matchz%sz.*?(?=%s|$)r$   z\"\"\"|\'\'\'r   z.*?(?=\"\"\"|\'\'\'|#|$))r   r+   r,   )r"   Zstart_stater&   r@   )r4   Ústater5   r   r   Úin_multi_lineÿ   s(    

z(adjust_whitespace.<locals>.in_multi_liner1   c             S   s   t  d| d| ¡S )Nz^%sr1   )r   r2   )r"   r3   r   r   r   r*   #  s    z'adjust_whitespace.<locals>._indent_lineNz\r?\nz^[ \t]*[^# \t]z	^([ \t]*)r   r   )r1   )r   r   r   r9   r+   r&   r,   Újoin)ÚtextrB   r*   r!   r3   r"   r   )r4   rA   r5   r   Úadjust_whitespaceù   s    $
rE   )Ú__doc__r   Zmakor   Úobjectr   rE   r   r   r   r   Ú<module>   s
    l