U
    ‰d-  ã                   @   s~   d Z ddlmZ ddlmZ dddddd	d
dgZdd„ Zdd„ Zdd„ Zdd„ Z	ddd„Z
dd	„ Zdd
„ Zdd„ Zdd„ ZdS )zFunctional utilities.é   )ÚThenable©ÚpromiseÚmaybe_promiseÚensure_promiseÚppartialÚpreplaceÚready_promiseÚstarpromiseÚ	transformÚwrapc                 C   s   | rt | tƒst| ƒS | S )zAReturn None if p is unefined, otherwise make sure it's a promise.)Ú
isinstancer   r   ©Úp© r   ú1/tmp/pip-unpacked-wheel-1j4yz0ik/vine/funtools.pyr      s    
c                 C   s   | dkrt ƒ S t| ƒS )zeEnsure p is a promise.

    If p is not a promise, a new promise is created with p' as callback.
    N)r   r   r   r   r   r   r      s    c                 O   s,   t | ƒ} |r|| j | _|r(| j |¡ | S )z-Create/modify promise with partial arguments.)r   ÚargsÚkwargsÚupdate)r   r   r   r   r   r   r      s    c                    s   ‡ ‡‡fdd„}t |ƒS )z¸Replace promise arguments.

    This will force the promise to disregard any arguments
    the promise is fulfilled with, and to be called with the
    provided arguments instead.
    c                     s
   ˆˆ ˆŽS ©Nr   )Ú_Ú__©r   r   r   r   r   Ú	_replacer/   s    zpreplace.<locals>._replacerr   )r   r   r   r   r   r   r   r   (   s    Nc                 G   s   t | ƒ}||Ž  |S )z)Create promise that is already fulfilled.)r   )Úcallbackr   r   r   r   r   r	   4   s    c                 O   s   t | ||ƒS )z%Create promise, using star arguments.r   )Zfunr   r   r   r   r   r
   ;   s    c                 O   s.   t |ƒ}tt| |||fƒ}| tƒ |j¡ |S )a?  Filter final argument to a promise.

    E.g. to coerce callback argument to :class:`int`::

        transform(int, callback)

    or a more complex example extracting something from a dict
    and coercing the value to :class:`float`:

    .. code-block:: python

        def filter_key_value(key, filter_, mapping):
            return filter_(mapping[key])

        def get_page_expires(self, url, callback=None):
            return self.request(
                'GET', url,
                callback=transform(get_key, callback, 'PageExpireValue', int),
            )

    )r   r   Ú
_transbackÚthenÚthrow)Úfilter_r   Zfilter_argsZfilter_kwargsÚPr   r   r   r   @   s    c                 C   s>   z| ||f |Ž}W n t k
r0   | ¡  Y n
X ||ƒS d S r   )Ú	Exceptionr   )r   r   r   r   Úretr   r   r   r   \   s
    r   c                    s   ‡ fdd„}|S )zžWrap promise.

    This wraps the promise such that if the promise is called with a promise as
    argument, we attach ourselves to that promise instead.
    c                     s6   t | ƒdkr(t| d tƒr(| d  ˆ ¡S ˆ | |ŽS d S )Nr   é    )Úlenr   r   r   )r   r   r   r   r   Úon_callk   s    zwrap.<locals>.on_callr   )r   r$   r   r   r   r   e   s    )N)Ú__doc__Zabstractr   Zpromisesr   Ú__all__r   r   r   r   r	   r
   r   r   r   r   r   r   r   Ú<module>   s(        ý


	