o
    be                     @   s  d dl mZmZ d dlZd dlZd dlmZmZmZm	Z	m
Z
mZmZmZmZmZ d dlmZmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZmZmZ d d
lm Z m!Z!m"Z"m#Z# d dlm$Z$m%Z%m&Z&m'Z' d dlm(Z( edZ)e*Z+e*Z,eG dd dZ-eG dd dZ.dee) de	ee+e)f  fddZ/	d2de-de-de
e de0ddf
ddZ1ddde-d e*d!e2de0fd"d#Z3de-de0fd$d%Z4	&d3de-d e*d'ee, de0fd(d)Z5de-d*ed e*de0fd+d,Z6de-d-ed e*de0fd.d/Z7de-de2fd0d1Z8dS )4    )	dataclassfieldN)
Callable
CollectionDictIteratorListOptionalSequenceTupleTypeVarcast)NodeLeaf)token)BracketTrackerDOT_PRIORITY)Mode)STANDALONE_COMMENTTEST_DESCENDANTS)BRACKETSOPENING_BRACKETSCLOSING_BRACKETS)syms
whitespacereplace_childchild_towards)is_multiline_string	is_importis_type_commentlast_two_except)is_one_tuple_betweenTc                	   @   s>  e Zd ZU dZeed< dZeed< ee	dZ
ee ed< eedZeeee f ed< eedZeed< d	Zeed
< d	Zeed< dZee ed< dDdededdfddZdDdededdfddZedefddZedefddZedefddZedefddZedefddZedefdd Z edefd!d"Z!edefd#d$Z"e#j$fd%edefd&d'Z%defd(d)Z&defd*d+Z'defd,d-Z(		dDd.ed/edefd0d1Z)d2edefd3d4Z*dedee fd5d6Z+dEd7d8Z,dedefd9d:Z-		dDd;ede.e/e0eef  fd<d=Z1dFd>d?Z2de3fd@dAZ4defdBdCZ5dS )GLinez;Holds leaves and comments. Can be printed with `str(line)`.moder   depthdefault_factoryleavescommentsbracket_trackerFinside_bracketsshould_split_rhsNmagic_trailing_commaleafpreformattedreturnc                 C   s   |j tv pt|j }|sdS tj|j kr| jr| jdd= | jr2|s2| j	t
|| |d7  _	| js7|sU| j| | jjrJ| |rI|| _n| j|ddrU|   | |sb| j| dS dS )ag  Add a new `leaf` to the end of the line.

        Unless `preformatted` is True, the `leaf` will receive a new consistent
        whitespace prefix and metadata applied by :class:`BracketTracker`.
        Trailing commas are maybe removed, unpacked for loop variables are
        demoted from being delimiters.

        Inline comments are put aside.
        N)Zcomplex_subscriptT)ensure_removable)typer   boolvaluestripr   COLONis_class_paren_emptyr(   prefixr   is_complex_subscriptr+   r*   markr$   r-   has_magic_trailing_commaremove_trailing_commaappend_commentappend)selfr.   r/   Z	has_value rA   J/var/www/secure340b-portal/env/lib/python3.10/site-packages/black/lines.pyr?   0   s(   






zLine.appendc                 C   sD   | j jdkr| jrtd| jr|jtkrtd| j||d dS )zLike :func:`append()` but disallow invalid standalone comment structure.

        Raises ValueError when any `leaf` is appended after a standalone comment
        or when a standalone comment is not the first leaf on the line.
        r   z$cannot append to standalone commentsz5cannot append standalone comments to a populated liner/   N)r*   r%   
is_comment
ValueErrorr(   r3   r   r?   )r@   r.   r/   rA   rA   rB   append_safeP   s   zLine.append_safec                 C   s   t | jdko| jd jtkS )z"Is this line a standalone comment?   r   )lenr(   r3   r   r@   rA   rA   rB   rD   a   s   zLine.is_commentc                 C   s   t | o| jd jtjkS )zIs this line a decorator?r   )r4   r(   r3   r   ATrI   rA   rA   rB   is_decoratorf   s   zLine.is_decoratorc                 C   s   t | o
t| jd S )zIs this an import line?r   )r4   r   r(   rI   rA   rA   rB   r   k   s   zLine.is_importc                 C   s*   t | o| jd jtjko| jd jdkS )z Is this line a class definition?r   class)r4   r(   r3   r   NAMEr5   rI   rA   rA   rB   is_classp   s
   zLine.is_classc                 C   s&   | j o| jdd dd tdD kS )zEIs this line a class definition with a body consisting only of "..."?Nc                 S   s   g | ]}t tjd qS ).)r   r   DOT).0_rA   rA   rB   
<listcomp>|   s    z&Line.is_stub_class.<locals>.<listcomp>   )rN   r(   rangerI   rA   rA   rB   is_stub_classy   s   zLine.is_stub_classc                 C   s   z| j d }W n
 ty   Y dS w z| j d }W n ty$   d}Y nw |jtjkr0|jdkpD|jtjkoD|duoD|jtjkoD|jdkS )zBIs this a function definition? (Also returns True for async defs.)r   FrG   Ndef)r(   
IndexErrorr3   r   rM   r5   ASYNC)r@   
first_leafZsecond_leafrA   rA   rB   is_def   s$   
zLine.is_defc                 C   s`   t | o/t| jdko/| jo/| jd jtjko/| jd jdko/| jd jtjko/| jd jdkS )zzIs this a class with no base classes but using parentheses?

        Those are unnecessary and should be removed.
              (rU   ))	r4   rH   r(   rN   r3   r   LPARr5   RPARrI   rA   rA   rB   r8      s   zLine.is_class_paren_emptyc                 C   s,   t | o| jd jtjko| jd jdS )z#Is the line a triple quoted string?r   )z"""z''')r4   r(   r3   r   STRINGr5   
startswithrI   rA   rA   rB   is_triple_quoted_string   s
   zLine.is_triple_quoted_stringdepth_limitc                 C   s*   | j D ]}|jtkr|j|kr dS qdS )z)If so, needs to be split before emitting.TF)r(   r3   r   Zbracket_depth)r@   rf   r.   rA   rA   rB   contains_standalone_comments   s
   
z!Line.contains_standalone_commentsc                 C   s   t  }z)| jd }|t| |jtjks|jtjkr+|js+| jd }|t| W n
 t	y6   Y dS w d}| j
 D ]\}}|D ]}t|rY|sUt|dsY||vrY  dS d}qDq>dS )Nr1   F ignoreT)setr(   addidr3   r   COMMArb   r5   rY   r)   itemsr   )r@   Zignored_ids	last_leafZcomment_seenZleaf_idr)   commentrA   rA   rB   $contains_uncollapsable_type_comments   s8   



z)Line.contains_uncollapsable_type_commentsc                 C   s   | j sdS tdd | j D d}tdd t| j D d}||kr@| j dd  D ]}| jt|g D ]}t|dr>  dS q3q(dS )	NFc                 s        | ]}|j d kr|j V  qdS r   NlinenorR   r.   rA   rA   rB   	<genexpr>       z9Line.contains_unsplittable_type_ignore.<locals>.<genexpr>r   c                 s   rr   rs   rt   rv   rA   rA   rB   rw      rx   r1   ri   T)r(   nextreversedr)   getrl   r   )r@   
first_line	last_linenoderp   rA   rA   rB   !contains_unsplittable_type_ignore   s   
z&Line.contains_unsplittable_type_ignorec                 C   s   t dd | jD S )Nc                 s   s    | ]}t |V  qd S N)r   rv   rA   rA   rB   rw      s    z2Line.contains_multiline_strings.<locals>.<genexpr>)anyr(   rI   rA   rA   rB   contains_multiline_strings   s   zLine.contains_multiline_stringsclosingr2   c                 C   s   |j tv r| jr| jd j tjksdS |j tjkrdS |j tjkr6|s%dS | jd }t|jo4|jj t	j
kS | jr;dS t|j|| jsEdS dS )zReturn True if we have a magic trailing comma, that is when:
        - there's a trailing comma here
        - it's not a one-tuple
        Additionally, if ensure_removable:
        - it's not from square bracket indexing
        rh   FT)r3   r   r(   r   rm   RBRACERSQBr4   parentr   	listmakerr   r!   opening_bracket)r@   r   r2   commarA   rA   rB   r<      s&   

zLine.has_magic_trailing_commarp   c                 C   s   |j tkr| j rd|_dS |j tjkrdS | js"t|_ d|_dS | jd }|j tjkrV|j	sV|j
rVtt|j
 dkrVt|sVt| jdk rQt|_ d|_dS | jd }| jt|g | dS )z0Add an inline or standalone comment to the line. Frh   rU   r^   r1   T)r3   r   r*   Zany_open_bracketsr9   r   COMMENTr(   rb   r5   r   rH   listr   r)   
setdefaultrl   r?   )r@   rp   ro   rA   rA   rB   r>     s<   



zLine.append_commentc                 C   s   | j t|g S )z;Generate comments that should appear directly after `leaf`.)r)   r{   rl   )r@   r.   rA   rA   rB   comments_afterB  s   zLine.comments_afterc                 C   s>   | j  }| jt|g }| jt| j d g | dS )z@Remove the trailing comma and moves the comments attached to it.rh   N)r(   popr)   rl   r   extend)r@   Ztrailing_commaZtrailing_comma_commentsrA   rA   rB   r=   F  s
   
zLine.remove_trailing_commac                 C   sj   | j  }|du rdS |j}t|tr&|jtjkrdS |jtjkr&t	||}|duo4t
dd | D S )zAReturn True iff `leaf` is part of a slice with non-trivial exprs.NFc                 s   s    | ]}|j tv V  qd S r   )r3   r   )rR   nrA   rA   rB   rw   \  s    

z,Line.is_complex_subscript.<locals>.<genexpr>)r*   Zget_open_lsqbZnext_sibling
isinstancer   r3   r   r   Zsubscriptlistr   r   Z	pre_order)r@   r.   Z	open_lsqbZsubscript_startrA   rA   rB   r:   N  s   


zLine.is_complex_subscriptrz   c                 c   s    t ttt gttttf  f |rtnt}|| j	D ]+\}}t
|jt
|j }d|jv r2 dS | |D ]	}|t
|j7 }q7|||fV  qdS )zReturn an enumeration of leaves with their length.

        Stops prematurely on multiline strings and standalone comments.
        
N)r   r   r
   r   r   r   Indexenumerate_reversed	enumerater(   rH   r9   r5   r   )r@   rz   opindexr.   lengthrp   rA   rA   rB   enumerate_with_length`  s   

zLine.enumerate_with_lengthc                 C   s   t | j| j| j| j| jdS )N)r$   r%   r+   r,   r-   )r#   r$   r%   r+   r,   r-   rI   rA   rA   rB   cloneu  s   z
Line.clonec                 C   sz   | sdS d| j  }t| j}t|}|j | |j }|D ]}|t|7 }qtj	| j
 D ]}|t|7 }q0|d S )zRender the line.r   z    )r%   iterr(   ry   r9   r5   str	itertoolschainfrom_iterabler)   values)r@   indentr(   firstresr.   rp   rA   rA   rB   __str__~  s   

zLine.__str__c                 C   s   t | jp| jS )z/Return True if the line has leaves or comments.)r4   r(   r)   rI   rA   rA   rB   __bool__  s   zLine.__bool__F)r0   N)r0   r#   )6__name__
__module____qualname____doc__r   __annotations__r%   intr   r   r(   r   r   dictr)   r   LeafIDr   r*   r+   r4   r,   r-   r	   r?   rF   propertyrD   rK   r   rN   rW   r\   r8   re   sysmaxsizerg   rq   r   r   r<   r>   r   r=   r:   r   r   r   r   r   r   r   r   rA   rA   rA   rB   r#   "   sj   
  $!
!%


	r#   c                   @   s   e Zd ZU dZdZeed< dZee	 ed< dZ
eed< eedZee ed	< d
e	deeef fddZd
e	deeef fddZd
e	dedeeef fddZdS )EmptyLineTrackera{  Provides a stateful method that returns the number of potential extra
    empty lines needed before and after the currently processed line.

    Note: this tracker works on lines that haven't been split yet.  It assumes
    the prefix of the first leaf consists of optional newlines.  Those newlines
    are consumed by `maybe_empty_lines()` and included in the computation.
    Fis_pyiNprevious_liner   previous_afterr&   previous_defscurrent_liner0   c                 C   s:   |  |\}}| jdu rdn|| j }|| _|| _||fS )zReturn the number of extra empty lines before and after the `current_line`.

        This is for separating `def`, `async def` and `class` with extra empty
        lines (two on module-level).
        Nr   )_maybe_empty_linesr   r   )r@   r   beforeafterrA   rA   rB   maybe_empty_lines  s   z"EmptyLineTracker.maybe_empty_linesc                 C   s  d}|j dkr| jrdnd}|jr%|jd }|jd}t||}d|_nd}|j }| jrS| jd |krS| j  | jrC|r@dnd}n|rGdnd}| jrS| jd |ks4|js\|j	s\|j
rb| ||S | jrx| jjrx|jsx|| jj krx|puddfS | jr| jj
r|jr|dfS |dfS )NrG   r   r^   r   r   rh   )r%   r   r(   r9   countminr   r   rK   r\   rN   #_maybe_empty_lines_for_class_or_defr   r   re   )r@   r   Zmax_allowedr[   r   r%   rA   rA   rB   r     sF   




z#EmptyLineTracker._maybe_empty_linesr   c                 C   s  |j s
| j|j | jd u rdS | jj r| jr|jrdS dS | jj|jk r0| jjs.| jjr0dS | jj	rA| jj|jkrA|dkrAdS | jrr| jj|jkrNd}n&|jsU| jjrb|jr_| jjr_d}nd}n|jsh|j ro| jjsod}nd}nd}|jr}|r}|d8 }|dfS )N)r   r   )r   rG   r   rG   r^   )
rK   r   r?   r%   r   r   rW   rN   r\   rD   )r@   r   r   newlinesrA   rA   rB   r     sP   

z4EmptyLineTracker._maybe_empty_lines_for_class_or_def)r   r   r   r   r   r4   r   r   r	   r#   r   r   r   r   r   r   r   r   r   r   rA   rA   rA   rB   r     s   
 '
r   sequencer0   c                 c   s2    t | d }t| D ]}||fV  |d8 }qdS )z;Like `reversed(enumerate(sequence))` if that were possible.rG   N)rH   rz   )r   r   elementrA   rA   rB   r     s   

r   Fnew_lineold_liner(   r/   c                 C   sR   |D ]$}t |j|j}t|| | j||d ||D ]	}| j|dd qqdS )a  
    Append leaves (taken from @old_line) to @new_line, making sure to fix the
    underlying Node structure where appropriate.

    All of the leaves in @leaves are duplicated. The duplicates are then
    appended to @new_line and used to replace their originals in the underlying
    Node structure. Any comments attached to the old leaves are reattached to
    the new leaves.

    Pre-conditions:
        set(@leaves) is a subset of set(@old_line.leaves).
    rC   TN)r   r3   r5   r   r?   r   )r   r   r(   r/   Zold_leafZnew_leafZcomment_leafrA   rA   rB   append_leaves  s   
r   r   )line_strlineline_lengthr   c                C   s*   |st | }t||kod|vo|   S )zReturn True if `line` is no longer than `line_length`.

    Uses the provided `line_str` rendering, if any, otherwise computes a new one.
    r   )line_to_stringrH   rg   )r   r   r   rA   rA   rB   is_line_short_enough/  s   r   c                 C   s   | j }t|dk rdS |d jtjkrr|d jtjkrrd}d}|d }|ddd D ]G}|jtv r>|jtvr9 dS |d7 }n(|jtjkrI|d7 }n|jtjkr^|jtjks]|jtv s] dS n|jtvrf dS |dkrq|dkrq dS q*dS )	zReturn False if the line cannot be split *for sure*.

    This is not an exhaustive search but a cheap heuristic that we can use to
    avoid some unfortunate formattings (mostly around wrapping unsplittable code
    in unnecessary parentheses).
    r^   Fr   rG   rh   r1   NT)	r(   rH   r3   r   rc   rQ   r   r   rM   )r   r(   Z
call_count	dot_country   r.   rA   rA   rB   can_be_split=  s0    




r   rA   omit_on_explodec           	      C   sJ  | j }|jsdS | }||dkrdS |tkrdS t| jdks&J d| jd }| jd }|jtv rC|jt	vrCt
| ||drCdS | jd }| jd	 }| jrfzt| j|d
\}}W n
 tye   Y dS w |jtjks|jtjks|jtjkr|jr|jjtjkr|jtv rdS t|rdS | jr|jtjkrdS t| ||drdS dS )a  Does `line` have a shape safe to reformat without optional parens around it?

    Returns True for only a subset of potentially nice looking formattings but
    the point is to not return false positives that end up producing lines that
    are too long.
    TrG   Fr^   zStranded delimiterr   )r   r   r1   rh   )Zomit)lastr   )r*   
delimitersZmax_delimiter_priorityZdelimiter_count_with_priorityr   rH   r(   r3   r   r   _can_omit_opening_parenr-   r    LookupErrorr   rb   r   r   r   r   trailerr   rm   _can_omit_closing_paren)	r   r   r   ZbtZmax_priorityr   secondZpenultimater   rA   rA   rB   can_omit_invisible_parensa  sR   







r   r   c                C   s   d}d| j  }d}|  D ]%\}}}|jtv r|j|u rd}|r2||7 }||kr+ dS |jtv r2d}qt| j|d kr>dS dS ) See `can_omit_invisible_parens`.Fr]   rh   TrG   )r%   r   r3   r   r   r   rH   r(   )r   r   r   	remainderr   _indexr.   leaf_lengthrA   rA   rB   r     s"   

r   r   c                C   sZ   d| j  }d}|  D ]\}}}||7 }||ju r#|s||kr" dS q|jtv r*d}qdS )r   r]   FT)r%   r   r   r3   r   )r   r   r   r   Zseen_other_bracketsr   r.   r   rA   rA   rB   r     s   


r   c                 C   s   t | dS )zmReturns the string representation of @line.

    WARNING: This is known to be computationally expensive.
    r   )r   r6   )r   rA   rA   rB   r     s   r   r   )rA   )9dataclassesr   r   r   r   typingr   r   r   r   r   r	   r
   r   r   r   Zblib2to3.pytreer   r   Zblib2to3.pgen2r   Zblack.bracketsr   r   Z
black.moder   Zblack.nodesr   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r   r   r   r#   r   r   r4   r   r   r   r   r   r   r   r   rA   rA   rA   rB   <module>   sd    0  r"{	
 '
N