
    `fQ#                         d Z ddlmZ ddlmZ ddlmZmZ dZ G d d          Z	 G d d	e
          Z G d
 d          Z G d d          ZdS )z Dependency graph implementation.    )Counter)dedent)bytes_to_strsafe_str)DOT
CycleErrorDependencyGraphGraphFormatterc                   F    e Zd ZdZ ed          ZdZdZdZdZ	ddd	Z
d
ZdS )r   z$Constants related to the dot format.z=
        {IN}{type} {id} {{
        {INp}graph [{attrs}]
    z{name}={value}z{INp}"{0}" [{attrs}]z {INp}"{0}" {dir} "{1}" [{attrs}]z, z--z->)graphdigraphz{IN}}}N)__name__
__module____qualname____doc__r   HEADATTRNODEEDGEATTRSEPDIRSTAIL     D/var/www/html/env/lib/python3.11/site-packages/celery/utils/graph.pyr   r   
   sR        ..6  	 	D D!D-DGd++DDDDr   r   c                       e Zd ZdZdS )r   z)A cycle was detected in an acyclic graph.N)r   r   r   r   r   r   r   r   r      s        3333r   r   c                       e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd ZddZd Zd Zd Zd Zd Zd ZexZZd ZddZdS )r	   a6  A directed acyclic graph of objects and their dependencies.

    Supports a robust topological sort
    to detect the order in which they must be handled.

    Takes an optional iterator of ``(obj, dependencies)``
    tuples to build the graph from.

    Warning:
        Does not support cycle detection.
    Nc                 p    |pt                      | _        i | _        ||                     |           d S d S N)r
   	formatteradjacentupdate)selfitr    s      r   __init__zDependencyGraph.__init__*   s:    "6n&6&6>KKOOOOO >r   c                 <    | j                             |g            dS )zAdd an object to the graph.N)r!   
setdefaultr#   objs     r   add_arczDependencyGraph.add_arc0   s       b)))))r   c                 <    | |                              |           dS )z]Add an edge from object ``A`` to object ``B``.

        I.e. ``A`` depends on ``B``.
        N)append)r#   ABs      r   add_edgezDependencyGraph.add_edge4   s     
 	Qqr   c                 D    | j                             |j                    dS )zAdd nodes from another graph.N)r!   r"   )r#   r   s     r   connectzDependencyGraph.connect;   s     U^,,,,,r   c                 F   t                      }|                                 }d |D             }|D ]}|                    |           | D ]9}||         }| |         D ]&}||         }||k    r|                    ||           ':d |                                D             S )zSort the graph topologically.

        Returns:
            List: of objects in the order in which they must be handled.
        c                     i | ]
}|D ]}||S r   r   ).0	componentnodes      r   
<dictcomp>z+DependencyGraph.topsort.<locals>.<dictcomp>H   s;     
 
 
 )I
 
<@D)
 
 
 
r   c                     g | ]
}|d          S )r   r   )r4   ts     r   
<listcomp>z+DependencyGraph.topsort.<locals>.<listcomp>S   s    ...!...r   )r	   	_tarjan72r*   r/   _khan62)	r#   r   
componentsNCr5   r6   node_c	successorsuccessor_cs	            r   topsortzDependencyGraph.topsort?   s      !!^^%%

 
-7
 
 
 $ 	% 	%IMM)$$$$ 	8 	8DXF!$Z 8 8	 m[((NN6;7778 /.emmoo....r   c                     	 t          | |                   g}n# t          $ r Y dS w xY w| |         D ]*}|                    |                     |                     +t	          |          S )z5Return the valency (degree) of a vertex in the graph.r   )lenKeyErrorr,   
valency_ofsum)r#   r)   lr6   s       r   rF   zDependencyGraph.valency_ofU   s|    	T#Y AA 	 	 	11	I 	, 	,DHHT__T**++++1vvs    
''c                     t          |          }|D ]\  }}|                     |           |D ] \  }}|D ]}|                     ||           !dS )z=Update graph with data from a list of ``(obj, deps)`` tuples.N)listr*   r/   )r#   r$   tupsr)   _depsdeps          r   r"   zDependencyGraph.update_   s}    Bxx 	 	FCLL 	( 	(IC ( (c3''''(	( 	(r   c                 >    d |                                  D             S )z8Return generator that yields for all edges in the graph.c              3   $   K   | ]\  }}||V  d S r   r   )r4   r)   adjs      r   	<genexpr>z(DependencyGraph.edges.<locals>.<genexpr>j   s+      88SC8888888r   )itemsr#   s    r   edgeszDependencyGraph.edgesh   s    88DJJLL8888r   c                 ~   t                      g }| D ]}| |         D ]}|xx         dz  cc<   fd| D             }|rg|                                }|                    |           | |         D ]3}|xx         dz  cc<   |         dk    r|                    |           4|g|                                 |S )zPerform Khan's simple topological sort algorithm from '62.

        See https://en.wikipedia.org/wiki/Topological_sorting
           c                 $    g | ]}|         
|S r   r   )r4   r6   counts     r   r:   z+DependencyGraph._khan62.<locals>.<listcomp>w   s"    :::$eDk::::r   r   )r   popr,   reverse)r#   resultr6   r@   readyrY   s        @r   r<   zDependencyGraph._khan62l   s   
 		 	& 	&D!$Z & &	i   A%    &::::$::: 	,99;;DMM$!$Z , ,	i   A%   #q((LL+++  	, 	r   c                 P     g g i c fd D ]} |           S )zPerform Tarjan's algorithm to find strongly connected components.

        See Also:
            :wikipedia:`Tarjan%27s_strongly_connected_components_algorithm`
        c                    | v rd S t                    }|| <   t          	          }	                    |            |          D ],} 
|           t          |          |                   | <   -||          k    rJt          	|d                    }g 	|d <                       |           |D ]}t                    |<   d S d S r   )rD   r,   mintuple)r6   num	stack_posr@   r5   itemlowr\   r#   stackvisits         r   rg   z(DependencyGraph._tarjan72.<locals>.visit   s    s{{c((CCIE

ILL!$Z ; ;	i   D	3y>::D		c$i!%	

"344	$&ijj!i(((% * *D #D		CII  * *r   r   )r#   r6   re   r\   rf   rg   s   ` @@@@r   r;   zDependencyGraph._tarjan72   sk      Rs	* 	* 	* 	* 	* 	* 	* 	* 	*&  	 	DE$KKKKr   c                   	 t                      	|p| j        fd	fd}                                            |                                 D ]M\  }}|s |j        |           |D ]2} |j        |                                ||                     3N                                            dS )zConvert the graph to DOT format.

        Arguments:
            fh (IO): A file, or a file-like object to write the graph to.
            formatter (celery.utils.graph.GraphFormatter): Custom graph
                formatter to use.
        c                 D    t          t          |                      d S )N)file)printr   )sfhs    r   Pz!DependencyGraph.to_dot.<locals>.P   s"    ,q//++++++r   c                                          |          vr>  | |                                                              |                     d S d S r   )labeladd)funr)   rn   drawseens     r   if_not_seenz+DependencyGraph.to_dot.<locals>.if_not_seen   sW    zz#d**##c((C))))) +*r   N)setr    headrS   terminal_noder6   edgetail)
r#   rm   r    ru   r)   r!   reqrn   rs   rt   s
    `     @@@r   to_dotzDependencyGraph.to_dot   s    uu*DN	, 	, 	, 	, 	,	* 	* 	* 	* 	* 	* 	*
 	
$))++!ZZ\\ 	' 	'MC 5D.444 ' 'DIs+++$))C%%&&&&' 	
$))++r   c                 >    | j         r|                      |          n|S r   )r    r(   s     r   formatzDependencyGraph.format   s     &*n=t~~c"""#=r   c                 *    t          | j                  S r   )iterr!   rT   s    r   __iter__zDependencyGraph.__iter__   s    DM"""r   c                     | j         |         S r   r!   )r#   r6   s     r   __getitem__zDependencyGraph.__getitem__   s    }T""r   c                 *    t          | j                  S r   )rD   r!   rT   s    r   __len__zDependencyGraph.__len__   s    4=!!!r   c                     || j         v S r   r   r(   s     r   __contains__zDependencyGraph.__contains__   s    dm##r   c                 4    | j                                         S r   )r!   rS   rT   s    r   _iterate_itemszDependencyGraph._iterate_items   s    }""$$$r   c                 F     d                      fd D                       S )N
c              3   B   K   | ]}                     |          V  d S r   )	repr_node)r4   Nr#   s     r   rR   z+DependencyGraph.__repr__.<locals>.<genexpr>   s/      99q**999999r   )joinrT   s   `r   __repr__zDependencyGraph.__repr__   s*    yy9999D999999r   rW   {0}({1})c                    |                     ||                     |                    g}|| v r| |         D ]}|                     ||                     |                    }|                    d|z  |z              |                    |                     ||dz                                 d          dd                     d                    |          S )Nz     rW   r   )r~   rF   r,   extendr   splitr   )r#   r)   levelfmtoutputotherds          r   r   zDependencyGraph.repr_node   s    **S$//#"6"6778$;;c P PJJudooe&<&<==go1222dnnUEAI>>DDTJJ122NOOOOyy   r   NNr   )rW   r   )r   r   r   r   r%   r*   r/   r1   rB   rF   r"   rU   r<   r;   r|   r~   r   r   r   r   r   rS   	iteritemsr   r   r   r   r   r	   r	      sU       
 
   * * *  - - -/ / /,  ( ( (9 9 9  0  @   8> > ># # ## # #" " "$ $ $% % %&&EI: : :! ! ! ! ! !r   r	   c                      e Zd ZdZej                                        Zej                                        Z	ej
                                        Zej                                        Zej                                        Zej        Z eej                  ZdddddZddd	d
ZdddZdddZddiZ	 	 d"dZd Zd#dZd Zd Zd Zd Z d Z!d Z"d Z#d Z$d$d Z%d$d!Z&dS )%r
   zFormat dependency graphs.boxveefilledHelveticaNeue)shape	arrowheadstylefontnamedarkseagreen4blackgffffff?)color
arrowcolor	arrowsize
palegreen3
palegreen4)	fillcolorr   
palegreen1
palegreen2bgcolor	mintcreamNr       c                 0   |pd| _         || _        |pd| _        | j        | j                 | _        ||pdz  | _        | j        |z   | _        t          | j        fi || _        t          | j	        | 
                    | j                            | _	        d S )Ndependenciesr   r   )root)idr   type_dirs	directionININpdictschemegraph_schemerp   )r#   r   r   r   indentinwr   s          r   r%   zGraphFormatter.__init__   s    &	%I	DI.1%7S=4;11&11 !2DI9N9NOOOr   c                 H    d| d}|                      | j        ||          S )N")namevalue)FMT_attr)r#   r   r   s      r   attrzGraphFormatter.attr  s)    Exx
Ux;;;r   c           	           t           j        fi |rt          |fi |pi n|} j                             fd|                                D                       S )Nc              3   d   K   | ]*\  }}t                              ||                    V  +d S r   )r   r   )r4   kvr#   s      r   rR   z'GraphFormatter.attrs.<locals>.<genexpr>  sL       "
 "
*.!QHTYYq!__%%"
 "
 "
 "
 "
 "
r   )r   r   _attrsepr   rS   )r#   r   r   s   `  r   attrszGraphFormatter.attrs	  s    II6 HV 7 7qwB 7 7 7qII}!! "
 "
 "
 "
23''))"
 "
 "
 
 
 	
r   c           	          |                      | j        | j        | j        |                     || j                            S )N)r   r   r   )r   _headr   r   r   r   )r#   r   s     r   rw   zGraphFormatter.head  s>    xxJ47**UD$566  
 
 	
r   c                 6    |                      | j                  S r   )r   _tailrT   s    r   rz   zGraphFormatter.tail  s    xx
###r   c                     |S r   r   r(   s     r   rp   zGraphFormatter.label  s    
r   c                 :    |                      || j        |          S r   )	draw_nodenode_schemer#   r)   r   s      r   r6   zGraphFormatter.node      ~~c4#3U;;;r   c                 :    |                      || j        |          S r   )r   term_schemer   s      r   rx   zGraphFormatter.terminal_node  r   r   c                       | j         ||fi |S r   )	draw_edge)r#   abr   s       r   ry   zGraphFormatter.edge!  s    t~a,,e,,,r   c                 .    |                     dd          S )Nzutf-8ignore)encode)r#   rl   s     r   _enczGraphFormatter._enc$  s    xx***r   c                 v    |                       |j        |i t          || j        | j                            S )N)r   r   )r   r~   r   r   r   )r#   r   argskwargss       r   r   zGraphFormatter.FMT'  sF    yy
&TW$(;;;
 
   	r   c           
          |                      | j        |                     |          |                     |          | j        |                     || j                            S )N)dirr   )r   _edgerp   r   r   edge_scheme)r#   r   r   r   r   s        r   r   zGraphFormatter.draw_edge,  sR    xxJ

1tzz!}}djj8H&I&I  
 
 	
r   c                     |                      | j        |                     |          |                     ||                    S )N)r   )r   _noderp   r   )r#   r)   r   r   s       r   r   zGraphFormatter.draw_node2  s>    xxJ

3tzz%/H/H  
 
 	
r   )NNNr   r   r   r   )'r   r   r   r   r   r   stripr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   r   r   rw   rz   rp   r6   rx   ry   r   r   r   r   r   r   r   r
   r
      s       ##HNNEHNNEHNNEHNNEHNNE{HDNNE #	 F ! K
 !-|DDK ,|DDK{+L04&	P 	P 	P 	P< < <
 
 
 

 
 
$ $ $  < < << < <- - -+ + +  

 
 
 

 
 
 
 
 
r   r
   N)r   collectionsr   textwrapr   kombu.utils.encodingr   r   __all__r   	Exceptionr   r	   r
   r   r   r   <module>r      s   & &             7 7 7 7 7 7 7 7
D       4 4 4 4 4 4 4 4@! @! @! @! @! @! @! @!FU
 U
 U
 U
 U
 U
 U
 U
 U
 U
r   