o
    #Zh                  	   @   s  d dl Z d dlZd dlZd dlmZmZmZ d dlmZmZ d dl	m
Z
mZmZmZmZmZmZmZ edZedZdefdd	Zd
ee dee fddZde jdeeef fddZdee de
eeef  fddZdedefddZeedddZdeeef defddZdeeef de
eeef  fddZdeeef deeef fdd Zd!ee d"eegef deeee f fd#d$Z G d%d& d&e!Z"d'd( Z#d,d*d+Z$dS )-    N)OrderedDictCounterdefaultdict)	FrameTypeTracebackType)IteratorListTupleIterableCallableUnionTypeVarMappingTR
max_lengthc                 C   sN   t | |kr%|t | d }|t | | }| d | | | | d   } | S )N   )len)seqr   middlerightleft r   G/var/www/html/lang_env/lib/python3.10/site-packages/stack_data/utils.pytruncate   s
   r   itreturnc                 C   s   t t| S N)listr   fromkeys)r   r   r   r   unique_in_order   s   r    nodec                 C   sJ   z| j jd | jjd d fW S  ty$   | jt| d| jd f Y S w )z
    Returns a pair of numbers representing a half open range
    (i.e. suitable as arguments to the `range()` builtin)
    of line numbers of the given AST nodes.
    r      
end_lineno)Zfirst_tokenstartZ
last_tokenendAttributeErrorlinenogetattr)r!   r   r   r   
line_range   s   
r)   lstc                 #   s    t |  tj|  fdddD ]L\}|rOtdgt fdd}tD ]"|fddd urD|fdd |fd	d q+ntd
tE d H  qd S )Nc                    s    |  dkS )N   r   x)countsr   r   <lambda>0       z"highlight_unique.<locals>.<lambda>keyFc                    s,   z|  }W n
 t y   Y d S w d |< |S )NT)
ValueError)fi)highlightedr   r   highlight_index5   s   
z)highlight_unique.<locals>.highlight_indexc                      s
     S r   indexr   groupitemr   r   r/   >   s   
 c                      s     d S Nr"   r8   r   )firstr;   r<   r   r   r/   @   s    c                      s   d d d d   S )Nr8   r   r:   r   r   r/   A   s    T)r   	itertoolsgroupbyr   r   setrepeatzip)r*   Z	is_commonr7   r   )r.   r>   r;   r6   r<   r   highlight_unique-   s    
rE   r-   c                 C   s   | S r   r   r,   r   r   r   identityH   s   rF   )mapperr2   c                c   s|    t t|| }tjt| t|dd dD ]%\}}t| \}}|r+t||E d H  qt| \}	}
|t |t |	V  qd S )Nc                 S   s   | d d S r=   r   )tr   r   r   r/   P   r0   z#collapse_repeated.<locals>.<lambda>r1   )r   mapr@   rA   rD   rE   )r*   Z	collapserrG   r2   ZkeyedZis_highlightedr;   Zoriginal_groupZhighlighted_groupZkeyed_group_r   r   r   collapse_repeatedL   s   rK   frame_or_tbc                 C   s$   t t| tjtjf t| tjfS r   )assert_
isinstancetypesr   r   rL   r   r   r   is_frameZ   s   rQ   c                 c   s.    | r| V  t | r| j} n| j} | sd S d S r   )rQ   f_backtb_nextrP   r   r   r   
iter_stack_   s   rT   c                 C   s   t | r	| | jfS | j| jfS r   )rQ   f_linenotb_frame	tb_linenorP   r   r   r   frame_and_linenoh   s   
rX   iterablekey_funcc                 C   s(   t t}| D ]}||| | q|S )a  
    Create a dictionary from an iterable such that the keys are the result of evaluating a key function on elements
    of the iterable and the values are lists of elements all of which correspond to the key.

    >>> def si(d): return sorted(d.items())
    >>> si(group_by_key_func("a bb ccc d ee fff".split(), len))
    [(1, ['a', 'd']), (2, ['bb', 'ee']), (3, ['ccc', 'fff'])]
    >>> si(group_by_key_func([-1, 0, 1, 3, 6, 8, 9, 2], lambda x: x % 2))
    [(0, [0, 6, 8, 2]), (1, [-1, 1, 3, 9])]
    )r   r   append)rY   rZ   resultr<   r   r   r   group_by_key_funco   s   r]   c                   @   s$   e Zd ZdZdd Zdd ZeZdS )cached_propertyz
    A property that is only computed once per instance and then replaces itself
    with an ordinary attribute. Deleting the attribute resets the property.

    Based on https://github.com/pydanny/cached-property/blob/master/cached_property.py
    c                 C   s   |j | _ || _d S r   )__doc__func)selfr`   r   r   r   __init__   s   
zcached_property.__init__c                 C   s(   |d u r| S |  | }|j| j j< |S r   )r`   __dict____name__)ra   obj_clsvaluer   r   r   cached_property_wrapper   s   z'cached_property.cached_property_wrapperN)rd   
__module____qualname__r_   rb   rh   __get__r   r   r   r   r^      s
    r^   c                    sL   dd l }ddlm} G  fdddt|d}|dd}||||  S )Nr   )get_lexer_by_namec                       s   e Zd Z fddZ  ZS )z'_pygmented_with_ranges.<locals>.MyLexerc                 3   sR    d t  |D ]\}}t fddD r|j} t|7  ||fV  q	d S )Nr   c                 3   s,    | ]\}}|   ko|k n  V  qd S r   r   ).0r$   r%   lengthr   r   	<genexpr>   s   * zE_pygmented_with_ranges.<locals>.MyLexer.get_tokens.<locals>.<genexpr>)super
get_tokensanyZExecutingNoder   )ra   textttyperg   )	__class__rangesrn   r   rr      s   z2_pygmented_with_ranges.<locals>.MyLexer.get_tokens)rd   ri   rj   rr   __classcell__r   rw   )rv   r   MyLexer   s    rz   python3F)stripnl)pygmentsZpygments.lexersrl   type	highlight
splitlines)	formattercoderw   r}   rl   rz   lexerr   ry   r   _pygmented_with_ranges   s
   
	r    c                 C   s   | st |trt|}|d S r   )rN   strAssertionError)	conditionerrorr   r   r   rM      s
   
rM   )r   )%astr@   rO   collectionsr   r   r   r   r   typingr   r   r	   r
   r   r   r   r   r   r   intr   r    ASTr)   boolrE   rF   rK   rQ   rT   rX   r]   objectr^   r   rM   r   r   r   r   <module>   s*    ("&"	0