3
՛gY&                 @   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               @   sv   e Z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 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| _| j  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   ,/usr/lib/python3/dist-packages/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 jd|  | j| d S )N
)r   writer   )r   r   r   r   r   write_blanks5   s    zPythonPrinter.write_blanksc             C   s6   d| _ x*tjd|D ]}| jj| | jd qW dS )zprint 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?\nr   N)r   resplitr
   appendr   )r   blocklr   r   r   write_indented_block9   s    z"PythonPrinter.write_indented_blockc             G   s   x|D ]}| j | qW dS )z"print a series of lines of python.N)	writeline)r   linesliner   r   r   
writelinesC   s    
zPythonPrinter.writelinesc             C   sV  | j s| j  d| _ |dks4tjd|s4tjd|r:d}nd}|oTt|oT|d dk}| r| sl| j|r| jdkr|  jd8  _t| jdkrtj	d	| jj
  |dkrdS | jj| j|d
  | jt|jd
 tjd|rRtjd|}|r"|jd}|  jd7  _| jj| n0d}tjd|}|rR|  jd7  _| jj| dS )zprint 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   H   sB    	


zPythonPrinter.writelinec             C   s   | j   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jd|}|s<dS dS )zqreturn true if the given line is an 'unindentor',
        relative to the last 'indent' event received.

        r   Fr   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 jd| | j| j |S )zindent 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.FN)FF)backslashedtriplequoted)r   r   r   r   r      s    z%PythonPrinter._reset_multi_line_flagsc             C   sX   | j p
| j}tjd|r d| _ nd| _ ttj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   )r2   r3   r   r)   r%   findall)r   r    Zcurrent_stateZtriplesr   r   r   _in_multi_line   s    

zPythonPrinter._in_multi_linec             C   s   d }| j   xp| jD ]f}| j|r4| jj|d  q|j }|d krbtjd|rbtjd|j	d}| jj| j
||d  qW g | _| j   d S )Nr   z^[ \t]*[^# \t]z	^([ \t]*)r   )r   r
   r6   r   r   
expandtabsr   r)   r$   r*   r(   )r   r1   entryr   r   r   r#      s    
z#PythonPrinter._flush_adjusted_linesN)r/   )__name__
__module____qualname__r   r   r   r   r   r!   r   r-   r&   r(   r   r6   r#   r   r   r   r   r      s   
I+
	r   c                s   ddgd\  fdd}ddd}g }d	}xft jd
| D ]V}||rX|j| q@|j }|d	krt jd|rt jd|jd}|j||| q@W dj|S )z<remove the left-whitespace margin of a block of Python code.Fr   r   c                s     p }t jd| r&d < nd < dd }x| rȈ r|d  | \}} |rhd< q|d  | \}} q8|d| \}} |r|S |d	| \}} |r|jd
< q8|d| \}} q8W |S )Nz\\$TFc             S   s6   t j| |}|r*||t|jdd  fS d |fS d S )Nr   )r   r$   r%   r*   )Z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=   )r2   stater3   r   r   in_multi_line   s(    

z(adjust_whitespace.<locals>.in_multi_liner/   c             S   s   t jd| d| S )Nz^%sr/   )r   r0   )r    r1   r   r   r   r(   !  s    z'adjust_whitespace.<locals>._indent_lineNz\r?\nz^[ \t]*[^# \t]z	^([ \t]*)r   )r   r   )r/   )r   r   r   r7   r)   r$   r*   join)textr?   r(   r   r1   r    r   )r2   r>   r3   r   adjust_whitespace   s    $
rB   )__doc__r   Zmakor   objectr   rB   r   r   r   r   <module>   s
    k