
    wgq6                         d Z ddlZddlZddlZg dZ ej                  dd      dd       Zd Zd Z	 ej                  d	dd
      d        Z
ddZddZej                  j                  dd      	 dd       Zy)au  
***************
Graphviz AGraph
***************

Interface to pygraphviz AGraph class.

Examples
--------
>>> G = nx.complete_graph(5)
>>> A = nx.nx_agraph.to_agraph(G)
>>> H = nx.nx_agraph.from_agraph(A)

See Also
--------
 - Pygraphviz: http://pygraphviz.github.io/
 - Graphviz:      https://www.graphviz.org
 - DOT Language:  http://www.graphviz.org/doc/info/lang.html
    N)from_agraph	to_agraph	write_dotread_dotgraphviz_layoutpygraphviz_layoutview_pygraphvizT)graphsreturns_graphc                    |s| j                         r2| j                         rt        j                  }nBt        j                  }n1| j                         rt        j
                  }nt        j                  }t        j                  d|      }| j                  | j                  |_        |j                  j                  | j                         | j                         D ]T  }|j                  j                         D ci c]  \  }}t        |      | }}} |j                   t        |      fi | V | j#                         D ]  }t        |d         t        |d         }}t%        |j                        }	|	j                         D ci c]  \  }}t        |      | }}}|j'                         s0|j                  |j                  |d<    |j(                  |fi |  |j(                  |fd|j                  i|  t%        | j                        |j                  d<   t%        | j*                        |j                  d<   t%        | j,                        |j                  d<   |S c c}}w c c}}w )a  Returns a NetworkX Graph or DiGraph from a PyGraphviz graph.

    Parameters
    ----------
    A : PyGraphviz AGraph
      A graph created with PyGraphviz

    create_using : NetworkX graph constructor, optional (default=None)
       Graph type to create. If graph instance, then cleared before populated.
       If `None`, then the appropriate Graph type is inferred from `A`.

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_agraph.to_agraph(K5)
    >>> G = nx.nx_agraph.from_agraph(A)

    Notes
    -----
    The Graph G will have a dictionary G.graph_attr containing
    the default graphviz attributes for graphs, nodes and edges.

    Default node attributes will be in the dictionary G.node_attr
    which is keyed by node.

    Edge attributes will be returned as edge data in G.  With
    edge_attr=False the edge data will be the Graphviz edge weight
    attribute or the value 1 if no edge weight attribute is found.

    r      keygraphnodeedge)is_directed	is_strictnxDiGraphMultiDiGraphGraph
MultiGraphempty_graphnamer   update
graph_attrnodesattritemsstradd_nodeedgesdictis_multigraphadd_edge	node_attr	edge_attr)
Acreate_usingNnkvstr_attreur   s
             _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/drawing/nx_agraph.pyr   r   %   s   @ ==?{{}!zz!{{}!xx!}} 	q,'Avv GGNN1<<  WWY '*+&&,,.9$!QCFAI99

3q6&X&'
 WWY 	51Q4y#ad)1AFF|*.**,7$!QCFAI77 vv!"#&&AJJq!(x(AJJq!4484	5 ALL)AGGG1;;'AGGFO1;;'AGGFOH) : 8s   >I/I5c           	      B   	 ddl }| j                         }t        j                  |       dk(  xr | j                          }|j                  | j                  ||      }|j                  j                  | j                  j                  di              |j                  j                  | j                  j                  di              |j                  j                  | j                  j                  di              |j                  j                  d | j                  j                         D               | j                  d	
      D ]x  \  }}|j!                  |       |j#                  |      }|j                         D ]>  \  }	}
|	dk(  r|
d    d|
d    d|j$                  d<   't'        |
      |j$                  |	<   @ z | j                         r| j)                  d	d	      D ]  \  }}}	}|j                         D ci c]  \  }}|dk7  s|t'        |       }}}|j+                  |t'        |	             |j-                  ||      }|j$                  j                  |        |S | j)                  d	
      D ]q  \  }}}|j                         D ci c]  \  }}|t'        |       }}}|j+                  |       |j-                  ||      }|j$                  j                  |       s |S # t        $ r}t        d      |d}~ww xY wc c}}w c c}}w )a  Returns a pygraphviz graph from a NetworkX graph N.

    Parameters
    ----------
    N : NetworkX graph
      A graph created with NetworkX

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_agraph.to_agraph(K5)

    Notes
    -----
    If N has an dict N.graph_attr an attempt will be made first
    to copy properties attached to the graph (see from_agraph)
    and then updated with the calling arguments if any.

    r   N0requires pygraphviz http://pygraphviz.github.io/)r   strictdirectedr   r   r   c              3   4   K   | ]  \  }}|d vs||f  yw))r   r   r   N ).0r,   r-   s      r1   	<genexpr>zto_agraph.<locals>.<genexpr>   s%      1aa7P.PAs   	Tdatapos,r   !)r;   keysr   )r   )
pygraphvizImportErrorr   r   number_of_selfloopsr$   AGraphr   r   r   r   getr&   r'   r   r   r!   get_noder   r    r"   r%   get_edge)r*   r@   errr5   r4   r(   r+   nodedataar   valr0   r-   edgedatar,   str_edgedatas                   r1   r   r   r   s   (W }}H##A&!+EAOO4E0EFqvvfxHA LLGR01KKqww{{62./KKqww{{62./LL 77==? 
 wwDw) '8	

1JJqM ( 	'HCe|#&q6(!CF81 5u!#hs		'	' 	#$7747#@ 	(Aq#x2:..2BQ$!Qa5jAs1vIQLQJJq!SJ*

1a AFFMM,'	( H  gg4g0 	(NAq(2:..2BC$!QAs1vICLCJJq!

1a AFFMM,'	( HY  WLMSVVW< R Ds)   K8 LLL8	LLLc                 \    t        |       }|j                  |       |j                          y)a  Write NetworkX graph G to Graphviz dot format on path.

    Parameters
    ----------
    G : graph
       A networkx graph
    path : filename
       Filename or file handle to write

    Notes
    -----
    To use a specific graph layout, call ``A.layout`` prior to `write_dot`.
    Note that some graphviz layouts are not guaranteed to be deterministic,
    see https://gitlab.com/graphviz/graphviz/-/issues/1767 for more info.
    N)r   writeclear)Gpathr(   s      r1   r   r      s$      	!AGGDMGGI
    agraph_read_dot)r   r
   r   c                     	 ddl }|j                  |       }t        |      }|j	                          |S # t        $ r}t        d      |d}~ww xY w)zReturns a NetworkX graph from a dot file on path.

    Parameters
    ----------
    path : file or string
       File name or file handle to read.
    r   Nz;read_dot() requires pygraphviz http://pygraphviz.github.io/)file)r@   rA   rC   r   rO   )rQ   r@   rG   r(   grs        r1   r   r      s[    
 	t$A	QBGGII  I
	s   5 	AA

Ac                      t        | |||      S )a  Create node positions for G using Graphviz.

    Parameters
    ----------
    G : NetworkX graph
      A graph created with NetworkX
    prog : string
      Name of Graphviz layout program
    root : string, optional
      Root node for twopi layout
    args : string, optional
      Extra arguments to Graphviz layout program

    Returns
    -------
    Dictionary of x, y, positions keyed by node.

    Examples
    --------
    >>> G = nx.petersen_graph()
    >>> pos = nx.nx_agraph.graphviz_layout(G)
    >>> pos = nx.nx_agraph.graphviz_layout(G, prog="dot")

    Notes
    -----
    This is a wrapper for pygraphviz_layout.

    Note that some graphviz layouts are not guaranteed to be deterministic,
    see https://gitlab.com/graphviz/graphviz/-/issues/1767 for more info.
    )progrootargs)r   )rP   rX   rY   rZ   s       r1   r   r      s    > QT4@@rR   c                 h   	 ddl }||d| z  }t        |       }|j                  ||       i }| D ]H  }|j	                  ||      }		 |	j
                  d   j                  d      }
t        d |
D              ||<   J |S # t        $ r}t        d      |d}~ww xY w#  t        d	|       d
||<   Y xY w)a5  Create node positions for G using Graphviz.

    Parameters
    ----------
    G : NetworkX graph
      A graph created with NetworkX
    prog : string
      Name of Graphviz layout program
    root : string, optional
      Root node for twopi layout
    args : string, optional
      Extra arguments to Graphviz layout program

    Returns
    -------
    node_pos : dict
      Dictionary of x, y, positions keyed by node.

    Examples
    --------
    >>> G = nx.petersen_graph()
    >>> pos = nx.nx_agraph.graphviz_layout(G)
    >>> pos = nx.nx_agraph.graphviz_layout(G, prog="dot")

    Notes
    -----
    If you use complex node objects, they may have the same string
    representation and GraphViz could treat them as the same node.
    The layout may assign both nodes a single location. See Issue #1568
    If this occurs in your case, consider relabeling the nodes just
    for the layout computation using something similar to::

        >>> H = nx.convert_node_labels_to_integers(G, label_attribute="node_label")
        >>> H_layout = nx.nx_agraph.pygraphviz_layout(G, prog="dot")
        >>> G_layout = {H.nodes[n]["node_label"]: p for n, p in H_layout.items()}

    Note that some graphviz layouts are not guaranteed to be deterministic,
    see https://gitlab.com/graphviz/graphviz/-/issues/1767 for more info.
    r   Nr3   z-Groot=)rX   rZ   r<   r=   c              3   2   K   | ]  }t        |        y wN)float)r8   xs     r1   r9   z$pygraphviz_layout.<locals>.<genexpr>9  s     5Qa5s   zno position for node)        r`   )	r@   rA   r   layoutNoder   splittupleprint)rP   rX   rY   rZ   r@   rG   r(   node_posr+   r   xss              r1   r   r     s    PW '$  !AHH$TH"H %q!$	%5!'',B5"55HQK	% O  WLMSVVW	%(!,$HQKs#   A? 3B?	BBBB1   zw+bc                     t               st        j                  d      g d}|D ]   }| j                  vsi  j                  |<   " ddi}	dddddd	}
i } fd
} fd} |d|	        |d|
        |d|       t	               } |d|	        |d|
        |d|       t              sfd}n} j                         rX j                  dd      D ]A  \  }}}}|j                  ||t        |            }t         ||            |j                  d<   C nK j                  d      D ]6  \  }}}|j                  ||      }t         ||            |j                  d<   8 |*d}|r	d| d| }nd| }t        j                  |d      }n	 |j                  |d||       |j                          |r/ddlm} |j#                  |j$                        j'                          |j$                  |fS )a  Views the graph G using the specified layout algorithm.

    Parameters
    ----------
    G : NetworkX graph
        The machine to draw.
    edgelabel : str, callable, None
        If a string, then it specifies the edge attribute to be displayed
        on the edge labels. If a callable, then it is called for each
        edge and it should return the string to be displayed on the edges.
        The function signature of `edgelabel` should be edgelabel(data),
        where `data` is the edge attribute dictionary.
    prog : string
        Name of Graphviz layout program.
    args : str
        Additional arguments to pass to the Graphviz layout program.
    suffix : str
        If `filename` is None, we save to a temporary file.  The value of
        `suffix` will appear at the tail end of the temporary filename.
    path : str, None
        The filename used to save the image.  If None, save to a temporary
        file.  File formats are the same as those from pygraphviz.agraph.draw.
    show : bool, default = True
        Whether to display the graph with :mod:`PIL.Image.show`,
        default is `True`. If `False`, the rendered graph is still available
        at `path`.

    Returns
    -------
    path : str
        The filename of the generated image.
    A : PyGraphviz graph
        The PyGraphviz graph instance used to generate the image.

    Notes
    -----
    If this function is called in succession too quickly, sometimes the
    image is not displayed. So you might consider time.sleep(.5) between
    calls if you experience problems.

    Note that some graphviz layouts are not guaranteed to be deterministic,
    see https://gitlab.com/graphviz/graphviz/-/issues/1767 for more info.

    zAn empty graph cannot be drawn.)r   r   r   fontsize10filledz	#0000FF40z0.75circle)style	fillcolorheightwidthshapec                     g }|j                         D ]:  \  }}|j                  |    vs|j                  |    |<   |j                  |       < y r]   )r   r   append)whichattrsaddedr,   r-   rP   s        r1   update_attrsz%view_pygraphviz.<locals>.update_attrs  sN    KKM 	 DAq&$%q!Q	 rR   c                 n    |D ]  }j                   |    |=  j                   |    sj                   | = y y r]   )r   )ru   rw   r   rP   s      r1   clean_attrsz$view_pygraphviz.<locals>.clean_attrs  s=     	%Dt$	%wwu~ rR   r   r   r   Nc                 D    dj                  dt        |          dg      S )N z  )joinr    )r;   	edgelabels    r1   funczview_pygraphviz.<locals>.func  s"    wwc$y/&:DABBrR   T)r?   r;   labelr:   png_.F)suffixdelete)rQ   formatrX   rZ   r   )Image)lenr   NetworkXExceptionr   r   callabler$   r"   rF   r    r   tempfileNamedTemporaryFiledrawclosePILr   openr   show)rP   r~   rX   rZ   r   rQ   r   rv   r   
edge_attrs
node_attrsgraph_attrsrx   rz   r(   r   r0   r-   r   r;   r   extr   s   ``                     r1   r	   r	   @  s   ` q6""#DEE &E qwwAGGDM
 d#J J K  $$+& 	!A 
#
#% 	"C D ??#$7747#@ 51c4zz!QC1%(d_		'"5
  gg4g0 5
1dzz!Q'%(d_		'"5 |#'FYF**&G 	 FFT4F8JJL 

499""$99a<rR   r]   )neatoNr|   )Ndotr|   r|   NT)__doc__osr   networkxr   __all___dispatchabler   r   r   r   r   r   utils	open_filer	   r7   rR   r1   <module>r      s   ( 
   T2I 3IXBJ, (TJ K(AD9x AuGKO OrR   