o
    ?Zcl)                     @   sD  d 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d1ddZdd Z d2dd Z!d3d"d#Z"		$	d4d&d'Z#d5d*d+Z$d6d-d.Z%d/d0 Z&eZ'eZ(dS )7zFunctional Utilities.    N)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   L/var/www/chikooza/env/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   a?  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   4   s   
zLRUCache.__init__c                 C   sB   | j  | j| }| |< |W  d    S 1 sw   Y  d S r   )r.   r/   popr   keyr    r   r   r   __getitem__9   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.   r/   r+   updatelenrangepopitem)r   argskwargsr/   r+   _r   r   r   r5   >   s   "zLRUCache.updateTc                 C   s6   | j  | j|W  d    S 1 sw   Y  d S r   )r.   r/   r8   )r   r4   r   r   r   r8   G   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+   r6   r/   r0   nextiterr1   r   r   r   __setitem__K   s
   "zLRUCache.__setitem__c                 C   s
   t | jS r   )r=   r/   r&   r   r   r   __iter__R   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.   r/   KeyErrorr   kr   r   r   _iterate_itemsU   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   r@   rB   r   r   r   _iterate_values^   s   "zLRUCache._iterate_valuesc                 C   s4   | j  | j W  d    S 1 sw   Y  d S r   )r.   r/   keysr&   r   r   r   _iterate_keysh   s   $zLRUCache._iterate_keysr   c                 C   sN   | j  t| j|| }t|| |< |W  d    S 1 s w   Y  d S r   )r.   intr/   r0   str)r   r2   deltanewvalr   r   r   incrn   s
   $zLRUCache.incrc                 C   s   t t| }|d |S )Nr.   )dictvarsr0   )r   dr   r   r   __getstate__v   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   r3   r5   r8   r>   r?   rD   	iteritemsrE   
itervaluesrG   iterkeysrL   rP   rS   rF   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sortedrY   rA   misseshits)r9   r:   r2   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_   r`   r   r   rc      s   z(memoize.<locals>._memoize.<locals>.clearr   )r,   Lockr
   r^   r]   rc   original_func)ra   rc   Cacherb   maxsize)r_   r`   ra   r.   r   _memoize   s   
zmemoize.<locals>._memoizer   )rh   rb   rg   ri   r   rf   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   ra   r9   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   rj   r&   r   r   r   rn      s   zlazy.evaluatec                 C   
   t |  S r   )rI   r&   r   r   r   __str__   r   zlazy.__str__c                 C   ro   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   rq   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)rl   rm   )	__class__rk   rl   rm   r&   r   r   r   
__reduce__   s   zlazy.__reduce__N)r(   r)   r*   rT   r   r!   rn   rp   r'   rs   rt   rv   rx   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   rn   )r    r   r   r   r      s   
r   c                 C   s   t | |ot | |p
d S )znReturn true if the object is iterable.

    Note:
        Returns false if object is a mapping or string.
    r   )ry   )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   )rz   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   .0rC   vr   r   r   
<dictcomp>   s    zdictfilter.<locals>.<dictcomp>)rM   rY   )rO   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<   r7   rH   r   abs)ra   catchr9   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>L  s   " zreprkwargs.<locals>.<genexpr>)joinrY   )r:   sepr   r   r   r   
reprkwargsK  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   )namer9   r:   r   r   r   r   reprcallO  s   r   c                 C   s   t | }||jv p||jv S r   )inspectgetfullargspecr9   
kwonlyargs)funcargument_nameargument_specr   r   r   accepts_argumentX  s   

r   r   )r   Nr   F)r   Nr   r   )	NNNNr   r   r   NN)r   r   )r   Nr   ))rT   r   r   r,   collectionsr   r   collections.abcr   r   	itertoolsr   r   r	   r   
vine.utilsr
   encodingr   r   __all__objectrZ   r   r   r   r   r   rI   r   r   r   r   r   r   r   r   r   r   promisemaybe_promiser   r   r   r   <module>   s@    Z%*	




<
		