a
    e	g°(  ã                   @   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   úM/var/www/html/llm_bihealth/app/venv/lib/python3.9/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vr|| j| j < d S r   )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   Úwrite_blanks>   s    zPythonPrinter.write_blanksNc                 C   sP   d| _ tt d|¡ƒD ]4\}}| j |¡ |dur@|  || ¡ |  d¡ q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.Fú\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   |D ]}|   |¡ q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u 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u 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_commentr0   ÚindentorÚm2r   r   r   r*   S   sN    	ÿ
þ
ýÿþþý


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u 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)r1   r   r   r0   Úbool)r   r,   r7   r0   r   r   r   r2   ž   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.r=   ú^%s)r   r   r   Úsub)r   r,   Ú
stripspacer   r   r   r4   Æ   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Útriplequotedr9   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.ú\\$ú\"\"\"|\'\'\'r   é   r   )rA   rB   r<   r   r5   r1   Úfindall)r   r,   Zcurrent_stateZtriplesr   r   r   Ú_in_multi_lineÙ   s    

zPythonPrinter._in_multi_linec                 C   sŒ   d }|   ¡  | jD ]f}|  |¡r2| j |d ¡ q| ¡ }|d u r`t d|¡r`t d|¡ 	d¡}| j |  
||¡d ¡ qg | _|   ¡  d S )Nr   ú^[ \t]*[^# \t]ú	^([ \t]*)r   )r   r
   rG   r   r    Ú
expandtabsr   r5   r0   r6   r4   )r   r@   Úentryr   r   r   r/   ì   s    

z#PythonPrinter._flush_adjusted_lines)N)r=   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r!   r)   r-   r*   r:   r2   r4   r   rG   r/   r   r   r   r   r      s   (
G(
r   c                    s    ddg‰d\‰ ‰‡ ‡‡fdd„}ddd„}g }d}t  d	| ¡D ]V}||ƒrV| |¡ q>| ¡ }|du r„t  d
|¡r„t  d|¡ d¡}| |||ƒ¡ q>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„ }| rÆˆˆ r~|dˆˆ  | ƒ\}} |rfdˆˆ< qÄ|dˆˆ  | ƒ\}} q6|d| ƒ\}} |r”|S |d	| ƒ\}} |r¶| d
¡ˆˆ< q6|d| ƒ\}} q6|S )NrC   TFc                 S   s6   t  | |¡}|r*||t| d¡ƒd … fS d |fS d S )Nr   )r   r0   r1   r6   )ÚregÚtÚmr   r   r   r0     s    z7adjust_whitespace.<locals>.in_multi_line.<locals>.matchz%sz.*?(?=%s|$)r.   rD   r   z.*?(?=\"\"\"|\'\'\'|#|$))r   r5   r6   )r,   Zstart_stater0   rQ   ©rA   ÚstaterB   r   r   Úin_multi_line  s(    

z(adjust_whitespace.<locals>.in_multi_liner=   c                 S   s   t  d| d| ¡S )Nr>   r=   )r   r?   )r,   r@   r   r   r   r4   '  s    z'adjust_whitespace.<locals>._indent_lineNr"   rH   rI   r   r   )r=   )r   r$   r%   rJ   r5   r0   r6   Újoin)ÚtextrT   r4   r+   r@   r,   r   rR   r   Úadjust_whitespaceý   s    $
rW   )Ú__doc__r   Zmakor   r   rW   r   r   r   r   Ú<module>   s
    p