a
    ÷£Be
  ã                   @   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   úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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 ]˜\}‰|rœtˆƒ‰dgtˆƒ ‰‡fdd„}tˆƒD ]D‰|‡‡fdd„ƒ‰ˆd u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 y   Y d S 0 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 u 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                    sl   dd l }ddlm} G ‡ fdd„dt|dƒƒƒ}|dd�}z| ||| ¡}W n tyb   |}Y n0 | ¡ 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>Ÿ   r8   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 0 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,   ( $
 ,
