U
    -e
                     @   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 d dlmZ edZedZedd	d
Zee ee dddZee jeeef dddZee e
eeef  dddZeedddZeedddZeeef edddZeeef e
eeef  dddZ eeef eeef ddd Z!ee eegef eeee f d!d"d#Z"G d$d% d%e#Z$d&d' Z%d-d)d*Z&d+d, Z'dS ).    N)OrderedDictCounterdefaultdict)	FrameTypeTracebackType)IteratorListTupleIterableCallableUnionTypeVarMapping)ASTTextTR)
max_lengthc                 C   sN   t | |krJ|t | d }|t | | }| d | | | | d   } | S )N   )len)seqr   middlerightleft r   Q/var/www/html/Darija-Ai-Train/env/lib/python3.8/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!   )atoknoder   c                 C   sh   t |ttddr>t| |j\}}t| |jd \}}||fS | j|dd\\}}\}}||d fS dS )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.
    Z
match_caser   F)padded   N)
isinstancegetattrast
line_rangepatternbodyZget_text_positions)r"   r#   start_end_startend_r   r   r   r*      s    r*   )lstr   c                 #   s   t |  tj|  fdddD ]\}|rtdgt fdd}tD ]D|fddd k	r|fdd |fd	d qTn
td
tE d H  qd S )Nc                    s    |  dkS )N   r   x)countsr   r   <lambda>/       z"highlight_unique.<locals>.<lambda>keyFc                    s.   z
|  }W n t k
r    Y d S X d |< |S )NT)
ValueError)fi)highlightedr   r   highlight_index4   s    
z)highlight_unique.<locals>.highlight_indexc                      s
     S r   indexr   groupitemr   r   r7   =   r8   c                      s     d S Nr&   r@   r   )firstrC   rD   r   r   r7   ?   r8   c                      s   d d d d   S )Nr$   r@   r   rB   r   r   r7   @   r8   T)r   	itertoolsgroupbyr   r   setrepeatzip)r2   Z	is_commonr?   r   )r6   rF   rC   r>   rD   r   highlight_unique,   s    
rL   )r5   r   c                 C   s   | S r   r   r4   r   r   r   identityG   s    rM   )mapperr:   c                c   sz   t t|| }tjt| t|dd dD ]J\}}t| \}}|rTt||E d H  q*t| \}	}
|t |t |	V  q*d S )Nc                 S   s   | d d S rE   r   )tr   r   r   r7   O   r8   z#collapse_repeated.<locals>.<lambda>r9   )r   maprG   rH   rK   rL   )r2   Z	collapserrN   r:   ZkeyedZis_highlightedrC   Zoriginal_groupZhighlighted_groupZkeyed_groupr1   r   r   r   collapse_repeatedK   s    rQ   )frame_or_tbr   c                 C   s$   t t| tjtjf t| tjfS r   )assert_r'   typesr   r   rR   r   r   r   is_frameY   s    rV   c                 c   s*   | }|r&|V  t |r|j}q|j}qd S r   )rV   f_backtb_next)rR   currentr   r   r   
iter_stack^   s    rZ   c                 C   s"   t | r| | jfS | j| jfS d S r   )rV   f_linenotb_frame	tb_linenorU   r   r   r   frame_and_linenoh   s    
r^   )iterablekey_funcr   c                 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)r_   r`   resultrD   r   r   r   group_by_key_funco   s    rc   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)selfrf   r   r   r   __init__   s    zcached_property.__init__c                 C   s(   |d kr| S |  | }|j| j j< |S r   )rf   __dict____name__)rg   obj_clsvaluer   r   r   cached_property_wrapper   s    z'cached_property.cached_property_wrapperN)rj   
__module____qualname__re   rh   rn   __get__r   r   r   r   rd      s   rd   c                    sn   dd l }ddlm} G  fdddt|d}|dd}z|||| }W n tk
rd   |}Y nX | S )Nr   )get_lexer_by_namec                       s   e Zd Z fddZ  ZS )z'_pygmented_with_ranges.<locals>.MyLexerc                 3   sP   d t  |D ]:\}}t fddD r4|j} t|7  ||fV  qd S )Nr   c                 3   s*   | ]"\}}|   ko|k n  V  qd S r   r   ).0r-   r0   lengthr   r   	<genexpr>   s     zE_pygmented_with_ranges.<locals>.MyLexer.get_tokens.<locals>.<genexpr>)super
get_tokensanyZExecutingNoder   )rg   textttyperm   )	__class__rangesrt   r   rx      s    z2_pygmented_with_ranges.<locals>.MyLexer.get_tokens)rj   ro   rp   rx   __classcell__r   r}   )r|   r   MyLexer   s   r   python3F)stripnl)pygmentsZpygments.lexersrr   type	highlight	Exception
splitlines)	formattercoder}   r   rr   r   lexerr>   r   r   r   _pygmented_with_ranges   s    	

r    c                 C   s   | st |trt|}|d S r   )r'   strAssertionError)	conditionerrorr   r   r   rS      s    
rS   c                 C   s*   z
t | W S    dt| j  Y S X d S )Nz<unprintable %s object>)r   r   rj   )rm   r   r   r   some_str   s    
r   )r   )(r)   rG   rT   collectionsr   r   r   r   r   typingr   r   r	   r
   r   r   r   r   Z	asttokensr   r   r   intr   r!   ASTr*   boolrL   rM   rQ   rV   rZ   r^   rc   objectrd   r   rS   r   r   r   r   r   <module>   s,   ( $
 ,
