o
    Nign                  
   @   s  d dl mZ d dlmZ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lmZ dd	lmZ dd
lmZmZmZmZ ddlmZmZmZmZmZmZ ddlmZ ddl m!Z! d dl"m#Z#m$Z$ 	 e! a0e Z1dd Z2dd Z3ed  dkZ4dZ5dZ6dZ7d Z8dZ9dZ:e;ee e<e=e>e?e@eAeef ZBdZCd ZDdZEdd ZFdd ZGe#dZHe#dd d!ZIG d"d# d#e$eH ZJe ZKd$d% ZLeJjMZMeJjNZOeJjPZPd&d' ZQdS )(    )
namedtuple)partialwraps)version_infoexc_info)RLock)TracebackType)WeakKeyDictionary)reraise   )Async)Futureensure_futureiscoroutineiterate_promise)
deprecatedinteger_typesstring_types	text_typebinary_typewarn)PromiseList)ImmediateScheduler)TypeVarGenericF)TypeListAnyCallableDictIteratorOptionalTupleUnionr   HashableMutableMappingc                   C   s   t S Ndefault_scheduler r)   r)   C/home/ubuntu/.local/lib/python3.10/site-packages/promise/promise.pyget_default_scheduler/   s   r+   c                 C   s   | a d S r&   r'   )	schedulerr)   r)   r*   set_default_scheduler4      r-      Ni     c                   C   s   t dS )NzPromise is self)	TypeErrorr)   r)   r)   r*   make_self_resolution_errorO   r.   r3   c              
   O   sP   z
| |i |d fW S  t y' } zt d }d ||ffW  Y d }~S d }~ww Nr/   )	Exceptionr   )handlerargskwargsetbr)   r)   r*   	try_catchT   s   
r;   TST)contravariantc                   @   s  e Zd ZdZeZdZdZdZdZ	dZ
dZdZdZdZdZdZdZdZdpddZedd Zed	d
 Zdd ZeZeddddd Zeddddd Zdd ZdqddZdd ZdrddZdd Z dsd d!Z!d"d# Z"d$d% Z#d&d' Z$d(d) Z%d*d+ Z&d,d- Z'd.d/ Z(d0d1 Z)d2d3 Z*d4d5 Z+d6d7 Z,d8d9 Z-d:d; Z.d<d= Z/d>d? Z0d@dA Z1dBdC Z2e3drdDdEZ4drdFdGZ5drdHdIZ6dqdJdKZ7e7 Z8Z9ee7 Z:Z;dLdM Z<edNdO Z=edPdQ Z>edRdS Z?dTdU Z@		dpdVdWZAeZBeZCe!ZDdpdXdYZEdpdZd[ZFdrd\d]ZGdrd^d_ZHe3d`da ZIe3dbdc ZJeJZKe3ddde ZLeLZMeMZNe3dfdg ZOdZPe3dhdi ZQe3djdk ZRe3dldm ZSe3dndo ZTdS )tPromisezP
    This is the Promise class that complies
    Promises/A+ specification.
    Fr   Nc                 C   s    || _ |dur| | dS dS )z>
        Initialize the Promise into a pending state.
        N)
_scheduler_resolve_from_executor)selfexecutorr,   r)   r)   r*   __init__|   s   zPromise.__init__c                 C   s
   | j ptS r&   )r@   r(   rB   r)   r)   r*   r,      s   
zPromise.schedulerc                 C   s(   | j st | _ | | j j| j j | j S r&   )_futurer   _then
set_resultset_exceptionrE   r)   r)   r*   future   s   zPromise.futurec                 C   s   t |  S r&   )r   _targetrE   r)   r)   r*   __iter__   s   zPromise.__iter__zRejecting directly in a Promise instance is deprecated, as Promise.reject() is now a class method. Please use promise.do_reject() instead.reject)namec                 C      |  | d S r&   )	do_reject)rB   r9   r)   r)   r*   _deprecated_reject      zPromise._deprecated_rejectzResolving directly in a Promise instance is deprecated, as Promise.resolve() is now a class method. Please use promise.do_resolve() instead.resolvec                 C   rO   r&   )
do_resolve)rB   valuer)   r)   r*   _deprecated_resolve   rR   zPromise._deprecated_resolvec                 C   s   || u r|  t dS | |s| |S | | }|| kr(| t  d S |jtkrT| j	}|dkr9|
|  td|D ]}|| | q>d| _d| _	| | d S |jtkrb| |  d S |jtkrt| | | j d S d S )NFr   r   T)_reject_callbackr3   is_thenable_fulfill_try_convert_to_promiserK   _reject_stateSTATE_PENDING_length_migrate_callback0range_migrate_callback_at_is_following_set_followeeSTATE_FULFILLED_valueSTATE_REJECTED_reason
_traceback)rB   rU   promiselenir)   r)   r*   _resolve_callback   s,   





zPromise._resolve_callbackc                 C   sJ   | j rJ | jtkr| jS | jtkr#|r | j}tt||| j | jS d S r&   )	rb   r\   rd   _rejection_handler0rf   _fulfillment_handler0r
   typerh   )rB   _raise	raise_valr)   r)   r*   _settled_value   s   


zPromise._settled_valuec                 C   sR   || u rt  }| |S t| _|| _| jdkr'| jr |   d S t	|  d S d S Nr   )
r3   r[   rd   r\   rm   r^   _is_async_guaranteed_settle_promisesasync_instancesettle_promises)rB   rU   errr)   r)   r*   rY      s   

zPromise._fulfillc                 C   sv   t | _|| _|| _| jr| jdksJ t|| j d S | jdkr't	|  n| 
  | jr4|   d S t	|  d S rs   )rf   r\   rn   rh   	_is_finalr^   rv   fatal_errorr,   rw   "_ensure_possible_rejection_handledrt   ru   )rB   reason	tracebackr)   r)   r*   r[      s   
zPromise._rejectc                 C   s   d S r&   r)   rE   r)   r)   r*   r{   	  s   z*Promise._ensure_possible_rejection_handledc                 C   s(   t |tsJ d|| || d S )Nz+A promise was rejected with a non-error: {})
isinstancer5   formatr[   )rB   r|   synchronousr}   r)   r)   r*   rW     s   zPromise._reject_callbackc                 C   sP   | j rJ |dksJ |t t }d | j|t < d | j|t < d | j|t < d S rs   )rb   CALLBACK_SIZE	_handlersCALLBACK_PROMISE_OFFSETCALLBACK_FULFILL_OFFSETCALLBACK_REJECT_OFFSET)rB   indexbaser)   r)   r*   _clear_callback_data_index_at  s   
z%Promise._clear_callback_data_index_atc                 C   B   t d|D ]}| |}| |}| | | |||d  qd S Nr   )r`   _fulfillment_handler_at_promise_atr   _settle_promise)rB   lengthrU   rk   r6   ri   r)   r)   r*   _fulfill_promises"     


zPromise._fulfill_promisesc                 C   r   r   )r`   _rejection_handler_atr   r   r   )rB   r   r|   rk   r6   ri   r)   r)   r*   _reject_promises*  r   zPromise._reject_promisesc                 C   s   | j rJ t|| j}| j}t|r(|s|| d S |rd|_| ||| d S |rD|r/d|_| jtkr;|| d S |	|| j
 d S d S NT)rb   r~   	__class__rt   callable_settle_promise_from_handlerr\   rd   rY   r[   rh   )rB   ri   r6   rU   r}   
is_promiseasync_guaranteedr)   r)   r*   r   2  s$   

zPromise._settle_promisec                 C   s    | j }d | _ | |||| d S r&   )	_promise0r   )rB   r6   rU   r}   ri   r)   r)   r*   _settle_promise0N  s   zPromise._settle_promise0c                 C   s:   t ||\}}|r|\}}||d| d S || d S )NF)r;   rW   rl   )rB   r6   rU   ri   error_with_tberrorr:   r)   r)   r*   r   Y  s
   z$Promise._settle_promise_from_handlerc                 C   s.   |dksJ | j rJ | j|t t t S rs   )rb   r   getr   r   rB   r   r)   r)   r*   r   c  s
   
zPromise._promise_atc                 C   .   | j rJ |dksJ | j|t t t S rs   )rb   r   r   r   r   r   r)   r)   r*   r   k  
   
zPromise._fulfillment_handler_atc                 C   r   rs   )rb   r   r   r   r   r   r)   r)   r*   r   s  r   zPromise._rejection_handler_atc                 C   s   |  |j|j|j d S r&   )_add_callbacksrn   rm   r   )rB   followerr)   r)   r*   r_   {  s
   zPromise._migrate_callback0c                 C   s$   |  |||||| d S r&   )r   r   r   r   )rB   r   r   r)   r)   r*   ra     s
   zPromise._migrate_callback_atc                 C   s
  | j rJ | jd u ri | _| j}|tt krd}d| _|dkr@| jr$J | jr)J | jr.J || _t|r8|| _t|r?|| _n>|t t }|t	 | jvsOJ |t
 | jvsXJ |t | jvsaJ || j|t	 < t|rs|| j|t
 < t|r~|| j|t < |d | _|S )Nr   r   )rb   r   r^   
MAX_LENGTHr   r   rn   rm   r   r   r   r   )rB   fulfillrM   ri   r   r   r)   r)   r*   r     s8   





zPromise._add_callbacksc                 C   s   | }|j r| }|j s|S r&   )rb   	_followee)rB   retr)   r)   r*   rK     s
   zPromise._targetc                 C   s    | j sJ t| jtsJ | jS r&   rb   r~   rm   r?   rE   r)   r)   r*   r     s   
zPromise._followeec                 C   s$   | j sJ t| jtrJ || _d S r&   r   )rB   ri   r)   r)   r*   rc     s   

zPromise._set_followeec                 C   sr   | j }|dkr7| jtkr!| j}| j}| | j|| | || n| j}| | j|d  | || d| _ d S d S rs   )	r^   r\   rf   rn   rh   r   rm   r   r   )rB   r   r|   r}   rU   r)   r)   r*   ru     s   

zPromise._settle_promisesc              
      s   d fdd}d fdd	}d }d }z||| W n t y4 } zt d }|}W Y d }~nd }~ww d|d urD |d| d S d S )	NTc                    s     |  d S r&   )rl   )rU   rE   r)   r*   rS        z/Promise._resolve_from_executor.<locals>.resolvec                    s     | | d S r&   rW   )r|   r}   rB   r   r)   r*   rM        z.Promise._resolve_from_executor.<locals>.rejectr/   Fr&   )r5   r   rW   )rB   rC   rS   rM   r   r}   r9   r)   r   r*   rA     s    
zPromise._resolve_from_executorc                 C   s   t || d S r&   )rv   wait)clsri   timeoutr)   r)   r*   r     s   zPromise.waitc                 C   s   |  | | d S r&   )r   )rB   r   r)   r)   r*   _wait  s   zPromise._waitc                 C   s"   |   }| |p	t | jddS )NT)rp   )rK   r   DEFAULT_TIMEOUT_target_settled_value)rB   r   targetr)   r)   r*   r     s   zPromise.getc                 C   s   |   |S r&   )rK   rr   )rB   rp   r)   r)   r*   r     r   zPromise._target_settled_valuec                 C   sr   t t| }| jrd||  S | j}|tkrd|S |tkr*d|t| j	S |t
kr7d|t| jS dS )Nz<Promise at {} following {}>z<Promise at {} pending>z!<Promise at {} fulfilled with {}>z <Promise at {} rejected with {}>z<Promise unknown>)hexidrb   r   rK   r\   r]   rd   reprrm   rf   rn   )rB   hex_idstater)   r)   r*   __repr__	  s   


zPromise.__repr__c                 C      |   jtkS )z^Indicate whether the Promise is still pending. Could be wrong the moment the function returns.)rK   r\   r]   rE   r)   r)   r*   
is_pending     zPromise.is_pendingc                 C   r   )z`Indicate whether the Promise has been fulfilled. Could be wrong the moment the function returns.)rK   r\   rd   rE   r)   r)   r*   is_fulfilled"  r   zPromise.is_fulfilledc                 C   r   )z_Indicate whether the Promise has been rejected. Could be wrong the moment the function returns.)rK   r\   rf   rE   r)   r)   r*   is_rejected(  r   zPromise.is_rejectedc                 C   s   |  d|S )z
        This method returns a Promise and deals with rejected cases only.
        It behaves the same as calling Promise.then(None, on_rejection).
        N)then)rB   on_rejectionr)   r)   r*   catch.  s   zPromise.catchc           	      C   s   |   }|  }|j}|tkr|||| |S d }|tkr$|j}|}n|tkr0|j}|j	}|}t
t|j|||||j |S r&   )r   rK   r\   r]   r   rd   rm   rf   rn   rh   rv   invoker   r   r,   )	rB   did_fulfill
did_rejectri   r   r   r}   rU   r6   r)   r)   r*   rG   6  s&   zPromise._thenc                 C   s   |  ||S )a  
        This method takes two optional arguments.  The first argument
        is used if the "self promise" is fulfilled and the other is
        used if the "self promise" is rejected.  In either case, this
        method returns another promise that effectively represents
        the result of either the first of the second argument (in the
        case that the "self promise" is fulfilled or rejected,
        respectively).
        Each argument can be either:
          * None - Meaning no action is taken
          * A function - which will be called with either the value
            of the "self promise" or the reason for rejection of
            the "self promise".  The function may return:
            * A value - which will be used to fulfill the promise
              returned by this method.
            * A promise - which, when fulfilled or rejected, will
              cascade its value or reason to the promise returned
              by this method.
          * A value - which will be assigned as either the value
            or the reason for the promise returned by this method
            when the "self promise" is either fulfilled or rejected,
            respectively.
        :type success: (Any) -> object
        :type failure: (Any) -> object
        :rtype : Promise
        )rG   )rB   r   r   r)   r)   r*   r   Z  s   zPromise.thenc                 C   s   |  ||}d|_d S r   )rG   ry   )rB   r   r   ri   r)   r)   r*   donex  s   
zPromise.donec                 C   sl   |sdS |D ]-}t |tr|\}}| || qt |tr.|d}|d}| || q| | qdS )zb
        :type handlers: list[(Any) -> object] | list[((Any) -> object, (Any) -> object)]
        Nsuccessfailure)r~   tupler   dictr   )rB   handlersr6   sfr)   r)   r*   done_all}  s   



zPromise.done_allc                 C   s   |sg S g }|D ]6}t |tr|\}}|| || qt |tr6|d}|d}|| || q|| | q|S )a  
        Utility function which calls 'then' for each handler provided. Handler can either
        be a function in which case it is used as success handler, or a tuple containing
        the success and the failure handler, where each of them could be None.
        :type handlers: list[(Any) -> object] | list[((Any) -> object, (Any) -> object)]
        :param handlers
        :rtype : list[Promise]
        r   r   )r~   r   appendr   r   r   )rB   r   promisesr6   r   r   r)   r)   r*   then_all  s   




zPromise.then_allc                    sj    j }t|tr| tur|  j jS  S t r t   j }t|r3 fdd}| |} |_|S  S )Nc                    s.      rt| |  d S  t| | d S r&   )r   _process_future_resultadd_done_callbackrS   rM   objr)   r*   rC     s   z1Promise._try_convert_to_promise.<locals>.executor)	r   
issubclassr?   r   r@   r   r   is_future_likerF   )r   r   _typerC   ri   r)   r   r*   rZ     s   
zPromise._try_convert_to_promisec                 C   s   |  }| |d |S r   r   )r   r|   r   r)   r)   r*   rM     s   zPromise.rejectc                 C   s*   |  |s|  }t|_||_|S | |S r&   )rX   rd   r\   rm   rZ   )r   r   r   r)   r)   r*   rS     s   

zPromise.resolvec                    s4   t std  S t fdd}|S )NzRPromise.promisify is now a function decorator, please use Promise.resolve instead.c                     s    fdd}|S )Nc                    s   |  i S r&   r)   r   )r7   r   r8   r)   r*   rC     r   z4Promise.promisify.<locals>.wrapper.<locals>.executorr)   )r7   r8   rC   r   r   r7   r8   r*   wrapper  s   z"Promise.promisify.<locals>.wrapper)r   r   rS   r   )r   r   r   r)   r   r*   	promisify  s   
zPromise.promisifyc                    s8   ddl m}  jstd  _| fdd}|S )Nr   )r   c                     s   j  fddS )Nc                    s    i S r&   r)   )v)r7   fnr8   r)   r*   <lambda>  s    z/Promise.safe.<locals>.wrapper.<locals>.<lambda>)_safe_resolved_promiser   r   r   r   r   r*   r     s   zPromise.safe.<locals>.wrapper)	functoolsr   r   r?   rS   )r   r   r   r   r)   r   r*   safe  s   zPromise.safec                 C   s   t || djS )N)promise_class)r   ri   )r   r   r)   r)   r*   all  s   zPromise.allc                    s:   t  s|   S  fdd}|  |S )a  
        A special function that takes a dictionary of promises
        and turns them into a promise for a dictionary of values.
        In other words, this turns an dictionary of promises for values
        into a promise for a dictionary of values.
        c                    s    t  | S r&   )zipkeys)resolved_values	dict_typemr)   r*   handle_success  r   z(Promise.for_dict.<locals>.handle_success)ro   rS   r   valuesr   )r   r   r   r)   r   r*   for_dict  s
   	zPromise.for_dictc                 C   s4   |j }|du s|tv rdS t|tpt|pt|S )zs
        A utility function to determine if the specified
        object is a promise using "duck typing".
        NF)r   
BASE_TYPESr   r?   r   r   )r   r   r   r)   r)   r*   rX   $  s   
zPromise.is_thenable)NN)Fr&   )FN)U__name__
__module____qualname____doc__r]   r\   ry   	_is_boundrb   rt   r^   r   rn   rm   r   rF   rh   _is_waitingr@   rD   propertyr,   rJ   rL   	__await__r   rQ   rV   rl   rr   rY   r[   r{   rW   r   r   r   r   r   r   r   r   r   r_   ra   r   rK   r   rc   ru   rA   classmethodr   r   r   r   re   rg   rU   r|   r   r   r   r   r   rG   r   rT   rP   r   r   r   r   rZ   rM   rejectedrS   cast	fulfilledr   r   r   r   r   rX   r)   r)   r)   r*   r?   a   s    



	





	
,







 










r?   c                 C   s$   | t vrtt| dd t | < t |  S )Nr   )_type_done_callbacksr   getattr)r   r)   r)   r*   r   9  s
   
r   c                    s    fdd}|S )Nc              
      sN   z	|    W d S  ty& } zt d } || W Y d }~d S d }~ww r4   )resultr5   r   )rJ   r9   r:   rM   rS   r)   r*   handle_future_resultI  s   
z4_process_future_result.<locals>.handle_future_resultr)   )rS   rM   r   r)   r   r*   r   G  s   r   )Rcollectionsr   r   r   r   sysr   r   	threadingr   typesr   weakrefr	   sixr
   async_r   compatr   r   r   r   utilsr   r   r   r   r   r   promise_listr   schedulers.immediater   typingr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r(   rv   r+   r-   
IS_PYTHON2r   r   r   r   r   r   setboolfloatcomplexr   listr   r   r]   rf   rd   r3   r;   r<   r=   r?   r   r   r   r   promise_for_dictrX   r   r)   r)   r)   r*   <module>   sf     	     Z	