o
    Df/                     @   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   I/home/ubuntu/webapp/venv/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   zA 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   )zReturn 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