File: //usr/local/lib/python3.9/site-packages/vine/__pycache__/funtools.cpython-39.pyc
a
X>h. � @ sr d Z ddlmZ ddlmZ g d�Zdd� Zdd� Zd d
� Zdd� Z ddd�Z
dd� Zdd� Zdd� Z
dd� Zd
S )zFunctional utilities.� )�Thenable��promise)�
maybe_promise�ensure_promise�ppartial�preplace�
ready_promise�starpromise� transform�wrapc C s | rt | t�st| �S | S )zBReturn None if p is undefined, otherwise make sure it's a promise.)�
isinstancer r ��p� r �7/usr/local/lib/python3.9/site-packages/vine/funtools.pyr s
r c C s | du rt � 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 r 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 r c s � ��fdd�}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 �� i ���S �Nr )�_�__�r r r r r � _replacer/ s zpreplace.<locals>._replacerr )r r r r r r r r ( s r Nc G s t | �}||� |S )z)Create promise that is already fulfilled.)r )�callbackr r r r r r 4 s r c O s t | ||�S )z%Create promise, using star arguments.r )Zfunr r r r r r
; s r
c O s. t |�}tt| |||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 Zfilter_args�
filter_kwargs�Pr r r r @ s r c C s@ z| ||f i |��}W n t y2 |�� Y n
0 ||�S d S r )� Exceptionr )r r r r �retr r r r \ s
r c s � fdd�}|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 s: t | �dkr(t| d t�r(| d �� �S � | i |��S d S )Nr � )�lenr
r r )r r r r r �on_callk s zwrap.<locals>.on_callr )r r% r r r r e s r )N)�__doc__Zabstractr Zpromisesr �__all__r r r r r r
r r r r r r r �<module> s