
    Gd6                         d Z ddlZddlmZ ddlZddlmZ g dZ edd          d	             Z	 edd
          d             Z
d Zd Zd ZddZddZdS )a  
*****
Pydot
*****

Import and export NetworkX graphs in Graphviz dot format using pydot.

Either this module or nx_agraph can be used to interface with graphviz.

Examples
--------
>>> G = nx.complete_graph(5)
>>> PG = nx.nx_pydot.to_pydot(G)
>>> H = nx.nx_pydot.from_pydot(PG)

See Also
--------
 - pydot:         https://github.com/erocarrera/pydot
 - Graphviz:      https://www.graphviz.org
 - DOT Language:  http://www.graphviz.org/doc/info/lang.html
    N)getpreferredencoding)	open_file)	write_dotread_dotgraphviz_layoutpydot_layoutto_pydot
from_pydot   w)modec                     d}t          j        |t          d           t          |           }|                    |                                           dS )zgWrite NetworkX graph G to Graphviz dot format on path.

    Path can be a string or a file handle.
    znx.nx_pydot.write_dot depends on the pydot package, which hasknown issues and is not actively maintained. Consider usingnx.nx_agraph.write_dot instead.

See https://github.com/networkx/networkx/issues/5723   
stacklevelN)warningswarnDeprecationWarningr	   write	to_string)GpathmsgPs       9lib/python3.11/site-packages/networkx/drawing/nx_pydot.pyr   r   &   sP    	?  M#)a8888AJJq{{}}
F    rc                     ddl }d}t          j        |t          d           |                                 }|                    |          }t          |d                   S )aO  Returns a NetworkX :class:`MultiGraph` or :class:`MultiDiGraph` from the
    dot file with the passed path.

    If this file contains multiple graphs, only the first such graph is
    returned. All graphs _except_ the first are silently ignored.

    Parameters
    ----------
    path : str or file
        Filename or file handle.

    Returns
    -------
    G : MultiGraph or MultiDiGraph
        A :class:`MultiGraph` or :class:`MultiDiGraph`.

    Notes
    -----
    Use `G = nx.Graph(nx.nx_pydot.read_dot(path))` to return a :class:`Graph` instead of a
    :class:`MultiGraph`.
    r   Nznx.nx_pydot.read_dot depends on the pydot package, which hasknown issues and is not actively maintained. Consider usingnx.nx_agraph.read_dot instead.

See https://github.com/networkx/networkx/issues/5723r   r   )pydotr   r   r   readgraph_from_dot_datar
   )r   r   r   dataP_lists        r   r   r   8   sg    . LLL	?  M#)a888899;;D &&t,,F fQi   r   c                 N   d}t          j        |t          d           |                     d          rd}nd}|                                 dk    r*|rt          j                    }n=t          j                    }n)|rt          j                    }nt          j	                    }| 
                                                    d          }|d	k    r||_        |                                 D ]N}|
                                                    d          }|d
v r. |j        |fi |                                 O|                                 D ]>}|                                }|                                }	|                                }
g }g }t'          |t(                    r)|                    |                    d                     n3|d         D ]*}|                    |                    d                     +t'          |	t(                    r)|                    |	                    d                     n3|	d         D ]*}|                    |                    d                     +|D ]}|D ]} |j        ||fi |
 @|                                 }|r
||j        d<   	 |                                 d         |j        d<   n# t2          t4          f$ r Y nw xY w	 |                                 d         |j        d<   n# t2          t4          f$ r Y nw xY w|S )a  Returns a NetworkX graph from a Pydot graph.

    Parameters
    ----------
    P : Pydot graph
      A graph created with Pydot

    Returns
    -------
    G : NetworkX multigraph
        A MultiGraph or MultiDiGraph.

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_pydot.to_pydot(K5)
    >>> G = nx.nx_pydot.from_pydot(A)  # return MultiGraph

    # make a Graph instead of MultiGraph
    >>> G = nx.Graph(nx.nx_pydot.from_pydot(A))

    znx.nx_pydot.from_pydot depends on the pydot package, which hasknown issues and is not actively maintained.

See https://github.com/networkx/networkx/issues/5723r   r   NFTgraph" )noder%   edgenodesr   r(   r)   )r   r   r   
get_strictget_typenx
MultiGraphGraphMultiDiGraphDiGraphget_namestripnameget_node_listadd_nodeget_attributesget_edge_list
get_sourceget_destination
isinstancestrappendadd_edger%   get_node_defaults
IndexError	TypeErrorget_edge_defaults)r   r   
multiedgesNr4   pneuvattrsdunodesvnodessource_nodedestination_nodepattrs                     r   r
   r
   b   sc   0	? 
 M#)a8888||D 


zz||w 	AA

AA 	!!AA
A ::<<c""Drzz __ , ,JJLLs##)))
1++((**++++ __ B BLLNN!!a 	,HHQWWS\\""""G* , ,c**++++a 	,HHQWWS\\""""G* , ,c**++++ 	B 	BK$% B B 
;(8AADAAAAB	B
 E ! --//2	"   --//2	"   Hs$   0"K K'&K'+"L L"!L"c                 :    d| v o| d         dk    p| d         dk    S )N:r   r&    )rK   s    r   _check_colon_quotesrV      s(     !8514"5r   c                 0   ddl }d}t          j        |t          d           |                                 rd}nd}t          j        |           dk    o|                                  }| j        }| j	        
                    di           }|dk    r |j        d||d	|}n |j        d
| d
f||d	|}	  |j        di | j	        d          n# t          $ r Y nw xY w	  |j        di | j	        d          n# t          $ r Y nw xY w|                     d          D ]\  }}	d |	                                D             }
t#          |          }t%          |          p*t'          d |
                                D                       }|rt)          d           |j        |fi |
}|                    |           |                                 r|                     dd          D ]\  }}}}d |                                D             }t#          |          t#          |          }}t%          |          p9t%          |          p*t'          d |                                D                       }|rt)          d           |j        ||fdt#          |          i|}|                    |           n|                     d          D ]\  }}}d |                                D             }t#          |          t#          |          }}t%          |          p9t%          |          p*t'          d |                                D                       }|rt)          d           |j        ||fi |}|                    |           |S )a  Returns a pydot graph from a NetworkX graph N.

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

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> P = nx.nx_pydot.to_pydot(K5)

    Notes
    -----

    r   Nznx.nx_pydot.to_pydot depends on the pydot package, which hasknown issues and is not actively maintained.

See https://github.com/networkx/networkx/issues/5723r   r   digraphr%   r'   )
graph_typestrictr&   r(   r)   T)r"   c                 N    i | ]"\  }}t          |          t          |          #S rU   r<   .0krI   s      r   
<dictcomp>zto_pydot.<locals>.<dictcomp>   s*    DDD41aAADDDr   c              3   X   K   | ]%\  }}t          |          pt          |          V  &d S NrV   r]   s      r   	<genexpr>zto_pydot.<locals>.<genexpr>  sP        Aq %Q''A+>q+A+A     r   zNode names and attributes should not contain ":" unless they are quoted with "".                For example the string 'attribute:data1' should be written as '"attribute:data1"'.                Please refer https://github.com/pydot/pydot/issues/258)r"   keysc                 Z    i | ](\  }}|d k    t          |          t          |          )S )keyr\   r]   s      r   r`   zto_pydot.<locals>.<dictcomp>  s0    VVVtq!1PU::CFFCFF:::r   c              3   X   K   | ]%\  }}t          |          pt          |          V  &d S rb   rc   r^   r_   vals      r   rd   zto_pydot.<locals>.<genexpr>  P        "As -Q//K3Fs3K3K     r   a   Node names and attributes should not contain ":" unless they are quoted with "".                    For example the string 'attribute:data1' should be written as '"attribute:data1"'.                    Please refer https://github.com/pydot/pydot/issues/258rg   c                 N    i | ]"\  }}t          |          t          |          #S rU   r\   r]   s      r   r`   zto_pydot.<locals>.<dictcomp>(  s*    HHHtq!CFFCFFHHHr   c              3   X   K   | ]%\  }}t          |          pt          |          V  &d S rb   rc   ri   s      r   rd   zto_pydot.<locals>.<genexpr>.  rk   r   )r'   rU   )r   r   r   r   is_directedr-   number_of_selfloopsis_multigraphr4   r%   getDotset_node_defaultsKeyErrorset_edge_defaultsr*   itemsr<   rV   any
ValueErrorNoder6   edgesEdger>   )rD   r   r   rY   rZ   r4   graph_defaultsr   rF   nodedatastr_nodedataraise_errorrE   rH   rI   rg   edgedatastr_edgedatar)   s                      r   r	   r	      s   " LLL	? 
 M#)a8888 	}} 


#A&&!+EAOO4E4E0EF6DW[["--NrzzEIQZQQ.QQEIKKK
$.v
 
AO
 
..agfo....   ..agfo....    wwDw))  8DD8>>3C3CDDDFF)!,, 
  (..00     	  	H  
 EJq))L))	

1 ,#$7747#@#@ 	 	Aq#xVVx~~7G7GVVVLq663q66qA#A&& &q))   &2&8&8&:&:    	    L  
 5:aAACALAADJJt)	.  gg4g00 	 	NAq(HHx~~7G7GHHHLq663q66qA#A&& &q))   &2&8&8&:&:    	    L  
 5:a33l33DJJtHs$   8C 
CC"C; ;
DDneatoc                 b    d}t          j        |t          d           t          | ||          S )a=  Create node positions using Pydot and Graphviz.

    Returns a dictionary of positions keyed by node.

    Parameters
    ----------
    G : NetworkX Graph
        The graph for which the layout is computed.
    prog : string (default: 'neato')
        The name of the GraphViz program to use for layout.
        Options depend on GraphViz version but may include:
        'dot', 'twopi', 'fdp', 'sfdp', 'circo'
    root : Node from G or None (default: None)
        The node of G from which to start some layout algorithms.

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

    Examples
    --------
    >>> G = nx.complete_graph(4)
    >>> pos = nx.nx_pydot.graphviz_layout(G)
    >>> pos = nx.nx_pydot.graphviz_layout(G, prog="dot")

    Notes
    -----
    This is a wrapper for pydot_layout.
    znx.nx_pydot.graphviz_layout depends on the pydot package, which hasknown issues and is not actively maintained. Consider usingnx.nx_agraph.graphviz_layout instead.

See https://github.com/networkx/networkx/issues/5723r   r   )r   progroot)r   r   r   r   )r   r   r   r   s       r   r   r   ?  s;    >	?  M#)a8888!$T2222r   c                 :   ddl }d}t          j        |t          d           t	          |           }|#|                    dt          |                     |                    |          }t          |t                                }|d	k    rct          d
| d           t                       t          d           t          d           t          d           t          d| d           dS |
                    |          }t          |          dk    sJ |d         }	i }
|                                 D ]}t          |          }t          |          rt          d          |                    |                                          }|	                    |          }t%          |t&                    r|d         }|                                dd         }|9|                    d          \  }}t-          |          t-          |          f|
|<   |
S )a  Create node positions using :mod:`pydot` and Graphviz.

    Parameters
    ----------
    G : Graph
        NetworkX graph to be laid out.
    prog : string  (default: 'neato')
        Name of the GraphViz command to use for layout.
        Options depend on GraphViz version but may include:
        'dot', 'twopi', 'fdp', 'sfdp', 'circo'
    root : Node from G or None (default: None)
        The node of G from which to start some layout algorithms.

    Returns
    -------
    dict
        Dictionary of positions keyed by node.

    Examples
    --------
    >>> G = nx.complete_graph(4)
    >>> pos = nx.nx_pydot.pydot_layout(G)
    >>> pos = nx.nx_pydot.pydot_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_pydot.pydot_layout(G, prog='dot')
        G_layout = {H.nodes[n]['node_label']: p for n, p in H_layout.items()}

    r   Nznx.nx_pydot.pydot_layout depends on the pydot package, which hasknown issues and is not actively maintained.

See https://github.com/networkx/networkx/issues/5723r   r   r   )r   )encodingr'   zGraphviz layout with z failedzTo debug what happened try:zP = nx.nx_pydot.to_pydot(G)zP.write_dot("file.dot")zAnd then run z on file.dotr   zNode names and node attributes should not contain ":" unless they are quoted with "".                For example the string 'attribute:data1' should be written as '"attribute:data1"'.                Please refer https://github.com/pydot/pydot/issues/258rT   ,)r   r   r   r   r	   setr<   
create_dotr   printr!   lenr*   rV   rx   ry   r2   get_noder;   listget_possplitfloat)r   r   r   r   r   r   D_bytesDQ_listQnode_posrF   str_n
pydot_noder(   posxxyys                     r   r   r   h  s$   L LLL	? 
 M#)a8888A	fc$ii    lll%%G 	G244555ABww3d333444+,,,+,,,'(((0d000111 &&q))Fv;;! 	q	AHWWYY 1 1Au%% 	H  
 ZZ&&//11
zz*%%dD!! 	7DllnnQrT"?YYs^^FB 99eBii0HQKOr   )r   N)__doc__r   localer   networkxr-   networkx.utilsr   __all__r   r   r
   rV   r	   r   r   rU   r   r   <module>r      s    *  ' ' ' ' ' '     $ $ $ $ $ $   13  " 13&! &! &!R^ ^ ^B6 6 6r r rj&3 &3 &3 &3R\ \ \ \ \ \r   