;i&BdZddlZddlZddlmZGddeZdS)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 `_ 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 `_ 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 `_ 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 `_ - for more details on how to control the graph drawing process see the `graphviz reference `_. N) GraphErrorcjeZdZdZ ddZd Zdd Zd Zd Zd Z dZ dZ ddZ ddZ dS)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. NGdotdottyneatodigraphc @|ic|_|_| dvsJ| |_d|_d|_||| c|_|_|_iic|_|_ |||}|||fd}|d}t|D]A} |i} n || } | -i|j| <|j | fi|  | B|dD]c} fd|| DD]G}|i}n || |}|2| |j vr i|j | <i|j | |<|j | |fi|HbdSdS)z! Initialization. )graphr z tmp_dot.dotz tmp_neo.dotNc,||SN)out_nbrs)noder s @C:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\altgraph/Dot.pyedgefnzDot.__init__..edgefns~~d+++c3$K|] }|v|V dSrr).0nseens r zDot.__init__..s'BB1T Q BBr)nameattrtypetemp_dottemp_neorrr nodesedgesset node_styleadd edge_style)selfr rr nodevisitor edgevisitorrrrr graphtyperstyleheadtail edgestylers @r__init__z Dot.__init__s $R 4900000 % % +.u($*dj"$R DJ  E  #( , , , , =Euu  D"# D)) #% 4 .....   A ABBBBt BBB A AD"*$& $/Kd$;$;  ,tz11/1DJt,13 4(.'d@@i@@@ A   A Arc ||_dS)z+ Changes the overall style N)r)r%rs rr)z Dot.styles rc |dkrH||j|jd|jd|j}t j|n||j|jd|j}t j|dS)z6 Displays the current graph via dotty r  -o  N)save_dotrr rossystemr)r%mode neato_cmdplot_cmds rdisplayz Dot.displays 7?? MM$- ( ( ()-T]]]DMMRI Ii MM$- ( ( ("jjj$--8 (rc @||jvr i|j|<||j|<dS)zB Modifies a node style to the dot representation. N)r r)r%rkwargss rr"zDot.node_styles/ tz ! !!DJt ! 4rc 6|jD]}|j|fi|dS)z* Modifies all node styles N)rr")r%r:rs rall_node_stylezDot.all_node_styles<J , ,D DOD + +F + + + + , ,rc ||jvrtd| ||j|vri|j||<||j||<dS#t$rtd|d|dwxYw)zC Modifies an edge style to the dot representation. z invalid node zinvalid edge z -> r1N)rrr KeyError)r%r*r+r:s rr$zDot.edge_styles tz ! !*$$899 9 G4:d+++)+ 4 &%+DJt T " " " G G G*$$$EFF F Gs /A!A/c#&K|jdkrd|jdVn0|jdkrd|jdVntd|jt|jD]\}}|d|dVd Vd }d }t|jD]A\}}d |d Vt|D]\}}|||fzV|VBt|jD]}t|j|D]i}|jdkr d |d|d Vn d |d|d Vt|j||D]\}}|||fzV|VjdVdS)Nr zdigraph z { r zgraph zunsupported graphtype z="z"; z%s="%s",z]; z "z" [z" -> "z" -- "z} )rrrsortedritemsrr ) r% attr_name attr_valuecpattepatt node_name node_attrr*r+s riterdotz Dot.iterdotsF 9 ! ! !&*iii1 1 1 1 1 Y' ! ! !$(III/ / / / /*499FGG G&,DIOO,=,=%>%> 7 7 !Iz ) :::6 6 6 6 6 %+4:+;+;+=+=$>$>   Iyy ) + + + +)/ 0A0A)B)B 6 6% :y*555555KKKK4:&&  Dtz$/00  9 )))04ddd;;;;;*15ddd;;;;-3DJt4DT4J4P4P4R4R-S-S::)Iz9j"999999   rc*|Sr)rI)r%s r__iter__z Dot.__iter__s||~~rc|s#tjtdd|j}t |d5}|D]}|| ddddS#1swxYwYdS)zD Saves the current graph representation into a file always pass a file_name stacklevelwN)warningswarnDeprecationWarningropenrIwrite)r% file_namefpchunks rr2z Dot.save_dots  & M,.GTU V V V V I )S ! ! R                     s-A11A58A5gifc|stjtddd}|dkrO||j|jd|jd|j}tj||j }n!||j|j }|d|}|d |d|jd|}tj|d S) z5 Saves the dot file as an image file rMrNrOoutr r0r1.z -TN) rRrSrTr2rr rr3r4r)r%rW file_typer5r6r7 create_cmds rsave_imgz Dot.save_img(s   M,.GTU V V V VI 7?? MM$- ( ( ()-T]]]DMMRI Ii xHH MM$- ( ( (xH(yy))4 HH II MMM I  *r) NNNNNrrrr r )rr)NrZr)__name__ __module__ __qualname____doc__r-r)r8r"r<r$rIrKr2r`rrrrrus  =A=A=A=A~    """,,, G G G&&&P     rr)rdr3rRaltgraphrobjectrrrrrgsvmm\ LLLLL&LLLLLr