o
    g.                     @   sh   d Z ddlZddlZddl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   sB   t j| }t jt j| }t j| d dd }|||fS )zDTransforms /some/path/foo.png into ('/some/path', 'foo.png', 'png').   N)ospathbasenamedirnameabspathsplitext)filenamer   storedirtarget r   J/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pylint/graph.pytarget_info_from_filename   s   
r   c                   @   sf   e Zd ZdZ						dddZdd ZeeZdd	ed
edef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 u ri }|| _ || _g | _d | _| dt| d |r'| d|  |r1| d|  |r<| d| d |rT| dv sKJ d| | d	| d | D ]
}| d
| qXd S )Nzdigraph z {zrankdir=zratio=zsize="")r   z
iso-8859-1latin1zunsupported charset z	charset="=)		graphnamerendererlines_sourceemitnormalize_node_idloweritemsjoin)	selfr   rankdirsizeratiocharsetr   additional_paramparamr   r   r   __init__*   s*   
zDotBackend.__init__c                 C   s,   | j du r| d d| j| _ | `| j S )zreturns self._sourceNz}

)r   r   r   r   )r   r   r   r   
get_sourceK   s
   

zDotBackend.get_source
outputfilemapfilereturnc              
   C   s^  d}| j }|du r&d}td|\}}td|\}}t| t| n%t|\}	}	}|s7d}|d | }||vrItd|\}}t| n|}tj|ddd	}
|
| j	 W d   n1 sdw   Y  ||vrt
| jdu rtd
| d| j dtjdk}|rtj| jdd|d||d|g	|d ntj| jd||d|g|d t| |S )a+  Generates a graph file.

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

        :rtype: str
        :return: a path to the generated file
        :raises RuntimeError: if the executable for rendering was not found
        )r   gvNpngz.gvz.png.wutf8)encodingzCannot generate `z` because 'zg' executable not found. Install graphviz, or specify a `.gv` outputfile to produce the DOT source code.win32z-Tcmapxz-oz-T)shell)r   tempfilemkstempr   closer   codecsopenwritesourceshutilwhichr   RuntimeErrorsysplatform
subprocesscallunlink)r   r)   r*   graphviz_extensionsnamer   pdotdot_sourcepathppng_file	use_shellr   r   r   generateU   sZ   



zDotBackend.generatec                 C   s   | j | dS )zAdds <line> to final output.N)r   append)r   liner   r   r   r      s   zDotBackend.emitc              
   K   sN   dd |  D }t|t|}}| | d| ddt| d dS )zxemit an edge from <name1> to <name2>.
        edge properties: see https://www.graphviz.org/doc/info/attrs.html
        c                 S       g | ]\}}| d | dqS z="r   r   .0propvaluer   r   r   
<listcomp>        z(DotBackend.emit_edge.<locals>.<listcomp>z ->  [, ];N)r   r   r   r   sorted)r   name1name2propsattrsn_fromn_tor   r   r   	emit_edge   s   *zDotBackend.emit_edgec                 K   s:   dd |  D }| t| ddt| d dS )zuemit a node with given properties.
        node properties: see https://www.graphviz.org/doc/info/attrs.html
        c                 S   rN   rO   r   rP   r   r   r   rT      rU   z(DotBackend.emit_node.<locals>.<listcomp>rV   rW   rX   N)r   r   r   r   rY   )r   rD   r\   r]   r   r   r   	emit_node   s   (zDotBackend.emit_node)NNNr   r   N)NN)__name__
__module____qualname____doc__r&   r(   propertyr:   strrK   r   r`   ra   r   r   r   r   r   '   s    
!=r   c                 C   s   d|  dS )z)Returns a suitable DOT node id for `nid`.r   r   )nidr   r   r   r      s   r   c                 C   s<   | sdS g }|du r|   }|D ]}t| g t || q|S )zgiven 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
    r   N)keys_get_cyclesset)
graph_dictverticesresultverticer   r   r   
get_cycles   s   rp   c           	      C   s   ||v r=|g}|ddd D ]}||kr n| d| qt|}||}||d |d|  }||vr;|| dS || z| | D ]}||vrZt| |||| || qGW n	 tye   Y nw |  dS )z5recursive function doing the real work for get_cyclesNr   r   )insertminindexrL   rj   addKeyErrorpop)	rl   r   visitedrn   ro   cyclenode
start_fromrs   r   r   r   rj      s0   



rj   )N)re   r7   r   r;   r@   r>   r4   r   r   r   rp   rj   r   r   r   r   <module>   s   
