o
    ›¨Êhõ/  ã                   @   s&  d 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 dd	lmZmZ dd
lmZ zddlmZ W n eyI   dZY nw efZdZdZdZdZeeƒZdd„ Zdd„ ZG dd„ deƒZG dd„ dƒZ G dd„ de!ƒZ"G dd„ de"d�Z#G dd„ de#ƒZ$G dd „ d e$ƒZ%d!S )"z0A directed acyclic graph of reusable components.é    )Údeque)ÚEvent)Úignore_errors)Úbytes_to_str©Úsymbol_by_nameé   )ÚDependencyGraphÚGraphFormatter)ÚinstantiateÚqualname)Ú
get_logger)ÚGreenletExit© )Ú	BlueprintÚStepÚStartStopStepÚConsumerStepé   é   c                 C   s   d| j › d|› �S )Nz| z: ©Úalias)ÚnsÚfmtr   r   úB/var/www/html/env/lib/python3.10/site-packages/celery/bootsteps.pyÚ_pre   ó   r   c                 C   s   | j  dd¡d S )NÚ.r   éÿÿÿÿ)ÚnameÚrsplit)Úsr   r   r   Ú_label#   r   r"   c                   @   sD   e Zd ZdZdZdZddddœZdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚStepFormatterz'Graph formatter for :class:`Blueprint`.u   â§‰u   âˆ˜ÚparallelogramÚ
slategray4Ú
slategray3)ÚshapeÚcolorÚ	fillcolorc                 C   s,   |od  |  |¡t|jpt|ƒ dd¡ƒ¡S )Nz{}{}zutf-8Úignore)ÚformatÚ_get_prefixr   Úlabelr"   Úencode©ÚselfÚstepr   r   r   r-   2   s   ÿþzStepFormatter.labelc                 C   s   |j r| jS |jr| jS dS )NÚ )ÚlastÚblueprint_prefixÚconditionalÚconditional_prefixr/   r   r   r   r,   9   s
   zStepFormatter._get_prefixc                 K   s    |j r| jn| j}|  |||¡S ©N)r3   Úblueprint_schemeÚnode_schemeÚ	draw_node)r0   ÚobjÚattrsÚschemer   r   r   Únode@   s   zStepFormatter.nodec                 K   s&   |j r
|jddd� |  ||| j|¡S )NÚnoneÚdarkseagreen3)Ú	arrowheadr(   )r3   ÚupdateÚ	draw_edgeÚedge_scheme)r0   ÚaÚbr<   r   r   r   ÚedgeD   s   zStepFormatter.edgeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r4   r6   r8   r-   r,   r>   rG   r   r   r   r   r#   '   s    ýr#   c                   @   sê   e Zd ZdZeZdZdZdZe	ƒ Z
ddededediZ		d3dd	„Zd
d„ Zdd„ Zdd„ Zdd„ Z		d4dd„Z	d5dd„Zd6dd„Zd7d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/d0„ Z e!d1d2„ ƒZ"dS )8r   aÁ  Blueprint containing bootsteps that can be applied to objects.

    Arguments:
        steps Sequence[Union[str, Step]]: List of steps.
        name (str): Set explicit name for this blueprint.
        on_start (Callable): Optional callback applied after blueprint start.
        on_close (Callable): Optional callback applied before blueprint close.
        on_stopped (Callable): Optional callback applied after
            blueprint stopped.
    Nr   ÚinitializingÚrunningÚclosingÚterminatingc                 C   sT   |p
| j p
tt| ƒƒ| _ t|pg ƒt| jƒB | _|| _|| _|| _t	ƒ | _
i | _d S r7   )r   r   ÚtypeÚsetÚdefault_stepsÚtypesÚon_startÚon_closeÚ
on_stoppedr   Úshutdown_completeÚsteps)r0   rX   r   rT   rU   rV   r   r   r   Ú__init__c   s   
zBlueprint.__init__c                 C   sb   t | _| jr
|  ¡  tdd„ |jD ƒƒD ]\}}|  d|j¡ |d | _| |¡ t	 
d¡ qd S )Nc                 s   s   � | ]	}|d ur|V  qd S r7   r   ©Ú.0r!   r   r   r   Ú	<genexpr>q   s   € z"Blueprint.start.<locals>.<genexpr>zStarting %sr   z^-- substep ok)ÚRUNÚstaterT   Ú	enumeraterX   Ú_debugr   ÚstartedÚstartÚloggerÚdebug)r0   ÚparentÚir1   r   r   r   rb   m   s   

üzBlueprint.startc                 C   s   | j | jpd S ©Nr   )Ústate_to_namer^   ©r0   r   r   r   Úhuman_statew   ó   zBlueprint.human_statec                 C   s(   i }|j D ]}| | |¡pi ¡ q|S r7   )rX   rB   Úinfo)r0   re   rl   r1   r   r   r   rl   z   s   
zBlueprint.infoc                 C   s$   | j r|   ¡  | j|dddd� d S )NÚcloserN   F)Úreverse)rU   Úsend_all©r0   re   r   r   r   rm   €   s   zBlueprint.closeÚstopÚ
restartingFc                 C   s   | j ||||d� d S )N)Ú	propagate)ro   )r0   re   ÚmethodÚdescriptionrs   r   r   r   Úrestart…   s   zBlueprint.restartTr   c                 C   s°   |p|  dd¡}|rt|jƒn|j}|D ]A}|rUt||d ƒ}	|	d urU|  d| ¡ |j¡ z
|	|g|¢R Ž  W q tyT }
 z|rA‚ t 	d||j|
¡ W Y d }
~
qd }
~
ww qd S )NÚ_ú z%s %s...zError on %s %s: %r)
ÚreplaceÚreversedrX   Úgetattrr`   Ú
capitalizer   Ú	Exceptionrc   Ú	exception)r0   re   rt   ru   rn   rs   ÚargsrX   r1   ÚfunÚexcr   r   r   ro   ‰   s*   
ÿ
ÿ€ý€øzBlueprint.send_allc                 C   s˜   |rdnd}| j ttfv rd S | j tks| jt|jƒkr&t| _ | j ¡  d S |  	|¡ t| _ | j
||r5dnd|dd� | jrB|  ¡  t| _ | j ¡  d S )NrO   ÚstoppingÚ	terminaterq   F)ru   rs   )r^   ÚCLOSEÚ	TERMINATEr]   ra   ÚlenrX   rW   rQ   rm   rv   rV   )r0   re   rm   rƒ   Úwhatr   r   r   rq   ›   s"   

þzBlueprint.stopc                 C   s*   z
| j j|d� W d S  ty   Y d S w )N)Útimeout)rW   ÚwaitÚIGNORE_ERRORS)r0   rˆ   r   r   r   Újoin²   s
   ÿzBlueprint.joinc                 K   s–   |   d¡ g  }| _|  ¡  }| _|   d¡ |  |¡D ]}||fi |¤Ž}|||j< | |¡ q|   dd dd„ | jD ƒ¡¡ |D ]}| |¡ qA| S )ar  Apply the steps in this blueprint to an object.

        This will apply the ``__init__`` and ``include`` methods
        of each step, with the object as argument::

            step = Step(obj)
            ...
            step.include(obj)

        For :class:`StartStopStep` the services created
        will also be added to the objects ``steps`` attribute.
        zPreparing bootsteps.zBuilding graph...zNew boot order: {%s}z, c                 s   ó   � | ]}|j V  qd S r7   r   rZ   r   r   r   r\   Ñ   ó   € z"Blueprint.apply.<locals>.<genexpr>)	r`   ÚorderÚclaim_stepsrX   Ú_finalize_stepsr   Úappendr‹   Úinclude)r0   re   ÚkwargsrŽ   rX   ÚSr1   r   r   r   Úapplyº   s   



ÿzBlueprint.applyc                 C   s8   | j j |j j¡ | j  t|jd ƒt| jd ƒ¡ d S )Nr   r   )ÚgraphÚadjacentrB   Úadd_edgerP   rŽ   )r0   Úotherr   r   r   Úconnect_withÖ   s   &zBlueprint.connect_withc                 C   s
   | j | S r7   )rX   )r0   r   r   r   r   Ú__getitem__Ú   ó   
zBlueprint.__getitem__c                 C   s   t dd„ | j ¡ D ƒd ƒS )Nc                 s   s   � | ]}|j r|V  qd S r7   )r3   ©r[   ÚCr   r   r   r\   Þ   ó   € z'Blueprint._find_last.<locals>.<genexpr>)ÚnextrX   Úvaluesri   r   r   r   Ú
_find_lastÝ   s   zBlueprint._find_lastc                 C   s~   |  ¡ D ]}dd„ |jD ƒ|_qtdd„ |  ¡ D ƒƒ}|r=| ¡ D ]}t|ƒ}|j| jvr2|||j< | |j¡ q!|sd S d S )Nc                 S   s   g | ]}t |ƒ‘qS r   r   )r[   Údepr   r   r   Ú
<listcomp>â   s    z(Blueprint._firstpass.<locals>.<listcomp>c                 s   rŒ   r7   ©Úrequires©r[   r1   r   r   r   r\   ã   r�   z'Blueprint._firstpass.<locals>.<genexpr>)r¡   r¦   r   Úpopleftr   r   rX   r‘   )r0   rX   r1   Ústreamr>   r   r   r   Ú
_firstpassà   s   
ûzBlueprint._firstpassc              
   C   s�   |   ¡ }|  |¡ dd„ | ¡ D ƒ}t|| j|d�d� }| _|r0|D ]}||kr/| ||¡ q#z| ¡ W S  tyG } ztd| ƒ‚d }~ww )Nc                 s   s   � | ]}||j fV  qd S r7   r¥   r�   r   r   r   r\   î   rŸ   z,Blueprint._finalize_steps.<locals>.<genexpr>)Úroot)Ú	formatterzunknown bootstep: %s)	r¢   rª   r¡   r	   r
   r–   r˜   ÚtopsortÚKeyError)r0   rX   r3   ÚitÚGr;   r�   r   r   r   r�   ë   s"   
ÿ€
€ÿzBlueprint._finalize_stepsc                    s   t ‡ fdd„ˆ jD ƒƒS )Nc                 3   s   � | ]}ˆ   |¡V  qd S r7   )Ú	load_stepr§   ri   r   r   r\   ü   rŸ   z(Blueprint.claim_steps.<locals>.<genexpr>)ÚdictrS   ri   r   ri   r   r�   û   ó   zBlueprint.claim_stepsc                 C   s   t |ƒ}|j|fS r7   )r   r   r/   r   r   r   r±   þ   s   
zBlueprint.load_stepc                 G   s   t jt| |ƒg|¢R Ž S r7   )rc   rd   r   )r0   Úmsgr   r   r   r   r`     r³   zBlueprint._debugc                 C   s   t | ƒS r7   )r"   ri   r   r   r   r     s   zBlueprint.alias)NNNNN)rq   rr   F)NTTr   )TFr7   )#rH   rI   rJ   rK   r#   r
   r   r^   ra   rQ   rR   r]   r„   r…   rh   rY   rb   rj   rl   rm   rv   ro   rq   r‹   r•   rš   r›   r¢   rª   r�   r�   r±   r`   Úpropertyr   r   r   r   r   r   J   sJ    ü
ÿ


ÿ
ÿ

r   c                       s8   e Zd ZdZdZdZ‡ fdd„Zdd„ Zdd„ Z‡  Z	S )	ÚStepTypezMeta-class for steps.Nc                    sJ   |  d¡}|r|› d|› �n|}|j||  d¡p|d� tƒ  | |||¡S )NrI   r   r   )rJ   r   )ÚgetrB   ÚsuperÚ__new__)Úclsr   Úbasesr<   ÚmoduleÚqname©Ú	__class__r   r   r¹     s   
þzStepType.__new__c                 C   ó   | j S r7   )r   ©rº   r   r   r   Ú__str__  s   zStepType.__str__c                 C   s
   d  | ¡S )Nzstep:{0.name}{{{0.requires!r}}})r+   rÁ   r   r   r   Ú__repr__  rœ   zStepType.__repr__)
rH   rI   rJ   rK   r   r¦   r¹   rÂ   rÃ   Ú__classcell__r   r   r¾   r   r¶   
  s    	r¶   c                   @   st   e Zd ZdZdZdZdZd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edd„ ƒZdd„ ZdS )r   zÖA Bootstep.

    The :meth:`__init__` method is called when the step
    is bound to a parent object, and can as such be used
    to initialize attributes in the parent object at
    parent instantiation-time.
    NFr   Tc                 K   ó   d S r7   r   )r0   re   r“   r   r   r   rY   ?  ó   zStep.__init__c                 C   rÀ   )z¤Return true if bootstep should be included.

        You can define this as an optional predicate that decides whether
        this step should be created.
        )Úenabledrp   r   r   r   Ú
include_ifB  s   zStep.include_ifc                 O   s   t |g|¢R i |¤ŽS r7   )r   )r0   r   r   r“   r   r   r   r   J  s   zStep.instantiatec                 C   s   |   |¡rd|  |¡fS dS )NT)FN)rÈ   Úcreaterp   r   r   r   Ú_should_includeM  s   
zStep._should_includec                 C   s   |   |¡d S rg   )rÊ   rp   r   r   r   r’   R  ó   zStep.includec                 C   s   dS )zCreate the step.Nr   rp   r   r   r   rÉ   U  s    zStep.createc                 C   s   d| j › d�S )Nz<step: ú>r   ri   r   r   r   rÃ   X  rË   zStep.__repr__c                 C   s   | j pt| ƒS r7   )r-   r"   ri   r   r   r   r   [  s   z
Step.aliasc                 C   rÅ   r7   r   )r0   r;   r   r   r   rl   _  rÆ   z	Step.info)rH   rI   rJ   rK   r   r-   r5   r¦   r3   rÇ   rY   rÈ   r   rÊ   r’   rÉ   rÃ   rµ   r   rl   r   r   r   r   r      s$    	
r   )Ú	metaclassc                   @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )r   z3Bootstep that must be started and stopped in order.Nc                 C   ó   | j r| j  ¡ S d S r7   )r;   rb   rp   r   r   r   rb   k  ó   
ÿzStartStopStep.startc                 C   rÎ   r7   )r;   rq   rp   r   r   r   rq   o  rÏ   zStartStopStep.stopc                 C   rÅ   r7   r   rp   r   r   r   rm   s  rÆ   zStartStopStep.closec                 C   s   | j rt| j d| j jƒƒ S d S )Nrƒ   )r;   r{   rq   rp   r   r   r   rƒ   v  s   ÿzStartStopStep.terminatec                 C   s(   |   |¡\}}|r|| _|j | ¡ |S r7   )rÊ   r;   rX   r‘   )r0   re   ÚincÚretr   r   r   r’   z  s
   zStartStopStep.include)
rH   rI   rJ   rK   r;   rb   rq   rm   rƒ   r’   r   r   r   r   r   c  s    r   c                   @   sB   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
d„ Z	ddd„Z
dS )r   z(Bootstep that starts a message consumer.)z!celery.worker.consumer:ConnectionNc                 C   s   t dƒ‚)Nzmissing get_consumers)ÚNotImplementedError)r0   Úchannelr   r   r   Úget_consumersˆ  s   zConsumerStep.get_consumersc                 C   s2   |j  ¡ }|  |¡| _| jpg D ]}| ¡  qd S r7   )Ú
connectionrÓ   rÔ   Ú	consumersÚconsume)r0   ÚcrÓ   Úconsumerr   r   r   rb   ‹  s
   

ÿzConsumerStep.startc                 C   ó   |   |d¡ d S )NT©Ú_close©r0   rØ   r   r   r   rq   ‘  rk   zConsumerStep.stopc                 C   rÚ   )NFrÛ   rÝ   r   r   r   Úshutdown”  rk   zConsumerStep.shutdownTc                 C   sV   t ƒ }| jpg D ]}|rt|j|jƒ |jr| |j¡ q|D ]	}t|j|jƒ qd S r7   )rQ   rÖ   r   rÕ   ÚcancelrÓ   Úaddrm   )r0   rØ   Úcancel_consumersÚchannelsrÙ   rÓ   r   r   r   rÜ   —  s   €ÿzConsumerStep._close)T)rH   rI   rJ   rK   r¦   rÖ   rÔ   rb   rq   rÞ   rÜ   r   r   r   r   r   ‚  s    r   N)&rK   Úcollectionsr   Ú	threadingr   Úkombu.commonr   Úkombu.utils.encodingr   Úkombu.utils.importsr   Úutils.graphr	   r
   Úutils.importsr   r   Ú	utils.logr   Úgreenletr   ÚImportErrorrŠ   Ú__all__r]   r„   r…   rH   rc   r   r"   r#   r   rP   r¶   r   r   r   r   r   r   r   Ú<module>   s:    ÿ# AC