PL i(6dZddlZddlmZGddZdZy)zs& $*% C rNcd|_ttjd|D]G\}}|jj |||j ||z|jdIy)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.F\r?\nNr)r enumeratersplitr appendr#r!)rblockstarting_linenoils rwrite_indented_blockz"PythonPrinter.write_indented_blockBsj %bhhx78 #DAq    # #A &*!!/A"56    "  #rc4|D]}|j|y)z"print a series of lines of python.N) writeline)rlineslines r writelineszPythonPrinter.writelinesNs !D NN4  !rc>|js|jd|_|6|jj|s|jj|rd}nd}|xrt |xr|ddk(}|s~|r|j |rk|jdkDr\|xjdzc_t |jdk(rtjd|jj|y|jj|j|dz|jt |j!d|j"j%|r|j&j|}|rB|j)d}|xjdz c_|jj+|yd}|j,j|}|r1|xjdz c_|jj+|yyy) 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. TNFr#rzToo many whitespace closuresr%)r_flush_adjusted_linesrmatchrlen_is_unindentorrr r MakoExceptionpopr r& _indent_liner!r+rsearchrgroupr,r)rr5hastext is_commentr:indentorm2s rr3zPythonPrinter.writelineSs##  & & (#'D  L%%++D1~~##D)GG:c$i:DGsN  3 3D 9 a KK1 K4%%&!+ ../MNN    " " $ <  $++D1D89 C 4 012 ?? ! !$ ' %%++D1E ;;q> q ""))(3,,2248KK1$K&&--h7# (rc$|jy)z1close this printer, flushing any remaining lines.N)r9rs rclosezPythonPrinter.closes ""$rct|jdk(ry|jd}|y|jj|}t |S)zqreturn true if the given line is an 'unindentor', relative to the last 'indent' event received. rF)r;r rr:bool)rr5rDr:s rr<zPythonPrinter._is_unindentorsY t!! "a '%%b)  ##))$/E{rc|dk(r|j|jz|zStjd|z|j|jz|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.^%s)r rrsub)rr5 stripspaces rr?zPythonPrinter._indent_linesR  $$t{{2T9 9vv Z !2!2T[[!@$  rc"d\|_|_y)z^reset the flags which would indicate we are in a backslashed or triple-quoted section.)FFN) backslashed triplequotedrGs rrz%PythonPrinter._reset_multi_line_flagss/;+$+rc|jxs |j}ttjd||_t tj d|}|dk(s|dzdk7r|j |_|S)zcreturn true if the given line is part of a multi-line block, via backslash or triple-quote.\\$ \"\"\"|\'\'\'rr)rRrSrKrr@r;findall)rr5 current_statetripless r_in_multi_linezPythonPrinter._in_multi_linesq((=D,=,=  &$ 78bjj!1489 a<7Q;!+$($5$5 5D rcd}|j|jD]}|j|r|jj |dz3|j }|;t jd|r%t jd|jd}|jj |j||dzg|_|jy)Nr%^[ \t]*[^# \t] ^([ \t]*)r) rr r[r r& expandtabsrr@r:rAr?)rrPentrys rr9z#PythonPrinter._flush_adjusted_liness  $$&%% OE""5) !!%$,/((*%"))4Eu*M!#,!>!D!DQ!GJ !!$"3"3E:"F"MN O $$&rrrM)__name__ __module__ __qualname__rr!r#r'r1r6r3rHr<r?rr[r9rrrrsH&PP2! #! E8N%P  ; &'rrcddgd\fd}d d}g}d}tjd|D]}||r|j||j}|;tjd|r%tj d|j d }|j|||d j|S) z.in_multi_line..match s=a A!C O-...Qwrz%sz .*?(?=%s|$)r8rVrz.*?(?=\"\"\"|\'\'\'|#|$))rr@rA)r5 start_stater:rkrRstaterSs r in_multi_linez(adjust_whitespace..in_multi_linesK(?E,,? 99VT "!%E+ !&E+  \"l(; ;TB4*/E,'#NU<5H$H$OGAtd+4&& 0$74*+''!*E,' ;TB4#&rc6tjd|zd|S)NrNrM)rrO)r5rPs rr?z'adjust_whitespace.._indent_line'svvfz)2t44rNr)r]r^rr%ra)rr+r,r_r@r:rAjoin) textrnr?r4rPr5rRrmrSs @@@radjust_whitespacerrsENE"([,"H5 EJ4(9   LL ??$D!bii0A4&HXXlD9??B LLdJ7 89 99U r)__doc__rmakorrrrrerrrus#C l'l'^8r