
    hQ#                         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y)z Dependency graph implementation.    )Counter)dedent)bytes_to_strsafe_str)DOT
CycleErrorDependencyGraphGraphFormatterc                   >    e Zd ZdZ ed      ZdZdZdZdZ	ddd	Z
d
Zy)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     N/var/www/Befach/backend/env/lib/python3.12/site-packages/celery/utils/graph.pyr   r   
   s9    .  	D D!D-DGd+DDr   r   c                       e Zd ZdZy)r   z)A cycle was detected in an acyclic graph.N)r   r   r   r   r   r   r   r   r      s    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y)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                 `    |xs
 t               | _        i | _        || j                  |       y y N)r
   	formatteradjacentupdate)selfitr    s      r   __init__zDependencyGraph.__init__*   s-    "6n&6>KKO r   c                 <    | j                   j                  |g        y)zAdd an object to the graph.N)r!   
setdefaultr#   objs     r   add_arczDependencyGraph.add_arc0   s      b)r   c                 ,    | |   j                  |       y)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                 N    | j                   j                  |j                          y)zAdd nodes from another graph.N)r!   r"   )r#   r   s     r   connectzDependencyGraph.connect;   s    U^^,r   c                 V   t               }| j                         }|D ci c]  }|D ]  }||  }}}|D ]  }|j                  |        | D ].  }||   }| |   D ]  }||   }||k7  s|j                  ||       ! 0 |j	                         D 	cg c]  }	|	d   	 c}	S c c}}w c c}	w )zSort the graph topologically.

        Returns:
            List: of objects in the order in which they must be handled.
        r   )r	   	_tarjan72r*   r/   _khan62)
r#   r   
components	componentnodeNCnode_c	successorsuccessor_cts
             r   topsortzDependencyGraph.topsort?   s      !^^%
 .8
 )I
<@D)O

 
 $IMM)$ $DXF!$Z	 m[(NN6;7 (  $mmo.!..
 /s   B B&c                     	 t        | |         g}| |   D ]"  }|j                  | j                  |             $ t	        |      S # t        $ r Y yw xY w)z5Return the valency (degree) of a vertex in the graph.r   )lenKeyErrorr,   
valency_ofsum)r#   r)   lr7   s       r   rA   zDependencyGraph.valency_ofU   sY    	T#Y A IDHHT__T*+ 1v	  		s   A 	AAc                     t        |      }|D ]  \  }}| j                  |        |D ]  \  }}|D ]  }| j                  ||          y)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_   sK    BxFCLL ICc3'  r   c                 0    d | j                         D        S )z8Return generator that yields for all edges in the graph.c              3   ,   K   | ]  \  }}|s	|  y wr   r   ).0r)   adjs      r   	<genexpr>z(DependencyGraph.edges.<locals>.<genexpr>j   s     8SC8s   
)itemsr#   s    r   edgeszDependencyGraph.edgesh   s    8DJJL88r   c                 \   t               }g }| D ]  }| |   D ]  }||xx   dz  cc<     | D cg c]
  }||   r	| }}|rU|j                         }|j                  |       | |   D ])  }||xx   dz  cc<   ||   dk(  s|j                  |       + |rU|j                          |S c c}w )zPerform Khan's simple topological sort algorithm from '62.

        See https://en.wikipedia.org/wiki/Topological_sorting
           r   )r   popr,   reverse)r#   countresultr7   r:   readys         r   r4   zDependencyGraph._khan62l   s    
 	D!$Z	i A%  (  #':$eDk::99;DMM$!$Z	i A% #q(LL+ (	  	 ;s
   
B)B)c                 N     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y t              }|| <   t        	      }	j                  |        |    D ]  } 
|       t        |    |         | <   ! ||    k(  r:t        	|d        }g 	|d  j                  |       |D ]  }t              |<    y y r   )r?   r,   mintuple)r7   num	stack_posr:   r6   itemlowrW   r#   stackvisits         r   rb   z(DependencyGraph._tarjan72.<locals>.visit   s    s{c(CCIE
ILL!$Z	i D	3y>:D	 ( c$i!%	
"34	$&ij!i(%D #D	CI &	  r   r   )r#   r7   r`   rW   ra   rb   s   ` @@@@r   r3   zDependencyGraph._tarjan72   s9      Rs	* 	*& D$K  r   c                 n  	 t               	|xs | j                  fd	fd} j                                | j                         D ]L  \  }}|s |j                  |       |D ]-  } |j
                  |        j                  ||             / N  j                                y)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                 2    t        t        |              y )N)file)printr   )sfhs    r   Pz!DependencyGraph.to_dot.<locals>.P   s    ,q/+r   c                     j                  |      vr/  | |             j                  j                  |             y y r   )labeladd)funr)   ri   drawseens     r   if_not_seenz+DependencyGraph.to_dot.<locals>.if_not_seen   s7    zz#d*#c(C) +r   N)setr    headrO   terminal_noder7   edgetail)
r#   rh   r    rp   r)   r!   reqri   rn   ro   s
    `     @@@r   to_dotzDependencyGraph.to_dot   s     u*DNN	,	*
 	
$))+!ZZ\MCD..4DIIs+$))C%&   * 	
$))+r   c                 @    | j                   r| j                  |      S |S r   )r    r(   s     r   formatzDependencyGraph.format   s    &*nnt~~c"=#=r   c                 ,    t        | j                        S r   )iterr!   rP   s    r   __iter__zDependencyGraph.__iter__   s    DMM""r   c                      | j                   |   S r   r!   )r#   r7   s     r   __getitem__zDependencyGraph.__getitem__   s    }}T""r   c                 ,    t        | j                        S r   )r?   r!   rP   s    r   __len__zDependencyGraph.__len__   s    4==!!r   c                     || j                   v S r   r~   r(   s     r   __contains__zDependencyGraph.__contains__   s    dmm##r   c                 6    | j                   j                         S r   )r!   rO   rP   s    r   _iterate_itemszDependencyGraph._iterate_items   s    }}""$$r   c                 8     dj                   fd D              S )N
c              3   @   K   | ]  }j                  |        y wr   )	repr_node)rL   Nr#   s     r   rN   z+DependencyGraph.__repr__.<locals>.<genexpr>   s     9q*9s   )joinrP   s   `r   __repr__zDependencyGraph.__repr__   s    yy9D999r   c                 `   |j                  || j                  |            g}|| v rx| |   D ]p  }|j                  || j                  |            }|j                  d|z  |z          |j                  | j	                  ||dz         j                  d      dd         r dj                  |      S )Nz     rS   r   )ry   rA   r,   extendr   splitr   )r#   r)   levelfmtoutputotherds          r   r   zDependencyGraph.repr_node   s    **S$//#"678$;cJJudooe&<=go12dnnUEAI>DDTJ12NO # yy  r   NNr   )rS   z{0}({1}))r   r   r   r   r%   r*   r/   r1   r=   rA   r"   rQ   r4   r3   rw   ry   r|   r   r   r   r   rO   	iteritemsr   r   r   r   r   r	   r	      sq    
*-/,(90@8>##"$%&&EI:!r   r	   c                      e Zd ZdZej
                  j                         Zej                  j                         Z	ej                  j                         Zej                  j                         Zej                  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&y)#r
   zFormat dependency graphs.boxveefilledHelveticaNeue)shape	arrowheadstylefontnamedarkseagreen4blackgffffff?)color
arrowcolor	arrowsize
palegreen3
palegreen4)	fillcolorr   
palegreen1
palegreen2bgcolor	mintcreamNc                 ^   |xs d| _         || _        |xs d| _        | j                  | j                     | _        ||xs dz  | _        | j
                  |z   | _        t        | j                  fi || _        t        | j                  | j                  | j                              | _	        y )Ndependenciesr   r   )root)idr   type_dirs	directionININpdictschemegraph_schemerk   )r#   r   r   r   indentinwr   s          r   r%   zGraphFormatter.__init__   s    &	%I	DII.1%77S=4;;1&1 !2!2DII9NOr   c                 J    d| d}| j                  | j                  ||      S )N")namevalue)FMT_attr)r#   r   r   s      r   attrzGraphFormatter.attr  s(    E7!xx

Ux;;r   c           
           t         j                  fi |rt        |fi |xs i n|} j                  j                   fd|j	                         D              S )Nc              3   Z   K   | ]"  \  }}t        j                  ||             $ y wr   )r   r   )rL   kvr#   s      r   rN   z'GraphFormatter.attrs.<locals>.<genexpr>  s)      "
*.!QHTYYq!_%"
s   (+)r   r   _attrsepr   rO   )r#   r   r   s   `  r   attrszGraphFormatter.attrs	  sR    I6V 7qwB 7qI}}!! "
23'')"
 
 	
r   c           	          | j                  | j                  | j                  | j                  | j	                  || j
                              S )N)r   r   r   )r   _headr   r   r   r   )r#   r   s     r   rr   zGraphFormatter.head  s?    xxJJ477**UD$5$56  
 	
r   c                 8    | j                  | j                        S r   )r   _tailrP   s    r   ru   zGraphFormatter.tail  s    xx

##r   c                     |S r   r   r(   s     r   rk   zGraphFormatter.label  s    
r   c                 <    | j                  || j                  |      S r   )	draw_nodenode_schemer#   r)   r   s      r   r7   zGraphFormatter.node      ~~c4#3#3U;;r   c                 <    | j                  || j                  |      S r   )r   term_schemer   s      r   rs   zGraphFormatter.terminal_node  r   r   c                 *     | j                   ||fi |S r   )	draw_edge)r#   abr   s       r   rt   zGraphFormatter.edge!  s    t~~a,e,,r   c                 &    |j                  dd      S )Nzutf-8ignore)encode)r#   rg   s     r   _enczGraphFormatter._enc$  s    xx**r   c                     | j                   |j                  |i t        || j                  | j                              S )N)r   r   )r   ry   r   r   r   )r#   r   argskwargss       r   r   zGraphFormatter.FMT'  s<    yy
&TWW$((;
  	r   c           
          | j                  | j                  | j                  |      | j                  |      | j                  | j	                  || j
                              S )N)dirr   )r   _edgerk   r   r   edge_scheme)r#   r   r   r   r   s        r   r   zGraphFormatter.draw_edge,  sM    xxJJ

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

3tzz%/H  
 	
r   )NNNr   z    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   rr   ru   rk   r7   rs   rt   r   r   r   r   r   r   r   r
   r
      s    #HHNNEHHNNEHHNNEHHNNEHHNNE{{HNE #	F !K
 !-|DK ,|DK{+L04&	P<

$<<-+


r   r
   N)r   collectionsr   textwrapr   kombu.utils.encodingr   r   __all__r   	Exceptionr   r	   r
   r   r   r   <module>r      sE    &   7
D 4 4@! @!FU
 U
r   