
    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     B1T	QBs   	)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	490000	%%+.u($*dj "$R
DJE#( , =Eu 	D"#D) #%

4 ..	  
ABtB 	AD"*$&	$/d$;	 ,tzz1/1DJJt,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?MM$--()-T]]DMMRIIIi MM$--("jj$--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,     tzz!!DJJt!

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*     JJ 	,DDOOD+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|     tzz!$899	G4::d++)+

4 &%+DJJt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    99	!&*ii11YY'!$(II// 499FGG &,DIIOO,=%> 	7!Iz ):66	7
  %+4::+;+;+=$> 	 Iy )++)/	0A)B 6%	:y*5556K		 4::& 	Dtzz$/0 99	)04d;; * 15d;;-3DJJt4DT4J4P4P4R-S :)Iz9j"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    
 MM,.GTU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    
 MM,.GTUVI7?MM$--()-T]]DMMRIIIi xxHMM$--(xxH()4	MM	

 			*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   