a
    õ£BeÂ!  ã                   @  s¶  d dl mZ d dlZd dlZd dlZd dlZd dl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 g d¢Zdejv Zedd	d
�ZG dd„ dee ƒZG dd„ de
d ƒZG dd„ deeef ƒZeƒ Zdddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Z ddœd d!„Z!ddœd"d#„Z"ddœd$d%„Z#ed&ƒZ$d'd(d)d*œd+d,„Z%d-dd.œd/d0„Z&d1dd.œd2d3„Z'ee	g e(f e(f Z)d4d5d.œd6d7„Z*d<d8dd9œd:d;„Z+dS )=é    )ÚannotationsN)Údeque)ÚCallableÚContextManagerÚDequeÚDictÚ	GeneratorÚGenericÚTypeVarÚUnion)Úwcwidth)ÚEventÚDummyContextÚ
get_cwidthÚsuspend_to_background_supportedÚis_conemu_ansiÚ
is_windowsÚin_main_threadÚget_bell_environment_variableÚget_term_environment_variableÚtake_using_weightsÚto_strÚto_intÚAnyFloatÚto_floatÚis_dumb_terminalzsphinx.ext.autodocÚ_SenderT)Ú	covariantc                   @  s€   e Zd ZdZdddddœdd„Zdd	œd
d„Zdd	œdd„Zdddœdd„Zdddœdd„Zdddœdd„Z	dddœdd„Z
dS )r   a±  
    Simple event to which event handlers can be attached. For instance::

        class Cls:
            def __init__(self):
                # Define event. The first parameter is the sender.
                self.event = Event(self)

        obj = Cls()

        def handler(sender):
            pass

        # Add event handler by using the += operator.
        obj.event += handler

        # Fire event.
        obj.event()
    Nr   z Callable[[_Sender], None] | NoneÚNone)ÚsenderÚhandlerÚreturnc                 C  s    || _ g | _|d ur| |7 } d S ©N)r   Ú	_handlers)Úselfr   r    © r%   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/utils.pyÚ__init__C   s    zEvent.__init__©r!   c                 C  s   | j D ]}|| jƒ qdS )zFire event.N)r#   r   ©r$   r    r%   r%   r&   Ú__call__L   s    
zEvent.__call__c                 C  s
   | ƒ  dS )z!Alias for just calling the event.Nr%   ©r$   r%   r%   r&   ÚfireQ   s    z
Event.firezCallable[[_Sender], None])r    r!   c                 C  s   | j  |¡ dS )z›
        Add another handler to this callback.
        (Handler should be a callable that takes exactly one parameter: the
        sender object.)
        N)r#   Úappendr)   r%   r%   r&   Úadd_handlerU   s    zEvent.add_handlerc                 C  s   || j v r| j  |¡ dS )z6
        Remove a handler from this callback.
        N)r#   Úremover)   r%   r%   r&   Úremove_handler^   s    
zEvent.remove_handlerzEvent[_Sender]c                 C  s   |   |¡ | S )zC
        `event += handler` notation for adding a handler.
        )r.   r)   r%   r%   r&   Ú__iadd__e   s    
zEvent.__iadd__c                 C  s   |   |¡ | S )zE
        `event -= handler` notation for removing a handler.
        )r0   r)   r%   r%   r&   Ú__isub__l   s    
zEvent.__isub__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r*   r,   r.   r0   r1   r2   r%   r%   r%   r&   r   .   s    ÿ		r   c                   @  s.   e Zd ZdZddœdd„Zdddœdd	„Zd
S )r   z5
    (contextlib.nested is not available on Py3)
    r   r(   c                 C  s   d S r"   r%   r+   r%   r%   r&   Ú	__enter__y   s    zDummyContext.__enter__Úobject)Úar!   c                 G  s   d S r"   r%   )r$   r9   r%   r%   r&   Ú__exit__|   s    zDummyContext.__exit__N)r3   r4   r5   r6   r7   r:   r%   r%   r%   r&   r   t   s   r   c                      s>   e Zd ZdZdZdZddœ‡ fdd„Zdd	d
œdd„Z‡  ZS )Ú_CharSizesCachez"
    Cache for wcwidth sizes.
    é@   é   r   r(   c                   s   t ƒ  ¡  tƒ | _d S r"   )Úsuperr'   r   Ú_long_stringsr+   ©Ú	__class__r%   r&   r'   ˆ   s    
z_CharSizesCache.__init__ÚstrÚint©Ústringr!   c                   s€   t |ƒdkrtdt|ƒƒ}nt‡ fdd„|D ƒƒ}|ˆ |< t |ƒˆ jkr|ˆ j}| |¡ t |ƒˆ jkr|| ¡ }|ˆ v r|ˆ |= |S )Né   r   c                 3  s   | ]}ˆ | V  qd S r"   r%   )Ú.0Úcr+   r%   r&   Ú	<genexpr>–   ó    z._CharSizesCache.__missing__.<locals>.<genexpr>)	ÚlenÚmaxr   ÚsumÚLONG_STRING_MIN_LENr?   r-   ÚMAX_LONG_STRINGSÚpopleft)r$   rE   ÚresultZlong_stringsZkey_to_remover%   r+   r&   Ú__missing__�   s    
z_CharSizesCache.__missing__)	r3   r4   r5   r6   rN   rO   r'   rR   Ú__classcell__r%   r%   r@   r&   r;   €   s
   r;   rB   rC   rD   c                 C  s   t |  S )z?
    Return width of a string. Wrapper around ``wcwidth``.
    )Ú_CHAR_SIZES_CACHE)rE   r%   r%   r&   r   ¬   s    r   Úboolr(   c                   C  s
   t tdƒS )z‰
    Returns `True` when the Python implementation supports
    suspend-to-background. This is typically `False' on Windows systems.
    ÚSIGTSTP)ÚhasattrÚsignalr%   r%   r%   r&   r   ³   s    r   c                   C  s
   t jdkS )z)
    True when we are using Windows.
    Úwin32)ÚsysÚplatformr%   r%   r%   r&   r   »   s    r   c                  C  s    t jdkrddlm}  | ƒ S dS )zS
    True when we are using Windows, but VT100 escape sequences are supported.
    rY   r   ©Úis_win_vt100_enabledF)rZ   r[   Zprompt_toolkit.output.windows10r]   r\   r%   r%   r&   Úis_windows_vt100_supportedÂ   s    
r^   c                   C  s   t jdkotj dd¡dkS )z7
    True when the ConEmu Windows console is used.
    rY   Z
ConEmuANSIZOFFÚON)rZ   r[   ÚosÚenvironÚgetr%   r%   r%   r&   r   Ï   s    r   c                   C  s   t  ¡ jjdkS )z:
    True when the current thread is the main thread.
    Ú_MainThread)Ú	threadingÚcurrent_threadrA   r3   r%   r%   r%   r&   r   Ö   s    r   c                  C  s   t j dd¡} |  ¡ dv S )zD
    True if env variable is set to true (true, TRUE, True, 1).
    ZPROMPT_TOOLKIT_BELLÚtrue)Ú1rf   )r`   ra   rb   Úlower©Úvaluer%   r%   r&   r   Ý   s    r   c                   C  s   t j dd¡S )z&Return the $TERM environment variable.ÚTERMÚ )r`   ra   rb   r%   r%   r%   r&   r   å   s    r   Ú_Tzlist[_T]z	list[int]zGenerator[_T, None, None])ÚitemsÚweightsr!   c                 c  sü   t | ƒt |ƒksJ ‚t | ƒdks$J ‚g }g }t| |ƒD ]$\}}|dkr6| |¡ | |¡ q6|} |}| sptdƒ‚dd„ | D ƒ}t | ƒ}t|ƒ}d}	d}
|
rîd}
tt|ƒ| |ƒD ]<\}}}|| |	| t|ƒ k r®|V  ||  d7  < d}
q®q–|	d7 }	q’dS )	aê  
    Generator that keeps yielding items from the items list, in proportion to
    their weight. For instance::

        # Getting the first 70 items from this generator should have yielded 10
        # times A, 20 times B and 40 times C, all distributed equally..
        take_using_weights(['A', 'B', 'C'], [5, 10, 20])

    :param items: List of items to take from.
    :param weights: Integers representing the weight. (Numbers have to be
                    integers, not floats.)
    r   z+Did't got any items with a positive weight.c                 S  s   g | ]}d ‘qS )r   r%   )rG   Úir%   r%   r&   Ú
<listcomp>  rJ   z&take_using_weights.<locals>.<listcomp>TFrF   N)rK   Úzipr-   Ú
ValueErrorrL   ÚrangeÚfloat)rn   ro   Zitems2Zweights2ÚitemÚwZalready_takenÚ
item_countZ
max_weightrp   ZaddingZitem_iÚweightr%   r%   r&   r   í   s2    
r   zCallable[[], str] | str)rj   r!   c                 C  s   t | ƒrt| ƒ ƒS t| ƒS dS )z$Turn callable or string into string.N)Úcallabler   rB   ri   r%   r%   r&   r   #  s    
r   zCallable[[], int] | intc                 C  s   t | ƒrt| ƒ ƒS t| ƒS dS )zTurn callable or int into int.N)rz   r   rC   ri   r%   r%   r&   r   +  s    
r   r   ru   c                 C  s   t | ƒrt| ƒ ƒS t| ƒS dS )z"Turn callable or float into float.N)rz   r   ru   ri   r%   r%   r&   r   6  s    
r   z
str | None)Útermr!   c                 C  s&   | du rt tj dd¡ƒS |  ¡ dv S )z»
    True if this terminal type is considered "dumb".

    If so, we should fall back to the simplest possible form of line editing,
    without cursor positioning and color support.
    Nrk   rl   )ÚdumbÚunknown)r   r`   ra   rb   rh   )r{   r%   r%   r&   r   >  s    r   )N),Ú
__future__r   r`   rX   rZ   rd   Úcollectionsr   Útypingr   r   r   r   r   r	   r
   r   r   Ú__all__ÚmodulesZSPHINX_AUTODOC_RUNNINGr   r   r   rB   rC   r;   rT   r   r   r   r^   r   r   r   r   rm   r   r   r   ru   r   r   r   r%   r%   r%   r&   Ú<module>   s:   (
F)6