3
tûœ]‘  ã               @   sj   d Z ddlZddlZddljZddlZddlZddlZdd„ Z	G dd„ dƒZ
dd„ Zdd	d
„Zdd„ ZdS )z^Graph manipulation utilities.

(dot generation adapted from pypy/translator/tool/make_dot.py)
é    Nc             C   s2   t j| ƒ}t jt j| ƒƒ}| jdƒd }|||fS )zDTransforms /some/path/foo.png into ('/some/path', 'foo.png', 'png').Ú.é   éÿÿÿÿ)ÚospÚbasenameÚdirnameÚabspathÚsplit)Úfilenamer   ÚstoredirÚtarget© r   ú./tmp/pip-build-fn42xldr/pylint/pylint/graph.pyÚtarget_info_from_filename   s    
r   c               @   sL   e Zd ZdZddd„Zdd„ ZeeƒZdd	d
„Zdd„ Z	dd„ Z
dd„ ZdS )Ú
DotBackendzDot File backend.Núutf-8Údotc       	      C   s¼   |d kri }|| _ || _g | _d | _| jdt|ƒ ƒ |rH| jd| ƒ |rZ| jd| ƒ |rl| jd| ƒ |r–|jƒ dksˆtd| ƒ‚| jd	| ƒ x |jƒ D ]}| jd
j	|ƒƒ q W d S )Nzdigraph %s {z
rankdir=%szratio=%sz	size="%s"úutf-8ú
iso-8859-1Úlatin1zunsupported charset %szcharset="%s"ú=)r   r   r   )
Ú	graphnameÚrendererÚlinesÚ_sourceÚemitÚnormalize_node_idÚlowerÚAssertionErrorÚitemsÚjoin)	Úselfr   ZrankdirÚsizeZratioÚcharsetr   Zadditional_paramÚparamr   r   r   Ú__init__!   s&    

zDotBackend.__init__c             C   s,   | j dkr&| jdƒ dj| jƒ| _ | `| j S )zreturns self._sourceNz}
Ú
)r   r   r    r   )r!   r   r   r   Ú
get_source@   s
    

zDotBackend.get_sourcec          
   C   s2  | j }|s&|r|jdƒr|}nd| }|dk	rnt|ƒ\}}}|dkr`tjd|ƒ\}}	tj|ƒ q¦tj||ƒ}	n8d}tjd|ƒ\}}	tjd|ƒ\}
}tj|ƒ tj|
ƒ t	j
|	ddd	�}|j| jƒ |jƒ  |dk�r.tjd
k}|�rtj| jdd|d||	d|g	|d� ntj| jd||	d|g|d� tj|	ƒ |S )a)  Generates a graph file.

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

        :rtype: str
        :return: a path to the generated file
        z.dotz%s.dotNr   Zpngz.pngÚwÚutf8)ÚencodingÚwin32z-Tcmapxz-oz-T)Úshell)r   Úendswithr   ÚtempfileÚmkstempÚosÚcloser   r    ÚcodecsÚopenÚwriteÚsourceÚsysÚplatformÚ
subprocessÚcallr   Úunlink)r!   Z
outputfileZdotfileZmapfileÚnamer   Ú_r   ZpdotZdot_sourcepathZppngZ	use_shellr   r   r   ÚgenerateJ   sL    






zDotBackend.generatec             C   s   | j j|ƒ dS )zAdds <line> to final output.N)r   Úappend)r!   Úliner   r   r   r   „   s    zDotBackend.emitc             K   sF   dd„ |j ƒ D ƒ}t|ƒt|ƒ }}| jd||djt|ƒƒf ƒ dS )zwemit an edge from <name1> to <name2>.
        edge properties: see http://www.graphviz.org/doc/info/attrs.html
        c             S   s   g | ]\}}d ||f ‘qS )z%s="%s"r   )Ú.0ÚpropÚvaluer   r   r   ú
<listcomp>Œ   s    z(DotBackend.emit_edge.<locals>.<listcomp>z%s -> %s [%s];z, N)r   r   r   r    Úsorted)r!   Zname1Zname2ÚpropsÚattrsZn_fromZn_tor   r   r   Ú	emit_edgeˆ   s    zDotBackend.emit_edgec             K   s6   dd„ |j ƒ D ƒ}| jdt|ƒdjt|ƒƒf ƒ dS )ztemit a node with given properties.
        node properties: see http://www.graphviz.org/doc/info/attrs.html
        c             S   s   g | ]\}}d ||f ‘qS )z%s="%s"r   )r@   rA   rB   r   r   r   rC   ”   s    z(DotBackend.emit_node.<locals>.<listcomp>z%s [%s];z, N)r   r   r   r    rD   )r!   r;   rE   rF   r   r   r   Ú	emit_node�   s    zDotBackend.emit_node)NNNr   r   N)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r'   Úpropertyr5   r=   r   rG   rH   r   r   r   r   r      s        

:r   c             C   s   d|  S )z)Returns a suitable DOT node id for `nid`.z"%s"r   )Únidr   r   r   r   ˜   s    r   c             C   s@   | sf S g }|dkr| j ƒ }x|D ]}t| g tƒ ||ƒ 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
    N)ÚkeysÚ_get_cyclesÚset)Ú
graph_dictZverticesÚresultÚverticer   r   r   Ú
get_cycles�   s    
rU   c       	      C   sà   ||kr||g}x,|ddd… D ]}||kr,P |j d|ƒ qW t|ƒ}|j|ƒ}||d… |d|…  }||krx|j|ƒ dS |j|ƒ y8x2| | D ]&}||kr’t| ||||ƒ |j|ƒ q’W W n tk
rÒ   Y nX |jƒ  dS )z5recursive function doing the real work for get_cyclesNr   r   r   )ÚinsertÚminÚindexr>   rP   ÚaddÚKeyErrorÚpop)	rR   ÚpathÚvisitedrS   rT   ÚcycleÚnodeZ
start_fromrX   r   r   r   rP   ¬   s*    


rP   )N)rL   r2   r0   Úos.pathr\   r   r8   r6   r.   r   r   r   rU   rP   r   r   r   r   Ú<module>   s   
z
