a
    
dH=                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddl
mZ dd	l
mZ dd
l
mZ ddl
mZ eh dZeh dZdd ZeejejejejgZG dd dejZddddZdd Zdd Zdd Zdd Zdd Z d d! Z!d"S )#a^  PyTreeUnwrapper - produces a list of logical lines from a pytree.

[for a description of what a logical line is, see logical_line.py]

This is a pytree visitor that goes over a parse tree and produces a list of
LogicalLine containers from it, each with its own depth and containing all the
tokens that could fit on the line if there were no maximal line-length
limitations.

Note: a precondition to running this visitor and obtaining correct results is
for the tree to have its comments spliced in as nodes. Prefixes are ignored.

For most uses, the convenience function UnwrapPyTree should be sufficient.
    )pytree)token)pytree_utils)pytree_visitor)split_penalty)format_token)logical_line)object_state)style)subtypes>   ([{>   )]}c                 C   s,   t  }||  | }|jdd d |S )zCreate and return a list of logical lines from the given pytree.

  Arguments:
    tree: the top-level pytree node to unwrap..

  Returns:
    A list of LogicalLine objects.
  c                 S   s   | j S N)lineno)x r   e/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/yapf/pytree/pytree_unwrapper.py<lambda>;       zUnwrapPyTree.<locals>.<lambda>)key)PyTreeUnwrapperVisitGetLogicalLinessort)treeZ	unwrapperZllinesr   r   r   UnwrapPyTree/   s
    	
r   c                   @   sr  e Zd ZdZdd Zdd Zdd Zeh dZd	d
 Z	dd Z
eh dZdd ZeddhZdd ZeddhZdd Zeh dZdd ZedhZdd ZedhZdd Zd d! Zed"hZd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zed-hZd.d/ Zed0d1hZ d2d3 Z!ed1hZ"d4d5 Z#d6d7 Z$d8d9 Z%d:d; Z&d<d= Z'd>d? Z(d@dA Z)dBdC Z*dDdE Z+dFdG Z,dHS )Ir   a  PyTreeUnwrapper - see file-level docstring for detailed description.

  Note: since this implements PyTreeVisitor and node names in lib2to3 are
  underscore_separated, the visiting methods of this class are named as
  Visit_node_name. invalid-name pragmas are added to each such method to silence
  a style warning. This is forced on us by the usage of lib2to3, and re-munging
  method names to make them different from actual node names sounded like a
  confusing and brittle affair that wasn't worth it for this small & controlled
  deviation from the style guide.

  To understand the connection between visitor methods in this class, some
  familiarity with the Python grammar is required.
  c                 C   s   g | _ td| _d| _d S Nr   )_logical_linesr   LogicalLine_cur_logical_line
_cur_depthselfr   r   r   __init__U   s    zPyTreeUnwrapper.__init__c                 C   s   |    | jS )zFetch the result of the tree walk.

    Note: only call this after visiting the whole tree.

    Returns:
      A list of LogicalLine objects.
    )_StartNewLiner!   r%   r   r   r   r   `   s    	zPyTreeUnwrapper.GetLogicalLinesc                 C   sF   | j jr4| j| j  t| j  t| j  t| j  t| j	| _ dS )zFinish current line and start a new one.

    Place the currently accumulated line into the _logical_lines list and
    start a new one.
    N)
r#   tokensr!   append_MatchBrackets_IdentifyParameterLists_AdjustSplitPenaltyr   r"   r$   r%   r   r   r   r(   l   s    


zPyTreeUnwrapper._StartNewLine>
   Zif_stmtZ
case_blockZtry_stmt
match_stmtZ	with_stmtZexpect_clauseZ
while_stmtZfor_stmtZclassdefZfuncdefc                 C   s   |j ot|j | jv }t|}|j o6t|j dk}|r@|rD|rR|  jd7  _|   | | |rl|rp|r~|  jd8  _d S )Nr.      )parentr   NodeName_STMT_TYPESZIsCommentStatementr$   r(   DefaultNodeVisit)r&   nodeZsingle_stmt_suiteZis_comment_stmtZis_inside_matchr   r   r   Visit_simple_stmt   s    


z!PyTreeUnwrapper.Visit_simple_stmtc                 C   s8   |j D ],}|jtjkr(|j|v r(|   | | qdS )a~  Helper for visiting compound statements.

    Python compound statements serve as containers for other statements. Thus,
    when we encounter a new compound statement, we start a new logical line.

    Arguments:
      node: the node to visit.
      substatement_names: set of node names. A compound statement will be
        recognized as a NAME node with a name in this set.
    N)childrentypegrammar_tokenNAMEvaluer(   r   )r&   r4   Zsubstatement_nameschildr   r   r   _VisitCompoundStatement   s    
z'PyTreeUnwrapper._VisitCompoundStatement>   elififelsec                 C   s   |  || j d S r   )r<   _IF_STMT_ELEMSr&   r4   r   r   r   Visit_if_stmt   s    zPyTreeUnwrapper.Visit_if_stmtwhiler?   c                 C   s   |  || j d S r   )r<   _WHILE_STMT_ELEMSrA   r   r   r   Visit_while_stmt   s    z PyTreeUnwrapper.Visit_while_stmtforc                 C   s   |  || j d S r   )r<   _FOR_STMT_ELEMSrA   r   r   r   Visit_for_stmt   s    zPyTreeUnwrapper.Visit_for_stmt>   tryexceptr?   finallyc                 C   s   |  || j d S r   )r<   _TRY_STMT_ELEMSrA   r   r   r   Visit_try_stmt   s    zPyTreeUnwrapper.Visit_try_stmtrJ   c                 C   s   |  || j d S r   )r<   _EXCEPT_STMT_ELEMSrA   r   r   r   Visit_except_clause   s    z#PyTreeUnwrapper.Visit_except_clausedefc                 C   s   |  || j d S r   )r<   _FUNC_DEF_ELEMSrA   r   r   r   Visit_funcdef   s    zPyTreeUnwrapper.Visit_funcdefc                 C   sZ   |    d}|jD ]&}|d7 }| | |jtjkr q:q|j| jD ]}| | qFd S )Nr   r/   )r(   r6   r   r7   r8   ASYNCr&   r4   indexr;   r   r   r   Visit_async_funcdef   s    

z#PyTreeUnwrapper.Visit_async_funcdefclassc                 C   s   |  || j d S r   )r<   _CLASS_DEF_ELEMSrA   r   r   r   Visit_classdef   s    zPyTreeUnwrapper.Visit_classdefc                 C   sx   |    d}|jD ]&}|d7 }| | |jtjkr q:q|j| jD ],}|jtjkrh|jdkrh|    | | qFd S )Nr   r/   r?   )r(   r6   r   r7   r8   rS   r9   r:   rT   r   r   r   Visit_async_stmt   s    

z PyTreeUnwrapper.Visit_async_stmtc                 C   s<   |j D ]0}| | |jtjkr||j d kr|   qd S r    )r6   r   r7   r8   COMMENTr(   r&   r4   r;   r   r   r   Visit_decorator   s    

zPyTreeUnwrapper.Visit_decoratorc                 C   s"   |j D ]}|   | | qd S r   r6   r(   r   r\   r   r   r   Visit_decorators   s    
z PyTreeUnwrapper.Visit_decoratorsc                 C   s"   |j D ]}|   | | qd S r   r^   r\   r   r   r   Visit_decorated   s    
zPyTreeUnwrapper.Visit_decoratedwithc                 C   s   |  || j d S r   )r<   _WITH_STMT_ELEMSrA   r   r   r   Visit_with_stmt  s    zPyTreeUnwrapper.Visit_with_stmtmatchZcasec                 C   s   |  || j d S r   )r<   _MATCH_STMT_ELEMSrA   r   r   r   Visit_match_stmt  s    z PyTreeUnwrapper.Visit_match_stmtc                 C   s6   |  j d7  _ |   | || j |  j d8  _ d S Nr/   )r$   r(   r<   _CASE_BLOCK_ELEMSrA   r   r   r   Visit_case_block  s    z PyTreeUnwrapper.Visit_case_blockc                 C   s2   |  j d7  _ |   | | |  j d8  _ d S rg   )r$   r(   r3   rA   r   r   r   Visit_suite  s    
zPyTreeUnwrapper.Visit_suitec                 C   s   t | | | d S r   _DetermineMustSplitAnnotationr3   rA   r   r   r   Visit_listmaker  s    zPyTreeUnwrapper.Visit_listmakerc                 C   s   t | | | d S r   rk   rA   r   r   r   Visit_dictsetmaker  s    z"PyTreeUnwrapper.Visit_dictsetmakerc                 C   s"   |j jdkrt| | | d S )Nr   )Zprev_siblingr:   rl   r3   rA   r   r   r   Visit_import_as_names!  s    z%PyTreeUnwrapper.Visit_import_as_namesc                 C   s   t | | | d S r   rk   rA   r   r   r   Visit_testlist_gexp&  s    z#PyTreeUnwrapper.Visit_testlist_gexpc                 C   s   t | | | d S r   rk   rA   r   r   r   Visit_arglist*  s    zPyTreeUnwrapper.Visit_arglistc                 C   s   t | | | d S r   rk   rA   r   r   r   Visit_typedargslist.  s    z#PyTreeUnwrapper.Visit_typedargslistc                 C   s   t | | | d S r   rk   rA   r   r   r   Visit_subscriptlist2  s    z#PyTreeUnwrapper.Visit_subscriptlistc                 C   sH   |j tv r|   n0|j tjks*|j rD| jt	
|t| dS )zDefault visitor for tree leaves.

    A tree leaf is always just gets appended to the current logical line.

    Arguments:
      leaf: the leaf to visit.
    N)r7   _WHITESPACE_TOKENSr(   r8   r[   r:   stripr#   ZAppendTokenr   ZFormatTokenr   r1   )r&   Zleafr   r   r   DefaultLeafVisit6  s    

z PyTreeUnwrapper.DefaultLeafVisitN)-__name__
__module____qualname____doc__r'   r   r(   	frozensetr2   r5   r<   r@   rB   rD   rE   rG   rH   rL   rM   rN   rO   rQ   rR   rV   rX   rY   rZ   r]   r_   r`   rb   rc   re   rf   rh   ri   rj   rm   rn   ro   rp   rq   rr   rs   rv   r   r   r   r   r   F   sR   




r   r   r   r   )r   r   r   c                 C   s   g }| j D ]v}|jtv r$|| n&|jtv rJ||d _|d |_|  |D ]0}tt	|j
t|j
krN|j| ||_qNq
dS )a  Visit the node and match the brackets.

  For every open bracket ('[', '{', or '('), find the associated closing bracket
  and "match" them up. I.e., save in the token a pointer to its associated open
  or close bracket.

  Arguments:
    line: (LogicalLine) A logical line.
  N)r)   r:   _OPENING_BRACKETSr*   _CLOSING_BRACKETSmatching_bracketpopidr   ZGetOpeningBracketr4   Zcontainer_elementsZcontainer_opening)lineZbracket_stackr   Zbracketr   r   r   r+   I  s    





r+   c                 C   s   g }g }| j D ]}tj|jv r<|jjdks.J ||j q|rb|jdkrb||d jkr|  qtj|jv rx|| |rtj	|jv r| }|d j
t|| qdS )zVisit the node to create a state for parameter lists.

  For instance, a parameter is considered an "object" with its first and last
  token uniquely identifying the object.

  Arguments:
    line: (LogicalLine) A logical line.
  r   r   r|   N)r)   r   ZFUNC_DEFZ
next_tokenr:   r*   r   r   ZPARAMETER_STARTZPARAMETER_STOP
parametersr	   	Parameter)r   Z
func_stackZparam_stacktokstartr   r   r   r,   b  s     	

r,   c                 C   s`   d}t | jD ]L\}}|r4|s4t|jtjjtj |j	t
v rH|d7 }q|j	tv r|d8 }qdS )zVisit the node and adjust the split penalties if needed.

  A token shouldn't be split if it's not within a bracket pair. Mark any token
  that's not within a bracket pair as "unbreakable".

  Arguments:
    line: (LogicalLine) An logical line.
  r   r/   N)	enumerater)   r   SetNodeAnnotationr4   
AnnotationZSPLIT_PENALTYr   ZUNBREAKABLEr:   r}   r~   )r   Zbracket_levelrU   r   r   r   r   r-     s    	


r-   c                 C   s  t drdS t| spt| j }|jdkrJtdd | jD dk rJdS t	| jd t
jrl| jd jdkrpdS t| j}d	}t| jd	  ||d
 k r| j| }t	|t
jr|jdkr| j|d
  }|jtjkr|d
7 }||d
 krqt| j|d
   |d
7 }qdS )z:Enforce a split in the list if the list ends with a comma.ZDISABLE_ENDING_COMMA_HEURISTICNr   c                 s   s   | ]}|j tjkrd V  qdS )r/   N)r7   r8   COMMA).0chr   r   r   	<genexpr>  r   z0_DetermineMustSplitAnnotation.<locals>.<genexpr>   r|   ,r   r/   )r
   ZGet_ContainsCommentsnextr0   Zleavesr:   sumr6   
isinstancer   Leaflen_SetMustSplitOnFirstLeafr7   r8   r[   )r4   r   Znum_childrenrU   r;   Z
next_childr   r   r   rl     s0    



rl   c                 C   s6   t | tjr| jtjkS | jD ]}t|r dS qdS )z,Return True if the list has a comment in it.TF)r   r   r   r7   r8   r[   r6   r   )r4   r;   r   r   r   r     s    
r   c                 C   s   t t | t jjd dS )z7Set the "must split" annotation on the first leaf node.TN)r   r   ZFirstLeafNoder   Z
MUST_SPLIT)r4   r   r   r   r     s    r   N)"rz   Zyapf_third_party._ylib2to3r   Z yapf_third_party._ylib2to3.pgen2r   r8   Zyapf.pytreer   r   r   Zyapf.yapflibr   r   r	   r
   r   r{   r}   r~   r   NEWLINEDEDENTINDENT	ENDMARKERrt   ZPyTreeVisitorr   Z_BRACKET_MATCHr+   r,   r-   rl   r   r   r   r   r   r   <module>   s6     !
