Ë
    åhiÉ&  ã                   ó:   — d Z ddlZddlZddlmZ  G d„ de«      Zy)a
  
altgraph.Dot - Interface to the dot language
============================================

The :py:mod:`~altgraph.Dot` module provides a simple interface to the
file format used in the
`graphviz <http://www.research.att.com/sw/tools/graphviz/>`_
program. The module is intended to offload the most tedious part of the process
(the **dot** file generation) while transparently exposing most of its
features.

To display the graphs or to generate image files the
`graphviz <http://www.research.att.com/sw/tools/graphviz/>`_
package needs to be installed on the system, moreover the :command:`dot` and
:command:`dotty` programs must be accesible in the program path so that they
can be ran from processes spawned within the module.

Example usage
-------------

Here is a typical usage::

    from altgraph import Graph, Dot

    # create a graph
    edges = [ (1,2), (1,3), (3,4), (3,5), (4,5), (5,4) ]
    graph = Graph.Graph(edges)

    # create a dot representation of the graph
    dot = Dot.Dot(graph)

    # display the graph
    dot.display()

    # save the dot representation into the mydot.dot file
    dot.save_dot(file_name='mydot.dot')

    # save dot file as gif image into the graph.gif file
    dot.save_img(file_name='graph', file_type='gif')

Directed graph and non-directed graph
-------------------------------------

Dot class can use for both directed graph and non-directed graph
by passing ``graphtype`` parameter.

Example::

    # create directed graph(default)
    dot = Dot.Dot(graph, graphtype="digraph")

    # create non-directed graph
    dot = Dot.Dot(graph, graphtype="graph")

Customizing the output
----------------------

The graph drawing process may be customized by passing
valid :command:`dot` parameters for the nodes and edges. For a list of all
parameters see the `graphviz <http://www.research.att.com/sw/tools/graphviz/>`_
documentation.

Example::

    # customizing the way the overall graph is drawn
    dot.style(size='10,10', rankdir='RL', page='5, 5' , ranksep=0.75)

    # customizing node drawing
    dot.node_style(1, label='BASE_NODE',shape='box', color='blue' )
    dot.node_style(2, style='filled', fillcolor='red')

    # customizing edge drawing
    dot.edge_style(1, 2, style='dotted')
    dot.edge_style(3, 5, arrowhead='dot', label='binds', labelangle='90')
    dot.edge_style(4, 5, arrowsize=2, style='bold')


.. note::

   dotty (invoked via :py:func:`~altgraph.Dot.display`) may not be able to
   display all graphics styles. To verify the output save it to an image file
   and look at it that way.

Valid attributes
----------------

    - dot styles, passed via the :py:meth:`Dot.style` method::

        rankdir = 'LR'   (draws the graph horizontally, left to right)
        ranksep = number (rank separation in inches)

    - node attributes, passed via the :py:meth:`Dot.node_style` method::

        style = 'filled' | 'invisible' | 'diagonals' | 'rounded'
        shape = 'box' | 'ellipse' | 'circle' | 'point' | 'triangle'

    - edge attributes, passed via the :py:meth:`Dot.edge_style` method::

        style     = 'dashed' | 'dotted' | 'solid' | 'invis' | 'bold'
        arrowhead = 'box' | 'crow' | 'diamond' | 'dot' | 'inv' | 'none'
            | 'tee' | 'vee'
        weight    = number (the larger the number the closer the nodes will be)

    - valid `graphviz colors
        <http://www.research.att.com/~erg/graphviz/info/colors.html>`_

    - for more details on how to control the graph drawing process see the
      `graphviz reference
        <http://www.research.att.com/sw/tools/graphviz/refs.html>`_.
é    N)Ú
GraphErrorc                   óh   — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 dd„Zd„ Zdd„Zd„ Zd„ Zd„ Z	d	„ Z
d
„ Zdd„Zdd„Zy)ÚDota0  
    A  class providing a **graphviz** (dot language) representation
    allowing a fine grained control over how the graph is being
    displayed.

    If the :command:`dot` and :command:`dotty` programs are not in the current
    system path their location needs to be specified in the contructor.
    Nc                 óZ  ‡— |i c| _         | _        |
dv sJ ‚|
| _        d| _        d| _        |||	c| _        | _        | _        i i c| _        | _	        |�|€|}|�|€|fd„}|€d}t        «       Š|D ]E  }|€i }n ||«      }|€Œi | j                  |<    | j                  |fi |¤Ž ‰j                  |«       ŒG |�r‰D ]l  }ˆfd„ ||«      D «       D ]V  }|€i }n	 |||«      }|€Œ|| j                  vri | j                  |<   i | j                  |   |<    | j                  ||fi |¤Ž ŒX Œn yy)z!
        Initialization.
        )ÚgraphÚdigraphztmp_dot.dotztmp_neo.dotNc                 ó$   — |j                  | «      S ©N)Úout_nbrs)Únoder   s     ú8C:\des-py\Monitor\venv\Lib\site-packages\altgraph/Dot.pyÚedgefnzDot.__init__.<locals>.edgefn¡   s   € Ø—~‘~ dÓ+Ð+ó    © c              3   ó,   •K  — | ]  }|‰v sŒ|–— Œ y ­wr
   r   )Ú.0ÚnÚseens     €r   ú	<genexpr>zDot.__init__.<locals>.<genexpr>³   s   øè ø€ ÒB 1¸¸Tº	œQÑBùs   ƒ	�)ÚnameÚattrÚtypeÚtemp_dotÚtemp_neoÚdotÚdottyÚneatoÚnodesÚedgesÚsetÚ
node_styleÚaddÚ
edge_style)Úselfr   r   r   ÚnodevisitorÚedgevisitorr   r   r   r   Ú	graphtyper   ÚstyleÚheadÚtailÚ	edgestyler   s                   @r   Ú__init__zDot.__init__   s‚  ø€ ð   $ RÐˆŒ	�4”9àÐ0Ñ0Ð0Ð0ØˆŒ	à%ˆŒØ%ˆŒà+.°°uÐ(ˆŒ�$”*˜dœjð "$ RÐˆŒ
�D”JàÐ  ØˆEØÐ  à#(ó ,ð ˆ=ØˆEä‹uˆØò 	ˆDØÐ"Ø‘á# DÓ)�ØÑ Ø#%�—
‘
˜4Ñ Ø�—‘ Ñ.¨Ò.Ø—‘˜•ð	ð ÐØò 
A�ÛB©¨t«ÔBò 	A�DØ"Ð*Ø$&™	á$/°°dÓ$;˜	Ø Ñ,Ø t§z¡zÑ1Ø/1˜DŸJ™J tÑ,Ø13˜Ÿ
™
 4Ñ(¨Ñ.Ø'˜Ÿ™¨¨dÑ@°iÓ@ñ	Añ
Að r   c                 ó   — || _         y)z+
        Changes the overall style
        N)r   )r$   r   s     r   r(   z	Dot.style¾   s   € ð ˆ�	r   c                 óV  — |dk(  rY| j                  | j                  «       | j                  ›d| j                  ›d| j                  ›�}t	        j
                  |«       n| j                  | j                  «       | j                  ›d| j                  ›�}t	        j
                  |«       y)z6
        Displays the current graph via dotty
        r   ú -o ú N)Úsave_dotr   r   r   ÚosÚsystemr   )r$   ÚmodeÚ	neato_cmdÚplot_cmds       r   ÚdisplayzDot.displayÄ   sm   € ð
 �7Š?Ø�M‰M˜$Ÿ-™-Ô(Ø)-¯«°T·]³]ÀDÇMÂMÐRˆIÜ�I‰I�iÕ à�M‰M˜$Ÿ-™-Ô(à"Ÿj›j¨$¯-ª-Ð8ˆÜ
�	‰	�(Õr   c                 ó\   — || j                   vri | j                   |<   || j                  |<   y)zB
        Modifies a node style to the dot representation.
        N)r   r   )r$   r   Úkwargss      r   r!   zDot.node_styleÓ   s,   € ð �t—z‘zÑ!Ø!ˆD�J‰J�tÑØ!ˆ�
‰
�4Òr   c                 óL   — | j                   D ]  } | j                  |fi |¤Ž Œ y)z*
        Modifies all node styles
        N)r   r!   )r$   r9   r   s      r   Úall_node_stylezDot.all_node_styleÛ   s*   € ð —J‘Jò 	,ˆDØˆD�O‰O˜DÑ+ FÓ+ñ	,r   c                 óæ   — || j                   vrt        d|›�«      ‚	 || j                  |   vri | j                  |   |<   || j                  |   |<   y# t        $ r t        d|›d|›d�«      ‚w xY w)zC
        Modifies an edge style to the dot representation.
        zinvalid node zinvalid edge  z -> r0   N)r   r   r   ÚKeyError)r$   r)   r*   r9   s       r   r#   zDot.edge_styleâ   s|   € ð �t—z‘zÑ!Ý±$Ð8Ó9Ð9ð	GØ˜4Ÿ:™: dÑ+Ñ+Ø)+�—
‘
˜4Ñ  Ñ&Ø%+ˆD�J‰J�tÑ˜TÒ"øÜò 	GÝº$ÂÐEÓFÐFð	Gús   ž5A ÁA0c              #   ó>  K  — | j                   dk(  rd| j                  ›d�–— n:| j                   dk(  rd| j                  ›d�–— nt        d| j                   ›�«      ‚t        | j                  j                  «       «      D ]  \  }}|›d|›d�–— Œ d	–— d
}d}t        | j                  j                  «       «      D ];  \  }}d|›d�–— t        |j                  «       «      D ]  \  }}|||fz  –— Œ |–— Œ= t        | j                  «      D ]ƒ  }t        | j                  |   «      D ]f  }| j                   dk(  rd|›d|›d�–— nd|›d|›d�–— t        | j                  |   |   j                  «       «      D ]  \  }}|||fz  –— Œ |–— Œh Œ… d–— y ­w)Nr   zdigraph z {
r   zgraph zunsupported graphtype z="z";ú
z%s="%s",z];
z	"z" [z" -> "z" -- "z}
)r   r   r   Úsortedr   Úitemsr   r   )	r$   Ú	attr_nameÚ
attr_valueÚcpattÚepattÚ	node_nameÚ	node_attrr)   r*   s	            r   ÚiterdotzDot.iterdotð   s   è ø€ à�9‰9˜	Ó!Ø&*§i£iÐ1Ó1Ø�Y‰Y˜'Ó!Ø$(§I£IÐ/Ó/õ ¸4¿9º9ÐFÓGÐGô &,¨D¯I©I¯O©OÓ,=Ó%>ò 	7Ñ!ˆI�zÚ )ª:Ð6Ó6ð	7àŠ
ð ˆØˆô %+¨4¯:©:×+;Ñ+;Ó+=Ó$>ò 	Ñ ˆI‘yÚ )Ð+Ò+Ü)/°	·±Ó0AÓ)Bò 6Ñ%�	˜:Ø˜y¨*Ð5Ñ5Ó5ð6à‹Kð		ô ˜4Ÿ:™:Ó&ò 	ˆDÜ˜tŸz™z¨$Ñ/Ó0ò �Ø—9‘9 	Ó)Ú04²dÐ;Ó;ð *ò 15²dÐ;Ò;Ü-3°D·J±J¸tÑ4DÀTÑ4J×4PÑ4PÓ4RÓ-Sò :Ñ)�I˜zØ 9¨jÐ"9Ñ9Ó9ð:à“ñð	ð ‹ùs   ‚FFc                 ó"   — | j                  «       S r
   )rH   )r$   s    r   Ú__iter__zDot.__iter__  s   € Ø�|‰|‹~Ðr   c                 óè   — |s(t        j                  t        dd¬«       | j                  }t	        |d«      5 }| j                  «       D ]  }|j                  |«       Œ 	 ddd«       y# 1 sw Y   yxY w)zD
        Saves the current graph representation into a file
        úalways pass a file_nameé   ©Ú
stacklevelÚwN)ÚwarningsÚwarnÚDeprecationWarningr   ÚopenrH   Úwrite)r$   Ú	file_nameÚfpÚchunks       r   r1   zDot.save_dot  sd   € ñ
 Ü�M‰MÔ,Ð.GÐTUÕVØŸ™ˆIä�)˜SÓ!ð 	  RØŸ™›ò  �Ø—‘˜•ñ ÷	 ÷ 	 ñ 	 ús   ·'A(Á(A1c                 óÌ  — |st        j                  t        dd¬«       d}|dk(  re| j                  | j                  «       | j
                  ›d| j                  ›d| j                  ›�}t        j                  |«       | j                  }n'| j                  | j                  «       | j                  }|›d|›�}|›d	|›d| j                  ›d|›�}t        j                  |«       y
)z5
        Saves the dot file as an image file
        rL   rM   rN   Úoutr   r/   r0   ú.z -TN)
rQ   rR   rS   r1   r   r   r   r2   r3   r   )r$   rV   Ú	file_typer4   r5   r6   Ú
create_cmds          r   Úsave_imgzDot.save_img(  s«   € ñ
 Ü�M‰MÔ,Ð.GÐTUÕVØˆIà�7Š?Ø�M‰M˜$Ÿ-™-Ô(Ø)-¯«°T·]³]ÀDÇMÂMÐRˆIÜ�I‰I�iÔ Ø—x‘x‰Hà�M‰M˜$Ÿ-™-Ô(Ø—x‘xˆHâ(©)Ð4ˆ	âÚØ�M‹MÙð	
ˆ
ô 	�	‰	�*Õr   )
NNNNNÚGr   r   r   r   )r   r
   )NÚgifr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r(   r7   r!   r;   r#   rH   rJ   r1   r^   r   r   r   r   r   u   s]   „ ñð ØØØØØØØØØó=Aò~óò"ò,òGò&òPó ôr   r   )rd   r2   rQ   Úaltgraphr   Úobjectr   r   r   r   ú<module>rg      s%   ðñmó\ 
Û å ôLˆ&õ Lr   