B
    't\#                 @   sd  d dl mZ d dlZd dl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 d dlmZmZ d dlmZ d dlmZ dd	lmZ d
dddddddddddgZG dd
 d
eZe ZeeddZdd Zedddd ZG dd deZG dd deZe Z d d Z!d!d Z"d"d Z#d#d$ Z$d%d Z%d&d Z&d'd( Z'd)d Z(d*d Z)d+d Z*d,d Z+dS )-    )unicode_literalsN)deque)partial)PY2	text_type)range)wcwidth   )memoizedEventDummyContext
get_cwidthsuspend_to_background_supportedis_conemu_ansi
is_windowsin_main_threadtake_using_weightstest_callable_argsto_strto_intto_floatc               @   sJ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	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()
    Nc             C   s    || _ g | _|d k	r| |7 } d S )N)sender	_handlers)selfr   handler r   3lib/python3.7/site-packages/prompt_toolkit/utils.py__init__4   s    zEvent.__init__c             C   s   x| j D ]}|| j qW dS )z Fire event. N)r   r   )r   r   r   r   r   __call__;   s    zEvent.__call__c             C   s
   |   dS )z# Alias for just calling the event. Nr   )r   r   r   r   fire@   s    z
Event.firec             C   s0   t |stt|s td| | j| dS )z
        Add another handler to this callback.
        (Handler should be a callable that takes exactly one parameter: the
        sender object.)
        z%%r doesn't take exactly one argument.N)callableAssertionError_func_takes_one_arg	TypeErrorr   append)r   r   r   r   r   add_handlerD   s    zEvent.add_handlerc             C   s   || j kr| j | dS )z6
        Remove a handler from this callback.
        N)r   remove)r   r   r   r   r   remove_handlerR   s    
zEvent.remove_handlerc             C   s   |  | | S )z3 `event += handler` notation for adding a handler. )r%   )r   r   r   r   r   __iadd__Y   s    
zEvent.__iadd__c             C   s   |  | | S )z5 `event -= handler` notation for removing a handler. )r'   )r   r   r   r   r   __isub__^   s    
zEvent.__isub__)N)__name__
__module____qualname____doc__r   r   r   r%   r'   r(   r)   r   r   r   r   r       s   
Z	signaturec             C   s   t |ttfsttdk	rvyt|  }W n$ tk
rJ   t| }|t| < Y nX y|j|  W n tk
rn   dS X dS nlt | t	rdS t
| }dd }||}|jdk	rdS t|jt|jpg  t|  kot|jkS   S dS )zP
    Return True when this function can be called with the given arguments.
    NFTc             S   s:   | \}}}}|dd dgkr*|dd  }t ||||S )Nr   r	   r   )inspectZArgSpec)specargsvarargsZvarkwdefaultsr   r   r   	drop_self   s    z%test_callable_args.<locals>.drop_self)
isinstancelisttupler!   _inspect_signature_signatures_cacheKeyErrorZbindr#   r   r.   Z
getargspecr1   lenr0   r2   )funcr0   Zsigr/   r3   r   r   r   r   j   s(    


i   )maxsizec             C   s   t | dgS )zR
    Test whether the given function can be called with exactly one argument.
    N)r   )r;   r   r   r   r"      s    r"   c               @   s    e Zd ZdZdd Zdd ZdS )r   z5
    (contextlib.nested is not available on Py3)
    c             C   s   d S )Nr   )r   r   r   r   	__enter__   s    zDummyContext.__enter__c             G   s   d S )Nr   )r   ar   r   r   __exit__   s    zDummyContext.__exit__N)r*   r+   r,   r-   r=   r?   r   r   r   r   r      s   c                   s0   e Zd ZdZdZdZ fddZdd Z  ZS )_CharSizesCachez"
    Cache for wcwidth sizes.
    @      c                s   t t|   t | _d S )N)superr@   r   r   _long_strings)r   )	__class__r   r   r      s    z_CharSizesCache.__init__c                s   t |dkrtdt|}nt fdd|D }| |< t | jkr| j}|| t | jkr|| }| kr| |= |S )Nr	   r   c             3   s   | ]} | V  qd S )Nr   ).0c)r   r   r   	<genexpr>   s    z._CharSizesCache.__missing__.<locals>.<genexpr>)	r:   maxr   sumLONG_STRING_MIN_LENrD   r$   MAX_LONG_STRINGSpopleft)r   stringresultZlong_stringsZkey_to_remover   )r   r   __missing__   s    
z_CharSizesCache.__missing__)	r*   r+   r,   r-   rK   rL   r   rP   __classcell__r   r   )rE   r   r@      s
   r@   c             C   s   t |  S )z?
    Return width of a string. Wrapper around ``wcwidth``.
    )_CHAR_SIZES_CACHE)rN   r   r   r   r      s    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    c               C   s   t jdS )z)
    True when we are using Windows.
    win)sysplatform
startswithr   r   r   r   r      s    c              C   s   ddl m}  t o|  S )zS
    True when we are using Windows, but VT100 escape sequences are supported.
    r   )is_win_vt100_enabled)Zprompt_toolkit.output.windows10rZ   r   )rZ   r   r   r   is_windows_vt100_supported   s    r[   c               C   s   t  otjdddkS )z7
    True when the ConEmu Windows console is used.
    Z
ConEmuANSIZOFFZON)r   osenvirongetr   r   r   r   r      s    c               C   s   t  jjdkS )z:
    True when the current thread is the main thread.
    Z_MainThread)	threadingZcurrent_threadrE   r*   r   r   r   r   r      s    c              C   s    t jdd} tr| d} | S )z( Return the $TERM environment variable. ZTERM zutf-8)r\   r]   r^   r   decode)Ztermr   r   r   get_term_environment_variable  s    
rb   c             c   s@  t | tstt |tsttdd |D s2tt| t|ksFtt| dksVtg }g }x2t| |D ]$\}}|dkrj|| || qjW |} |}| stddd | D }t| }t|}d}xrd}	x`|	r.d}	xPtt	|| |D ]<\}
}}||
 || t
| k r|V  ||
  d	7  < d}	qW qW |d	7 }qW 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.)
    c             s   s   | ]}t |tV  qd S )N)r4   int)rF   ir   r   r   rH     s    z%take_using_weights.<locals>.<genexpr>r   z+Did't got any items with a positive weight.c             S   s   g | ]}d qS )r   r   )rF   rd   r   r   r   
<listcomp>-  s    z&take_using_weights.<locals>.<listcomp>TFr	   N)r4   r5   r!   allr:   zipr$   
ValueErrorrI   r   float)itemsZweightsZitems2Zweights2rd   wZalready_takenZ
item_countZ
max_weightZaddingZitem_iitemZweightr   r   r   r   
  s:    
c             C   s   t | rt|  S t| S dS )z& Turn callable or string into string. N)r    r   r   )valuer   r   r   r   A  s    
c             C   s   t | rt|  S t| S dS )z  Turn callable or int into int. N)r    r   rc   )rm   r   r   r   r   I  s    
c             C   s   t | rt|  S t| S dS )z$ Turn callable or float into float. N)r    r   ri   )rm   r   r   r   r   Q  s    
),Z
__future__r   r.   r\   rU   rW   r_   weakrefcollectionsr   	functoolsr   Zsixr   r   Z	six.movesr   r   cacher
   __all__objectr   WeakKeyDictionaryr8   getattrr7   r   r"   r   dictr@   rR   r   r   r   r[   r   r   rb   r   r   r   r   r   r   r   r   <module>   sV   E.'	7