
    hw%                         S r SSKJr  SSKrSSKJr  / SQr\" SSS9S	 5       r\" SS
S9\R                  " SSSS9S 5       5       r
\R                  " SSS9S 5       rS rSS jrSS jrg)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
    )getpreferredencodingN)	open_file)	write_dotread_dotgraphviz_layoutpydot_layoutto_pydot
from_pydot   w)modec                 X    [        U 5      nUR                  UR                  5       5        g)z_Write NetworkX graph G to Graphviz dot format on path.

Path can be a string or a file handle.
N)r	   write	to_string)GpathPs      K/var/www/html/env/lib/python3.13/site-packages/networkx/drawing/nx_pydot.pyr   r   &   s"     	AJJq{{}
    rpydot_read_dotT)namegraphsreturns_graphc                 h    SSK nU R                  5       nUR                  U5      n[        US   5      $ )a  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   N)pydotreadgraph_from_dot_datar
   )r   r   dataP_lists       r   r   r   1   s5    0 99;D &&t,F fQi  r   )r   r   c                    U R                  S5      (       a  SnOSnU R                  5       S:X  a3  U(       a  [        R                  " 5       nOH[        R                  " 5       nO2U(       a  [        R
                  " 5       nO[        R                  " 5       nU R                  5       R                  S5      nUS:w  a  X2l	        U R                  5        HK  nUR                  5       R                  S5      nUS;   a  M*  UR                  " U40 UR                  5       D6  MM     U R                  5        GH!  nUR                  5       nUR                  5       nUR                  5       n	/ n
/ n[!        U["        5      (       a!  U
R%                  UR                  S5      5        O,US    H#  nU
R%                  UR                  S5      5        M%     [!        U["        5      (       a!  UR%                  UR                  S5      5        O,US    H#  nUR%                  UR                  S5      5        M%     U
 H  nU H  nUR&                  " X40 U	D6  M     M!     GM$     U R                  5       nU(       a  UUR(                  S'    U R+                  5       S	   UR(                  S
'    U R1                  5       S	   UR(                  S'   U$ ! [,        [.        4 a     N5f = f! [,        [.        4 a     U$ f = f)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))

NFTgraph" )noder"   edgenodesr   r%   r&   )
get_strictget_typenx
MultiGraphGraphMultiDiGraphDiGraphget_namestripr   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   
multiedgesNr   pneuvattrsdunodesvnodessource_nodedestination_nodepattrs                    r   r
   r
   T   se   2 	||D

zz|wA
A!A

A ::<c"Drz __JJLs#))	

1+((*+	  __LLN!aHHQWWS\"G*c*+ % aHHQWWS\"G*c*+ % K$% 

;ADA %& ' 0 E --/2--/2 H 	"  	" Hs$   < J?  K ?KKK)(K)c           	         SSK nU R                  5       (       a  SnOSn[        R                  " U 5      S:H  =(       a    U R	                  5       (       + nU R
                  nU R                  R                  S0 5      nUS:X  a  UR                  " SX#S.UD6nOUR                  " SU S34X#S.UD6n UR                  " S0 U R                  S   D6   UR                  " S0 U R                  S	   D6  U R                  S
S9 Hk  u  pxUR                  5        V	V
s0 s H  u  p[        U	5      [        U
5      _M     nn	n
[        U5      nUR                  " U40 UD6nUR!                  U5        Mm     U R	                  5       (       a  U R#                  S
S
S9 H  u  ppUR                  5        V	V
s0 s H"  u  pU	S:w  d  M  [        U	5      [        U
5      _M$     nn	n
[        U5      [        W
5      pUR$                  " X4S[        U5      0UD6nUR'                  U5        M     U$ U R#                  S
S9 Hv  u  pnUR                  5        V	V
s0 s H  u  p[        U	5      [        U
5      _M     nn	n
[        U5      [        W
5      pUR$                  " X40 UD6nUR'                  U5        Mx     U$ ! [         a     GNf = f! [         a     GNf = fs  sn
n	f s  sn
n	f s  sn
n	f )zReturns 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   Ndigraphr"   r$   )
graph_typestrictr#   r%   r&   T)r   )r   keyskey)r$    )r   is_directedr*   number_of_selfloopsis_multigraphr   r"   getDotset_node_defaultsKeyErrorset_edge_defaultsr'   itemsr8   Noder2   edgesEdger:   )r@   r   rP   rQ   r   graph_defaultsr   rB   nodedatakrE   str_nodedatarA   rD   rS   edgedatastr_edgedatar&   s                     r   r	   r	      s   "  	}}

##A&!+EAOO4E0EF66DWW[["-NrzIIQZQ.QIIvQK
$.
AO
	.aggfo.	.aggfo. wwDw)3;>>3CD3C41AA3CDFJJq)L)	

1	 * 	#$7747#@A#7?~~7GV7Gtq1PU:NCFCFN7GLVq63q6q::aACALADJJt	 $A H  gg4g0NA(7?~~7GH7GtqCFCFN7GLHq63q6q::a3l3DJJt	 1
 H5     E W Is<   1J- J> !K,K<K
!K-
J;:J;>
KKc                     [        XUS9$ )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.
r   progroot)r   rh   s      r   r   r      s    < !T22r   c                    SSK n[        U 5      nUb  UR                  S[        U5      5        UR	                  US9n[        U[        5       S9nUS:X  aJ  [        SU S35        [        5         [        S	5        [        S
5        [        S5        [        SU S35        gUR                  U5      n[        U5      S:X  d   eUS   n0 n	U R                  5        H  n
[        U
5      nUR                  UR                  U5      5      n[        U[        5      (       a  US   nUR                  5       SS nUc  M`  UR                  S5      u  p[!        U5      [!        U5      4X'   M     U	$ )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(H, prog="dot")
    G_layout = {H.nodes[n]["node_label"]: p for n, p in H_layout.items()}

r   Nrj   )ri   )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   ,)r   r	   setr8   
create_dotr   printr   lenr'   get_nodequote_id_if_necessaryr7   listget_possplitfloat)r   ri   rj   r   r   D_bytesDQ_listQnode_posrB   str_nr%   posxxyys                   r   r   r     sW   L A	fc$i  lll%G 	G245ABw%dV734+,+,'(dV<01 &&q)Fv;! 	q	AHWWYAzz%55e<=dD!!7DllnQr"?YYs^FB 9eBi0HK  Or   )neatoN)__doc__localer   networkxr*   networkx.utilsr   __all__r   _dispatchabler   r
   r	   r   r   rT   r   r   <module>r      s   , (  $ 13  13'DI! J !B T2X 3Xv>B3BNr   