o
    Df)                     @  sP  d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
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Ze ZG dd dZG dd deZddefddZG dd dZdd Zeefe
ffddZeeffddZ d2ddZ!dd Z"d3d d!Z#d4d#d$Z$		%	d5d'd(Z%d6d+d,Z&d7d.d/Z'd0d1 Z(eZ)eZ*dS )8zFunctional Utilities.    )annotationsN)OrderedDictUserDict)IterableMapping)countrepeat)sleeptime)wraps   )	safe_repr)LRUCachememoizelazymaybe_evaluateis_list
maybe_list
dictfilterretry_over_timec                   @  s$   e Zd Zdd Zdd Zdd ZdS )ChannelPromisec                 C  s
   || _ d S N)__contract__)selfcontract r   O/home/ubuntu/webapp/venv/lib/python3.10/site-packages/kombu/utils/functional.py__init__      
zChannelPromise.__init__c                 C  s.   z| j W S  ty   |   }| _ | Y S w r   )	__value__AttributeErrorr   )r   valuer   r   r   __call__   s   zChannelPromise.__call__c                 C  s4   zt | jW S  ty   dt| jdd Y S w )Nz<promise: 0xx>)reprr   r    idr   r   r   r   r   __repr__%   s
   zChannelPromise.__repr__N)__name__
__module____qualname__r   r"   r(   r   r   r   r   r      s    r   c                   @  s   e Zd ZdZdddZdd Zdd Zdd
dZdd Zdd Z	dd Z
e
Zdd ZeZdd ZeZdddZdd Zdd ZeZeZe
ZdS ) r   aM  LRU Cache implementation using a doubly linked list to track access.

    Arguments:
    ---------
        limit (int): The maximum number of keys to keep in the cache.
            When a new key is inserted and the limit has been exceeded,
            the *Least Recently Used* key will be discarded from the
            cache.
    Nc                 C  s   || _ t | _t | _d S r   )limit	threadingRLockmutexr   data)r   r,   r   r   r   r   7   s   
zLRUCache.__init__c                 C  sB   | j  | j| }| |< |W  d    S 1 sw   Y  d S r   )r/   r0   popr   keyr!   r   r   r   __getitem__<   s   $zLRUCache.__getitem__c                 O  s   | j A | j| j}}|j|i | |r,t||kr4tt|| D ]}|jdd q#W d    d S W d    d S W d    d S 1 sGw   Y  d S )NF)last)r/   r0   r,   updatelenrangepopitem)r   argskwargsr0   r,   _r   r   r   r6   A   s   "zLRUCache.updateTc                 C  s6   | j  | j|W  d    S 1 sw   Y  d S r   )r/   r0   r9   )r   r5   r   r   r   r9   J   s   
$zLRUCache.popitemc                 C  sb   | j $ | jrt| j| jkr| jtt| j || j|< W d    d S 1 s*w   Y  d S r   )r/   r,   r7   r0   r1   nextiterr2   r   r   r   __setitem__N   s
   "zLRUCache.__setitem__c                 C  s
   t | jS r   )r>   r0   r'   r   r   r   __iter__U   r   zLRUCache.__iter__c              
   c  s^    | j ! | D ]}z
|| j| fV  W q ty   Y qw W d    d S 1 s(w   Y  d S r   r/   r0   KeyErrorr   kr   r   r   _iterate_itemsX   s   "zLRUCache._iterate_itemsc              
   c  sZ    | j  | D ]}z| j| V  W q ty   Y qw W d    d S 1 s&w   Y  d S r   rA   rC   r   r   r   _iterate_valuesa   s   "zLRUCache._iterate_valuesc                 C  s4   | j  | j W  d    S 1 sw   Y  d S r   )r/   r0   keysr'   r   r   r   _iterate_keysk   s   $zLRUCache._iterate_keysr   c                 C  sN   | j  t| j|| }t|| |< |W  d    S 1 s w   Y  d S r   )r/   intr0   r1   str)r   r3   deltanewvalr   r   r   incrq   s
   $zLRUCache.incrc                 C  s   t t| }|d |S )Nr/   )dictvarsr1   )r   dr   r   r   __getstate__y   s   
zLRUCache.__getstate__c                 C  s   || _ t | _d S r   )__dict__r-   r.   r/   )r   stater   r   r   __setstate__~   s   zLRUCache.__setstate__r   )T)r   )r)   r*   r+   __doc__r   r4   r6   r9   r?   r@   rE   	iteritemsrF   
itervaluesrH   iterkeysrM   rQ   rT   rG   valuesitemsr   r   r   r   r   ,   s(    


	
r   c                   s    fdd}|S )z)Decorator to cache function return value.c                   sX   t  dt fdd  fdd}d  _ _| _ _ S )N)r,   c                    s   r| |}n| t f tt|  }z | }W d    n1 s'w   Y  W n0 ty]   | i |}  jd7  _ ||< W d    Y |S 1 sUw   Y  Y |S w   jd7  _|S )Nr   )KEYWORD_MARKtuplesortedrZ   rB   misseshits)r:   r;   r3   r!   )_Mcachefunkeyfunr/   r   r   r`      s(   

z%memoize.<locals>._memoize.<locals>._Mc                     s      d  _ _dS )z+Clear the cache and reset cache statistics.r   N)clearr_   r^   r   )r`   ra   r   r   rd      s   z(memoize.<locals>._memoize.<locals>.clearr   )r-   Lockr   r_   r^   rd   original_func)rb   rd   Cacherc   maxsize)r`   ra   rb   r/   r   _memoize   s   
zmemoize.<locals>._memoizer   )ri   rc   rh   rj   r   rg   r   r      s    r   c                   @  sX   e Zd Z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 )r   a
  Holds lazy evaluation.

    Evaluated when called or if the :meth:`evaluate` method is called.
    The function is re-evaluated on every call.

    Overloaded operations that will evaluate the promise:
        :meth:`__str__`, :meth:`__repr__`, :meth:`__cmp__`.
    c                 O  s   || _ || _|| _d S r   _fun_args_kwargs)r   rb   r:   r;   r   r   r   r      s   
zlazy.__init__c                 C  s   |   S r   )evaluater'   r   r   r   r"      s   zlazy.__call__c                 C  s   | j | ji | jS r   rk   r'   r   r   r   ro      s   zlazy.evaluatec                 C  
   t |  S r   )rJ   r'   r   r   r   __str__   r   zlazy.__str__c                 C  rp   r   )r%   r'   r   r   r   r(      r   zlazy.__repr__c                 C  s
   |  |kS r   r   r   rhsr   r   r   __eq__   r   zlazy.__eq__c                 C  s
   |  |kS r   r   rr   r   r   r   __ne__   r   zlazy.__ne__c                 C  s   | |t | < | S r   )r&   )r   memor   r   r   __deepcopy__   s   zlazy.__deepcopy__c                 C  s   | j | jf| j| jdfS )N)rm   rn   )	__class__rl   rm   rn   r'   r   r   r   
__reduce__   s   zlazy.__reduce__N)r)   r*   r+   rU   r   r"   ro   rq   r(   rt   ru   rw   ry   r   r   r   r   r      s    	r   c                 C  s   t | tr	|  S | S )z9Evaluate value only if value is a :class:`lazy` instance.)
isinstancer   ro   )r!   r   r   r   r      s   
r   c                 C  s   t | |ot | |p
d S )zwReturn true if the object is iterable.

    Note:
    ----
        Returns false if object is a mapping or string.
    r   )rz   )objscalarsitersr   r   r   r      s   r   c                 C  s   | du s	t | |r| S | gS )z0Return list of one element if ``l`` is a scalar.N)r   )r{   r|   r   r   r   r      s   r   c                 K  s6   | du r|n|rt | fi |n| } dd |  D S )z=Remove all keys from dict ``d`` whose value is :const:`None`.Nc                 S  s   i | ]\}}|d ur||qS r   r   .0rD   vr   r   r   
<dictcomp>   s    zdictfilter.<locals>.<dictcomp>)rN   rZ   )rP   kwr   r   r   r      s   $r   c                 c  s4    t | } tj}td D ]}||  | d V  qd S )Nr   )listrandomshuffler   )itr   r<   r   r   r   shufflecycle   s   r         ?Fc                 c  s<    | d }	 |r||kr|V  ||7 }n	|sd S || V  q)Nr   r   )startstopstep
repeatlastcurr   r   r   fxrange   s   

r         Y@c                 c  sJ    d| d }}	 ||krd S |V  |rt || |}n||7 }||7 }q	)Nr   r   )min)r   r   r   maxsum_r   r   r   r   
fxrangemax  s   r         c                 C  s
  |si n|}|s
g n|}t ||| |dd}|
rt |
 nd}t D ]`}z
| |i |W   S  |y } zH|dur?||kr? |rGt |krG |	rL|	  t|rU||||nt|}|rxtt|D ]}|	ri|	  td qbttt||  W Y d}~q"d}~ww dS )a  Retry the function over and over until max retries is exceeded.

    For each retry we sleep a for a while before we try again, this interval
    is increased for every retry until the max seconds is reached.

    Arguments:
    ---------
        fun (Callable): The function to try
        catch (Tuple[BaseException]): Exceptions to catch, can be either
            tuple or a single exception class.

    Keyword Arguments:
    -----------------
        args (Tuple): Positional arguments passed on to the function.
        kwargs (Dict): Keyword arguments passed on to the function.
        errback (Callable): Callback for when an exception in ``catch``
            is raised.  The callback must take three arguments:
            ``exc``, ``interval_range`` and ``retries``, where ``exc``
            is the exception instance, ``interval_range`` is an iterator
            which return the time in seconds to sleep next, and ``retries``
            is the number of previous retries.
        max_retries (int): Maximum number of retries before we give up.
            If neither of this and timeout is set, we will retry forever.
            If one of this and timeout is reached, stop.
        interval_start (float): How long (in seconds) we start sleeping
            between retries.
        interval_step (float): By how much the interval is increased for
            each retry.
        interval_max (float): Maximum number of seconds to sleep
            between retries.
        timeout (int): Maximum seconds waiting before we give up.
    T)r   Nr   )	r   r
   r   floatr=   r8   rI   r	   abs)rb   catchr:   r;   errbackmax_retriesinterval_startinterval_stepinterval_maxcallbacktimeoutinterval_rangeendretriesexcttsr<   r   r   r   r     s:   #

r   , {0}={1}c                   s   |  fdd|  D S )Nc                 3  s$    | ]\}}  |t|V  qd S r   )format
_safe_reprr~   fmtr   r   	<genexpr>R  s   " zreprkwargs.<locals>.<genexpr>)joinrZ   )r;   sepr   r   r   r   
reprkwargsQ  s   r   r   c                 C  s>   |si n|}d | |tt|pd|r|r|pdt||S )Nz
{}({}{}{})r    )r   r   mapr   r   )namer:   r;   r   r   r   r   reprcallU  s   r   c                 C  s   t | }||jv p||jv S r   )inspectgetfullargspecr:   
kwonlyargs)funcargument_nameargument_specr   r   r   accepts_argument^  s   

r   r   )r   Nr   F)r   Nr   r   )	NNNNr   r   r   NN)r   r   )r   Nr   )+rU   
__future__r   r   r   r-   collectionsr   r   collections.abcr   r   	itertoolsr   r   r
   r	   
vine.utilsr   encodingr   r   __all__objectr[   r   r   r   r   r   rJ   r   r   r   r   r   r   r   r   r   r   promisemaybe_promiser   r   r   r   <module>   sB    [%*





>
		