ó
±xYc        	   @  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 Z d  d l Z d  d l m Z d  d l	 m
 Z
 d Z d e f d „  ƒ  YZ e j ƒ  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   wcwidth(   t   rangeu   Eventu   DummyContextu
   get_cwidthu   suspend_to_background_supportedu   is_conemu_ansiu
   is_windowsu   in_main_threadu   take_using_weightsu   test_callable_argst   Eventc           B  s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   u±  
    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()
    c         C  s/   | |  _  g  |  _ | d  k	 r+ |  | 7}  n  d  S(   N(   t   sendert	   _handlerst   None(   t   selfR   t   handler(    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   __init__.   s    		c         C  s%   x |  j  D] } | |  j ƒ q
 Wd S(   u    Fire event. N(   R   R   (   R   R   (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   __call__5   s    c         C  s   |  ƒ  d S(   u#    Alias for just calling the event. N(    (   R   (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   fire:   s    c         C  sK   t  | ƒ s t ‚ t | d g ƒ s7 t d | ƒ ‚ n  |  j j | ƒ |  S(   u›   
        Add another handler to this callback.
        (Handler should be a callable that takes exactly one parameter: the
        sender object.)
        u%   %r doesn't take exactly one argument.N(   t   callablet   AssertionErrort   test_callable_argsR   t	   TypeErrorR   t   append(   R   R   (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   __iadd__>   s
    c         C  s   |  j  j | ƒ |  S(   u6   
        Remove a handler from this callback.
        (   R   t   remove(   R   R   (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   __isub__M   s    N(	   t   __name__t
   __module__t   __doc__R   R	   R
   R   R   R   (    (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyR      s   			c         C  s  t  | t t f ƒ s t ‚ t t d d ƒ } | d k	 rž y t |  } Wn' t k
 rp | |  ƒ } | t |  <n Xy | j	 | Œ  Wn t
 k
 r– t SXt Snz t j |  ƒ } d „  } | | ƒ } | j d k	 rÕ t St | j ƒ t | j pð g  ƒ t | ƒ k ot | j ƒ k SSd S(   uP   
    Return True when this function can be called with the given arguments.
    u	   signaturec         S  sK   |  \ } } } } | d d !d g k r5 | d } n  t  j | | | | ƒ S(   Ni    i   u   self(   t   inspectt   ArgSpec(   t   spect   argst   varargst   varkwt   defaults(    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt	   drop_selfs   s    N(   t
   isinstancet   listt   tupleR   t   getattrR   R   t   _signatures_cachet   KeyErrort   bindR   t   Falset   Truet
   getargspecR   t   lenR   R   (   t   funcR   t	   signaturet   sigR   R   (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyR   Y   s&    	t   DummyContextc           B  s    e  Z d  Z d „  Z d „  Z RS(   u5   
    (contextlib.nested is not available on Py3)
    c         C  s   d  S(   N(    (   R   (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt	   __enter__ˆ   s    c         G  s   d  S(   N(    (   R   t   a(    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   __exit__‹   s    (   R   R   R   R.   R0   (    (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyR-   „   s   	t   _CharSizesCachec           B  s   e  Z d  Z d „  Z RS(   u"   
    Cache for wcwidth sizes.
    c         C  sc   t  | ƒ d k r* t d t | ƒ ƒ } n t d „  | Dƒ ƒ } t  | ƒ d k  r_ | |  | <n  | S(   Ni   i    c         s  s$   |  ] } t  d  t | ƒ ƒ Vq d S(   i    N(   t   maxR   (   t   .0t   c(    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pys	   <genexpr>›   s    i   (   R)   R2   R   t   sum(   R   t   stringt   result(    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   __missing__“   s    (   R   R   R   R8   (    (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyR1      s   c         C  s   t  |  S(   u?   
    Return width of a string. Wrapper around ``wcwidth``.
    (   t   _CHAR_SIZES_CACHE(   R6   (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt
   get_cwidth¨   s    c           C  s   t  t d ƒ S(   u‰   
    Returns `True` when the Python implementation supports
    suspend-to-background. This is typically `False' on Windows systems.
    u   SIGTSTP(   t   hasattrt   signal(    (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   suspend_to_background_supported¯   s    c           C  s   t  j j d ƒ S(   u)   
    True when we are using Windows.
    u   win(   t   syst   platformt
   startswith(    (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt
   is_windows·   s    c           C  s"   t  ƒ  o! t j j d d ƒ d k S(   u7   
    True when the ConEmu Windows console is used.
    u
   ConEmuANSIu   OFFu   ON(   RA   t   ost   environt   get(    (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   is_conemu_ansi¾   s    c           C  s   t  j ƒ  j j d k S(   u:   
    True when the current thread is the main thread.
    u   _MainThread(   t	   threadingt   current_threadt	   __class__R   (    (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   in_main_threadÅ   s    c   
      c  sO  t  |  t ƒ s t ‚ t  | t ƒ s* t ‚ t d „  | Dƒ ƒ sF t ‚ t |  ƒ t | ƒ k sd t ‚ t |  ƒ d k s| t ‚ g  |  D] } d ^ qƒ } t |  ƒ } t | ƒ } d } x• t rJt } xx | r<t } xe t t	 | ƒ |  | ƒ D]K \ } } }	 | | | |	 t
 | ƒ k  rê | V| | c d 7<t } qê qê WqÅ W| d 7} q¶ Wd S(   uê  
    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(   R   t   int(   R3   t   i(    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pys	   <genexpr>Û   s    i    i   N(   R   R    R   t   allR)   R2   R'   R&   t   zipR   t   float(
   t   itemst   weightsRK   t   already_takent
   item_countt
   max_weightt   addingt   item_it   itemt   weight(    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   take_using_weightsÌ   s&    		((	   u   Eventu   DummyContextu
   get_cwidthu   suspend_to_background_supportedu   is_conemu_ansiu
   is_windowsu   in_main_threadu   take_using_weightsu   test_callable_args(   t
   __future__R    R   RB   R<   R>   RF   t   weakrefR   t	   six.movesR   t   __all__t   objectR   t   WeakKeyDictionaryR#   R   R-   t   dictR1   R9   R:   R=   RA   RE   RI   RX   (    (    (    s3   lib/python2.7/site-packages/prompt_toolkit/utils.pyt   <module>   s:           <	+						