Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/graph.cpython-37.pyc

B

�d{T(�@s�dZdZeZddlmZddlZddlZddl	Z	ddl
Z
ddlZdd�Zdd�Z
Gdd	�d	�Zd
d�ZGdd
�d
�ZGdd�de�Zdd�Zddd�Zdd�Zddd�ZdS)z^Graph manipulation utilities.

(dot generation adapted from pypy/translator/tool/make_dot.py)
zrestructuredtext en�NcCs&dd�|�d�D�}d�|�}d|S)z"Make <value> usable in a dot file.cSsg|]}|�dd��qS)�"z\")�replace)�.0�line�r�E/opt/alt/python37/lib/python3.7/site-packages/logilab/common/graph.py�
<listcomp>$szescape.<locals>.<listcomp>�
z\lz\n)�split�join)�value�lines�datarrr�escape"s
rcCs2t�|�}t�t�|��}|�d�d}|||fS)zDTransforms /some/path/foo.png into ('/some/path', 'foo.png', 'png').�.���)�osp�basename�dirname�abspathr
)�filenamer�storedir�targetrrr�target_info_from_filename(s
rc@sXeZdZdZdddddifdd�Zdd�Zee�Zdd	d
�Zdd�Z	d
d�Z
dd�ZdS)�
DotBackendzDot File backend.Nzutf-8�dotc	Cs�||_||_g|_d|_|�dt|��|r<|�d|�|rN|�d|�|r`|�d|�|r�|��dks|td|��|�d|�x$t|�	��D]}|�d�
|��q�WdS)	Nzdigraph %s {z
rankdir=%szratio=%sz	size="%s")zutf-8z
iso-8859-1�latin1zunsupported charset %szcharset="%s"�=)�	graphname�rendererr
�_source�emit�normalize_node_id�lower�AssertionError�sorted�itemsr)	�selfrZrankdir�sizeZratio�charsetrZadditionnal_param�paramrrr�__init__2s"
zDotBackend.__init__cCs,|jdkr&|�d�d�|j�|_|`|jS)zreturns self._sourceNz}
r	)r r!rr
)r'rrr�
get_sourceFs


zDotBackend.get_sourcec
Cs�ddl}|j}|s.|r&|�d�r&|}nd|}|dk	rvt|�\}}}|dkrht�d|�\}	}
t�|	�q�t�	||�}
n8d}t�d|�\}	}
t�d|�\}}t�|	�t�|�t
j|
dd	d
�}	|	�|j
�|	��|dk�r�tjdkr�d}nd
}yJ|�r|j|jdd|d||
d|g	|d�n|j|jd||
d|g|d�WnBtk
�r|}
z"|
jtjk�rld�|j�|
_�Wdd}
~
XYnXt�|
�|S)z�Generates a graph file.

        :param outputfile: filename and path [defaults to graphname.png]
        :param dotfile: filename and path [defaults to graphname.dot]

        :rtype: str
        :return: a path to the generated file
        rNz.dotz%s.dotrZpngz.png�w�utf8)�encoding�win32TFz-Tcmapxz-oz-T)�shellzFile not found: {0})�
subprocessr�endswithr�tempfile�mkstemp�os�closerr�codecs�open�write�source�sys�platform�callr�OSError�errno�ENOENT�format�strerror�unlink)r'�
outputfileZdotfile�mapfiler2�namerrrZpdotZdot_sourcepathZppngZ	use_shell�errr�generatePsJ	





zDotBackend.generatecCs|j�|�dS)zAdds <line> to final output.N)r
�append)r'rrrrr!�szDotBackend.emitc	KsFdd�|��D�}t|�t|�}}|�d||d�t|��f�dS)zwemit an edge from <name1> to <name2>.
        edge properties: see http://www.graphviz.org/doc/info/attrs.html
        cSsg|]\}}d||f�qS)z%s="%s"r)r�proprrrrr�sz(DotBackend.emit_edge.<locals>.<listcomp>z%s -> %s [%s];z, N)r&r"r!rr%)r'Zname1Zname2�props�attrsZn_fromZn_torrr�	emit_edge�szDotBackend.emit_edgecKs6dd�|��D�}|�dt|�d�t|��f�dS)ztemit a node with given properties.
        node properties: see http://www.graphviz.org/doc/info/attrs.html
        cSsg|]\}}d||f�qS)z%s="%s"r)rrKrrrrr�sz(DotBackend.emit_node.<locals>.<listcomp>z%s [%s];z, N)r&r!r"rr%)r'rGrLrMrrr�	emit_node�szDotBackend.emit_node)NNN)�__name__�
__module__�__qualname__�__doc__r+r,�propertyr;rIr!rNrOrrrrr0s
5rcCsd|S)z)Returns a suitable DOT node id for `nid`.z"%s"r)Znidrrrr"�sr"c@seZdZdd�Zddd�ZdS)�GraphGeneratorcCs
||_dS)N)�backend)r'rVrrrr+�szGraphGenerator.__init__NcCs~||_x.|��D]"\}}|�|�}|jj|f|�qWx6|��D]*\}}	}
|�|
||	�}|jj||	f|�q@W|jj||d�S)N)rErF)	�	propshdlrZnodesZnode_propertiesrVrO�edgesZedge_propertiesrNrI)r'�visitorrWrErFZnodeid�noderLZsubjnodeZobjnodeZedgerrrrI�s
zGraphGenerator.generate)NN)rPrQrRr+rIrrrrrU�srUc@seZdZdS)�UnorderableGraphN)rPrQrRrrrrr[�sr[cCsFt|�}|r,d�dd�|D��}td|��t|�}t�}x|��D]}|t|�O}qDW||}|rvtdd�|���g}t�}d}x�|�r|t|�kr�td|��t|�}g}	x6|��D]*\}
}x |D]}||kr�Pq�W|	�|
�q�W|�|	�|t|	�O}x|	D]}
||
=�qWq�Wg}
x t|�D]}|
�	t
|���q&Wt|
�S)	a!takes a dependency graph dict as arguments and return an ordered tuple of
    nodes starting with nodes without dependencies and up to the outermost node.

    If there is some cycle in the graph, :exc:`UnorderableGraph` will be raised.

    Also the given graph dict will be emptied.
    r	cSsg|]}d�|��qS)z -> )r)r�cyclerrrr�sz!ordered_nodes.<locals>.<listcomp>zcycles in graph: %szmissing vertices: %sz, Nzunknown problem with %s)�
get_cyclesrr[�set�values�lenr&rJ�reversed�extendr%�tuple)ZgraphZcycles�verticesZto_verticesrXZmissing_vertices�orderZ	order_setZold_lenZdeps_okrZZ	node_deps�dep�result�grprrr�
ordered_nodes�s@	


ricCs@|sdSg}|dkr|��}x|D]}t|gt�||�q"W|S)z�given a dictionary representing an ordered graph (i.e. key are vertices
    and values is a list of destination vertices representing edges), return a
    list of detected cycles
    rN)�keys�_get_cyclesr^)�
graph_dictrdrg�verticerrrr]�s
r]c	Cs�||kr||g}x,|ddd�D]}||kr,P|�d|�qWt|�}|�|�}||d�|d|�}||krx|�|�dS|�|�y8x2||D]&}||kr�t|||||�|�|�q�WWntk
r�YnX|��dS)z5recursive function doing the real work for get_cyclesNrr)�insert�min�indexrJrk�add�KeyError�pop)	rl�pathZvisitedrgrmr\rZZ
start_fromrprrrrk�s*


rkcCsj|dkrg}n||krdS|�|�x8||D],}||ksHt||||�r.|dd�|gSq.W|��dS)a.generic function taking a simple graph definition as a dictionary, with
    node has key associated to a list of nodes directly reachable from it.

    Return None if no path exists to go from `fromnode` to `tonode`, else the
    first path found (as a list including the destination node at last)
    N�)rJ�has_pathrs)rlZfromnodeZtonodertZdestnoderrrrv	s
rv)N)N)rS�
__docformat__�typeZ
__metaclass__�os.pathrtrr6r<r4r8r@rrrr"rU�	Exceptionr[rir]rkrvrrrr�<module>s$h-