o
    ›¨Êhñ.  ã                   @   sˆ  d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZmZmZmZmZmZmZ ddlmZ dd	lmZ eeƒZd
ZdZG dd„ dƒZG dd„ deƒZdd„ Zdd„ Z dd„ Z!dd„ Z"d@dd„Z#dd„ Z$d@dd„Z%dd„ Z&d d!„ Z'd"d#„ Z(d$d%„ Z)G d&d'„ d'ee*ƒZ+dAd)d*„Z,dBd,ed-ef d.e-d/e.fd0d1„Z/d2d3„ Z0d@d4d5„Z1d6d7„ Z2d8d9„ Z3d:d;„ Z4d<d=„ Z5d>d?„ Z6dS )CzFunctional-style utilities.é    N)ÚUserList)Úpartial)ÚisliceÚteeÚzip_longest)ÚAnyÚCallable)ÚLRUCacheÚ
dictfilterÚis_listÚlazyÚmaybe_evaluateÚ
maybe_listÚmemoize)Úpromise)Ú
get_logger)r	   r   r   r   ÚmlazyÚnoopÚfirstÚfirstmethodÚchunksÚpadlistÚmattrgetterÚuniqÚregenr
   r   r   Úhead_from_funÚmaybeÚfun_accepts_kwargsz4
def {fun_name}({fun_args}):
    return {fun_value}
c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚDummyContextc                 C   ó   | S ©N© ©Úselfr!   r!   úI/var/www/html/env/lib/python3.10/site-packages/celery/utils/functional.pyÚ	__enter__   ó   zDummyContext.__enter__c                 G   s   d S r    r!   )r#   Úexc_infor!   r!   r$   Ú__exit__!   r&   zDummyContext.__exit__N)Ú__name__Ú
__module__Ú__qualname__r%   r(   r!   r!   r!   r$   r      s    r   c                       s(   e Zd ZdZdZdZ‡ fdd„Z‡  ZS )r   z€Memoized lazy evaluation.

    The function is only evaluated once, every subsequent access
    will return the same value.
    FNc                    s   | j stƒ  ¡ | _d| _ | jS ©NT)Ú	evaluatedÚsuperÚevaluateÚ_valuer"   ©Ú	__class__r!   r$   r/   0   s   zmlazy.evaluate)r)   r*   r+   Ú__doc__r-   r0   r/   Ú__classcell__r!   r!   r1   r$   r   %   s
    r   c                  O   s   dS )zONo operation.

    Takes any arguments/keyword arguments and does nothing.
    Nr!   )ÚargsÚkwargsr!   r!   r$   r   7   s    r   c                 O   r   )z%Return the first positional argument.r!   )Úargr5   r6   r!   r!   r$   Úpass1>   s   r8   c                 c   s&   � | D ]}t |tƒr|ƒ }|V  qd S r    )Ú
isinstancer   )ÚitÚvaluer!   r!   r$   Úevaluate_promisesC   s   €
ýr<   c                    s   t ‡ fdd„t|ƒD ƒdƒS )z�Return the first element in ``it`` that ``predicate`` accepts.

    If ``predicate`` is None it will return the first item that's not
    :const:`None`.
    c                 3   s.   � | ]}ˆ d urˆ |ƒrn|d ur|V  qd S r    r!   )Ú.0Úv©Ú	predicater!   r$   Ú	<genexpr>Q   s   € ÿÿzfirst.<locals>.<genexpr>N)Únextr<   )r@   r:   r!   r?   r$   r   J   s   ýr   c                    s   ‡ ‡fdd„}|S )zîMultiple dispatch.

    Return a function that with a list of instances,
    finds the first instance that gives a value for the given method.

    The list can also contain lazy instances
    (:class:`~kombu.utils.functional.lazy`.)
    c              	      sl   | D ]1}zt t|ƒˆ ƒ}ˆrˆ|g|¢R i |¤Žn||i |¤Ž}W n	 ty*   Y qw |d ur3|  S qd S r    )Úgetattrr   ÚAttributeError)r:   r5   r6   ÚobjÚmethÚreply©ÚmethodÚon_callr!   r$   Ú_matchera   s   ÿÿÿøzfirstmethod.<locals>._matcherr!   )rI   rJ   rK   r!   rH   r$   r   W   s   
r   c                 c   s*   � | D ]}|gt t| |d ƒƒ V  qdS )as  Split an iterator into chunks with `n` elements each.

    Warning:
        ``it`` must be an actual iterator, if you pass this a
        concrete sequence will get you repeating elements.

        So ``chunks(iter(range(1000)), 10)`` is fine, but
        ``chunks(range(1000), 10)`` is not.

    Example:
        # n == 2
        >>> x = chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 2)
        >>> list(x)
        [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10]]

        # n == 3
        >>> x = chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 3)
        >>> list(x)
        [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]]
    é   N)Úlistr   )r:   ÚnÚitemr!   r!   r$   r   p   s   €ÿr   c                 C   s"   t | ƒd|… |g|t| ƒ   S )a»  Pad list with default elements.

    Example:
        >>> first, last, city = padlist(['George', 'Costanza', 'NYC'], 3)
        ('George', 'Costanza', 'NYC')
        >>> first, last, city = padlist(['George', 'Costanza'], 3)
        ('George', 'Costanza', None)
        >>> first, last, city, planet = padlist(
        ...     ['George', 'Costanza', 'NYC'], 4, default='Earth',
        ... )
        ('George', 'Costanza', 'NYC', 'Earth')
    N)rM   Úlen)Ú	containerÚsizeÚdefaultr!   r!   r$   r   ‰   s   "r   c                     s   ‡ fdd„S )z²Get attributes, ignoring attribute errors.

    Like :func:`operator.itemgetter` but return :const:`None` on missing
    attributes instead of raising :exc:`AttributeError`.
    c                    s   ‡ fdd„ˆD ƒS )Nc                    s   i | ]	}|t ˆ |d ƒ“qS r    )rC   )r=   Úattr©rE   r!   r$   Ú
<dictcomp>Ÿ   s    z1mattrgetter.<locals>.<lambda>.<locals>.<dictcomp>r!   rU   ©ÚattrsrU   r$   Ú<lambda>Ÿ   s    zmattrgetter.<locals>.<lambda>r!   rW   r!   rW   r$   r   ™   s   r   c                    s   t ƒ ‰ ‡ fdd„| D ƒS )z7Return all unique elements in ``it``, preserving order.c                 3   s&   � | ]}|ˆ vrˆ   |¡p|V  qd S r    )Úadd)r=   rE   ©Úseenr!   r$   rA   ¥   s   €$ zuniq.<locals>.<genexpr>)Úset©r:   r!   r[   r$   r   ¢   s   r   c                 C   s    t | ƒ\}}t|dƒ t||ƒS )zßYield pairs of (current, next) items in `it`.

    `next` is None if `current` is the last item.
    Example:
        >>> list(lookahead(x for x in range(6)))
        [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, None)]
    N)r   rB   r   )r:   ÚaÚbr!   r!   r$   Ú	lookahead¨   s   

ra   c                 C   s   t | ttfƒr	| S t| ƒS )zþConvert iterator to an object that can be consumed multiple times.

    ``Regen`` takes any iterable, and if the object is an
    generator it will cache the evaluated list on first access,
    so that the generator can be "consumed" multiple times.
    )r9   rM   ÚtupleÚ_regenr^   r!   r!   r$   r   µ   s   r   c                   @   sb   e Zd 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
edd„ ƒZdd„ Zd	S )rc   c                 C   s   || _ g | _d| _d S )NF)Ú
_regen__itÚ_regen__consumedÚ_regen__done)r#   r:   r!   r!   r$   Ú__init__Ä   s   
z_regen.__init__c                 C   s   t | jffS r    )rM   Údatar"   r!   r!   r$   Ú
__reduce__Ì   s   z_regen.__reduce__c                    s(   ‡ fdd„| j D ƒ| _ tˆ | jƒ| _d S )Nc                    s   g | ]}ˆ |ƒ‘qS r!   r!   )r=   Úel©Úfuncr!   r$   Ú
<listcomp>Ð   s    z_regen.map.<locals>.<listcomp>)re   Úmaprd   )r#   rl   r!   rk   r$   rn   Ï   s   z
_regen.mapc                 C   s
   | j  ¡ S r    )rd   Ú__length_hint__r"   r!   r!   r$   ro   Ó   s   
z_regen.__length_hint__Nc                 c   sÜ   � | j sh|d u s|dkrjt| jƒ}zt|ƒ}W n
 ty!   Y d S w | j |¡ | j slz#zt|ƒ}| j |¡ W n tyI   d| _ Y W |V  d S w W |V  n|V  w |}|d urc|d8 }|dkrcd S | j r+d S d S d S d S )Nr   TrL   )rf   Úiterrd   rB   ÚStopIterationre   Úappend)r#   Úlimitr:   ÚnowÚnext_r!   r!   r$   Ú__lookahead_consumeÖ   s8   €
ÿ
üÿ
ò÷	z_regen.__lookahead_consumec                 c   s    � | j E d H  |  ¡ E d H  d S r    )re   Ú_regen__lookahead_consumer"   r!   r!   r$   Ú__iter__ð   s   €z_regen.__iter__c                 C   s@   |dk r	| j | S |t| jƒ d }| j|d�D ]}q| j| S )Nr   rL   )rs   )rh   rP   re   rw   )r#   ÚindexÚconsume_countÚ_r!   r!   r$   Ú__getitem__ô   s   

z_regen.__getitem__c                 C   s6   t | jƒrdS z	tt| ƒƒ W dS  ty   Y dS w )NTF)rP   re   rB   rp   rq   r"   r!   r!   r$   Ú__bool__þ   s   
ýÿz_regen.__bool__c                 C   s    | j s| j | j¡ d| _ | jS r,   )rf   re   Úextendrd   r"   r!   r!   r$   rh   	  s   z_regen.datac                 C   s0   d  | jjd dd„ | jD ƒ¡| jsd¡S d¡S )Nz<{}: [{}{}]>ú, c                 s   s   � | ]}t |ƒV  qd S r    )Úrepr)r=   Úer!   r!   r$   rA     s   € z"_regen.__repr__.<locals>.<genexpr>z...Ú )Úformatr2   r)   Újoinre   rf   r"   r!   r!   r$   Ú__repr__  s   ýýz_regen.__repr__r    )r)   r*   r+   rg   ri   rn   ro   rw   rx   r|   r}   Úpropertyrh   r…   r!   r!   r!   r$   rc   Á   s    


rc   Tc           
      C   sL  | j r,t| j ƒ}|rttt| j ƒƒƒn| j }| jd | … }tt| j| d … |ƒƒ}n| jg }}| j}| j}| jr^t	| j
ƒt	| j ¡ ƒ }|rVdd„ t| j ¡ ƒD ƒ}	nt| j ¡ ƒ}	n| j
g }}	d td d |¡d dd„ |D ƒ¡|r|d|› �nd |s�|	r…|s…dnd |r�d |¡nd d dd„ |	D ƒ¡|r¡d|› �gƒ¡S d gƒ¡S )	Nc                 S   s   g | ]\}}||f‘qS r!   r!   )r=   ÚiÚkwr!   r!   r$   rm   '  s    ÿz!_argsfromspec.<locals>.<listcomp>r   c                 s   s"   � | ]\}}|› d |› �V  qdS )ú=Nr!   ©r=   Úkr>   r!   r!   r$   rA   1  s   €  z _argsfromspec.<locals>.<genexpr>Ú*c                 s   s$   � | ]\}}|› d |› d�V  qdS )z="ú"Nr!   rŠ   r!   r!   r$   rA   5  s   €" z**)ÚdefaultsrP   rM   Úranger5   ÚzipÚvarargsÚvarkwÚkwonlydefaultsr]   Ú
kwonlyargsÚkeysÚ	enumerateÚitemsr„   Úfilter)
ÚspecÚreplace_defaultsÚsplitrŽ   Ú
positionalÚoptionalr‘   r’   r”   Úkwonlyargs_optionalr!   r!   r$   Ú_argsfromspec  s:   
ÿÿùùrŸ   FÚfun.ÚboundÚreturnc           
      C   sª   t  | ¡}t| ƒ}| jjdk}t  | ¡}|s%|r%|s%|s%| jj| j}} n| j}tj|t	t  
| ¡ƒdd�}t |¡ d| ji}t||ƒ || }	||	_|rSt|	tƒ ƒS |	S )z1Generate signature function from actual function.Úcython_function_or_methodrL   )Úfun_nameÚfun_argsÚ	fun_valuer)   )ÚinspectÚ
isfunctionÚcallabler2   r)   ÚismethodÚ__call__ÚFUNHEAD_TEMPLATErƒ   rŸ   ÚgetfullargspecÚloggerÚdebugr*   ÚexecÚ_sourcer   Úobject)
r    r¡   Úis_functionÚis_callableÚ	is_cythonÚ	is_methodÚnameÚ
definitionÚ	namespaceÚresultr!   r!   r$   r   :  s(   

ý


r   c                 C   s   t  | ¡}|jpt|jƒ|kS r    )r§   r­   r‘   rP   r5   )r    rN   Úargspecr!   r!   r$   Úarity_greater[  s   
r¼   c                 C   s2   t  |¡}|jp|jp|rt|jƒ|kS | |jv S r    )r§   r­   r’   r‘   rP   r5   )r·   r    Úpositionr™   r!   r!   r$   Úfun_takes_argument`  s   
þþr¾   c                 C   s   t dd„ t | ¡j ¡ D ƒƒS )z<Return true if function accepts arbitrary keyword arguments.c                 s   s    � | ]}|j |jkr|V  qd S r    )ÚkindÚVAR_KEYWORD)r=   Úpr!   r!   r$   rA   j  s   € ÿþz%fun_accepts_kwargs.<locals>.<genexpr>)Úanyr§   Ú	signatureÚ
parametersÚvalues)r    r!   r!   r$   r   h  s   ÿr   c                 C   s   |dur| |ƒS |S )z$Call typ on value if val is defined.Nr!   )ÚtypÚvalr!   r!   r$   r   p  s   r   c                 C   s   t | tƒr
| |f S | |g S )zÆReturn copy of sequence seq with item added.

    Returns:
        Sequence: if seq is a tuple, the result will be a tuple,
           otherwise it depends on the implementation of ``__add__``.
    )r9   rb   )ÚseqrO   r!   r!   r$   Úseq_concat_itemu  s   rÉ   c                 C   s@   t t| |gtd�ƒ}t| |ƒs|| ƒ} t||ƒs||ƒ}| | S )a  Concatenate two sequences: ``a + b``.

    Returns:
        Sequence: The return value will depend on the largest sequence
            - if b is larger and is a tuple, the return value will be a tuple.
            - if a is larger and is a list, the return value will be a list,
    )Úkey)ÚtypeÚmaxrP   r9   )r_   r`   Úpreferr!   r!   r$   Úseq_concat_seq  s   	

rÎ   c                 C   s   t | ttfƒot | tƒ S r    )r9   ÚintÚfloatÚbool)r;   r!   r!   r$   Úis_numeric_value‘  s   rÒ   r    )T)F)7r3   r§   Úcollectionsr   Ú	functoolsr   Ú	itertoolsr   r   r   Útypingr   r   Úkombu.utils.functionalr	   r
   r   r   r   r   r   Úviner   Úcelery.utils.logr   r)   r®   Ú__all__r¬   r   r   r   r8   r<   r   r   r   r   r   r   ra   r   rM   rc   rŸ   rÑ   Ústrr   r¼   r¾   r   r   rÉ   rÎ   rÒ   r!   r!   r!   r$   Ú<module>   sF    $	

	
W "!

