o
    èÇh°(  ã                   @   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   ú^/var/www/html/construction_image-detection-poc/venv/lib/python3.10/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   ó   zPythonPrinter._update_linenoc                 C   s    | j | jvr|| j| j < d S 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 ]\}}| 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   ÚblockÚ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   sV  | j s
|  ¡  d| _ |du s| j |¡s| j |¡rd}nd}|o*t|ƒo*|d dk}|sQ|r4|  |¡rQ| jdkrQ|  jd8  _t| jƒdkrLt	 
d¡‚| j ¡  |du rWdS | j |  |¡d ¡ |  t| d¡ƒ¡ | j |¡r§| j |¡}|rŽ| d¡}|  jd7  _| j |¡ dS d}| j |¡}|r©|  jd7  _| j |¡ dS dS 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   ÚMakoExceptionÚpopr   r!   Ú_indent_liner   r%   r   Úsearchr   Úgroupr&   r   )r   r.   ÚhastextÚ
is_commentr2   ÚindentorÚm2r   r   r   r,   S   sN   	
ÿ
þÿþþ



ízPythonPrinter.writelinec                 C   s   |   ¡  dS )z1close this printer, flushing any remaining lines.N)r1   ©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)r3   r   r   r2   Úbool)r   r.   r<   r2   r   r   r   r4   ž   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.rB   ú^%s)r   r   r   Úsub)r   r.   Ú
stripspacer   r   r   r7   Æ   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Útriplequotedr>   r   r   r   r   Ó   s   z%PythonPrinter._reset_multi_line_flagsc                 C   sP   | j p| j}tt d|¡ƒ| _ tt d|¡ƒ}|dks!|d dkr&| j | _|S )zcreturn true if the given line is part of a multi-line block,
        via backslash or triple-quote.ú\\$ú\"\"\"|\'\'\'r   é   r   )rF   rG   rA   r   r8   r3   Úfindall)r   r.   Úcurrent_stateÚtriplesr   r   r   Ú_in_multi_lineÙ   s   

zPythonPrinter._in_multi_linec                 C   sŒ   d }|   ¡  | jD ]3}|  |¡r| j |d ¡ q	| ¡ }|d u r0t d|¡r0t d|¡ 	d¡}| j |  
||¡d ¡ q	g | _|   ¡  d S )Nr    ú^[ \t]*[^# \t]ú	^([ \t]*)r   )r   r
   rN   r   r!   Ú
expandtabsr   r8   r2   r9   r7   )r   rE   Úentryr   r   r   r1   ì   s   

z#PythonPrinter._flush_adjusted_linesr   ©rB   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r"   r+   r/   r,   r?   r4   r7   r   rN   r1   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 ]+}||ƒr+| |¡ q| ¡ }|du rBt  d
|¡rBt  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„ }| rdˆˆ r?|dˆˆ  | ƒ\}} |r3dˆˆ< n/|dˆˆ  | ƒ\}} n#|d| ƒ\}} |rJ|S |d	| ƒ\}} |r[| d
¡ˆˆ< q|d| ƒ\}} | s|S )NrH   TFc                 S   s2   t  | |¡}|r||t| d¡ƒd … fS d |fS )Nr   )r   r2   r3   r9   )ÚregÚtÚmr   r   r   r2     s   z7adjust_whitespace.<locals>.in_multi_line.<locals>.matchz%sz.*?(?=%s|$)r0   rI   r   z.*?(?=\"\"\"|\'\'\'|#|$))r   r8   r9   )r.   Ústart_stater2   rY   ©rF   ÚstaterG   r   r   Úin_multi_line  s*   

ïz(adjust_whitespace.<locals>.in_multi_linerB   c                 S   s   t  d| d| ¡S )NrC   rB   )r   rD   )r.   rE   r   r   r   r7   '  r   z'adjust_whitespace.<locals>._indent_lineNr#   rO   rP   r   r    rS   )r   r%   r&   rQ   r8   r2   r9   Újoin)Útextr]   r7   r-   rE   r.   r   r[   r   Úadjust_whitespaceý   s   
$
r`   )Ú__doc__r   Úmakor   r   r`   r   r   r   r   Ú<module>   s    p