U
    Acjp  ã                   @  s:  d dl mZ d dlZd dlZd dlmZ d dlmZmZ d dl	Z	ddl
mZmZmZmZ ddlmZ ddlmZ dd	lmZmZmZ d
d
dœdd„Zd
d
dœdd„Zd
d
d
d
dœdd„Zefd
dd
dd
dœdd„Zdddœdd„Zeeefdddddd œd!d"„Zejd  ejdd… ejee fd
ddd#d$d%d&œd'd(„Z!dS ))é    )ÚannotationsN©Úwraps)ÚCallableÚIteratoré   )Ú	AutomatonÚInputÚOutputÚState)ÚfindMachines)ÚMethodicalMachine)ÚTypeMachineÚInputProtocolÚCoreÚstr)ÚsÚreturnc                 C  s   d  |  dd¡¡S )Nz"{}"ú"z\")ÚformatÚreplace©r   © r   ú6/tmp/pip-unpacked-wheel-phmf5huh/automat/_visualize.pyÚ_gvquote   s    r   c                 C  s
   d  | ¡S )Nz<{}>)r   r   r   r   r   Ú_gvhtml   s    r   )ÚnameÚchildrenÚattrsr   c                 O  s6   d  dd„ t| ¡ ƒD ƒ¡}d  |¡}dj| ||d�S )z?
    Construct a string from the HTML element description.
    ú c                 s  s&   | ]\}}d   |tt|ƒƒ¡V  qdS )z{}={}N)r   r   r   )Ú.0ÚkeyÚvaluer   r   r   Ú	<genexpr>   s   ÿzelementMaker.<locals>.<genexpr>Ú z#<{name} {attrs}>{children}</{name}>)r   r   r   )ÚjoinÚsortedÚitemsr   )r   r   r   ZformattedAttrsZformattedChildrenr   r   r   ÚelementMaker   s    

þ
  ÿr(   z	list[str]zCallable[..., str])Ú
inputLabelÚoutputLabelsÚportÚ_Er   c                   s„   i }|rt t|ƒƒ|d< ˆ dˆ d| dd�fd|dœ|—Ž}dd	i‰‡ ‡fd
d„|D ƒ}ˆ d|ƒg}|rx| ˆ d|žŽ ¡ ˆ d|žŽ S )z>
    Construct an HTML table to label a state transition.
    ÚcolspanÚtdÚfontzmenlo-italic)ÚfaceZpurple)Úcolorr+   z
point-sizeÚ9c                   s$   g | ]}ˆ d ˆ d|fˆŽdd�‘qS )r.   r/   Zpink)r1   r   )r    ZoutputLabel©r,   Z	pointSizer   r   Ú
<listcomp><   s   ÿztableMaker.<locals>.<listcomp>ÚtrÚtable)r5   )r6   )r   ÚlenÚappend)r)   r*   r+   r,   r-   ZinputLabelCellZoutputLabelCellsZrowsr   r3   r   Ú
tableMaker&   s(    	þüûþr9   zCallable[[State], str])Úxr   c                   s    t ˆ ƒdddœ‡ fdd„ƒ}|S )Nr   r   )Útr   c                   s   ˆ | ƒ  dd¡  dd¡S )Nú<z&lt;ú>z&gt;)r   )r;   ©r:   r   r   ÚimplJ   s    zescapify.<locals>.implr   )r:   r?   r   r>   r   ÚescapifyI   s    r@   zAutomaton[State, Input, Output]zCallable[[Input], str]zCallable[[Output], str]zgraphviz.Digraph)Ú	automatonÚinputAsStringÚoutputAsStringÚstateAsStringr   c                   s  t |ƒ}t ˆ ƒ‰ t |ƒ}tjdddœddiddid�}|  ¡ D ]8}|| jkrVd}d}nd	}d}|j||ƒ|d
|dd� q>t|  ¡ ƒD ]�\}}	|	\}
}}}d |¡}||ƒ}d}t	|‡ fdd„|D ƒ|d�}|j|t
|ƒddd� |j||
ƒd ||¡dd� | d ||¡||ƒ¡ q„|S )zA
    Produce a L{graphviz.Digraph} object from an automaton.
    ÚtrueZ100)ÚpackZdpiZfontnameZMenlo)Z
graph_attrZ	node_attrZ	edge_attrZboldz
Menlo-Boldr$   ZellipseZblue)ZfontameÚshapeÚstyler1   zt{}Z	tableportc                   s   g | ]}ˆ |ƒ‘qS r   r   )r    ZoutputSymbol©rC   r   r   r4   {   s     zmakeDigraph.<locals>.<listcomp>)r+   z0.2Únone)ÚlabelÚmarginrG   z{}:{}:w)Z	arrowheadz{}:{}:e)r@   ÚgraphvizZDigraphZstatesZinitialStateÚnodeÚ	enumerateZallTransitionsr   r9   r   Zedge)rA   rB   rC   rD   ÚdigraphÚstateZ
stateShapeZfontNameÚnZeachTransitionZinStateZinputSymbolZoutStateZoutputSymbolsZthisTransitionr)   r+   r6   r   rI   r   ÚmakeDigraphQ   sN    
ý
û
ý
ýrS   z[Callable[[str], Iterator[tuple[str, MethodicalMachine | TypeMachine[InputProtocol, Core]]]]zCallable[(Ellipsis, None)]ÚNone)Ú	_prognameÚ_argvÚ_syspathÚ_findMachinesÚ_printr   c                 C  sx  d}d}|d r|  dd¡ tj| ||d�}|jddd� |jd	d
dddd� |jddddd� |jddddd� |jdddtjdd� |jdddddd� | |¡}|jo¾|j p¾|j|jk}	|j	rÎdd „ }||j
ƒD ]š\}
}||
d!ƒ | ¡ }|	�r|jd" |
¡|jd#� ||
d$|jƒ |jrØ|j �p.|	}|j|_|jd" |
¡|j|j|d%� |�r`d&}nd'}||
||jƒ qØd(S ))z/
    Entry point for command line utility.
    zB
    Visualize automat.MethodicalMachines as graphviz graphs.
    zz
    You must have the graphviz tool suite installed.  Please visit
    http://www.graphviz.org for more information.
    r   r$   )ÚprogÚdescriptionÚepilogÚfqpnz@A Fully Qualified Path name representing where to find machines.)Úhelpz--quietz-qzsuppress outputFÚ
store_true)r^   ÚdefaultÚactionz--dot-directoryz-dzWhere to write out .dot files.z.automat_visualize)r^   r`   z--image-directoryz-izWhere to write out image files.z--image-typez-tzThe image format.Zpng)r^   Úchoicesr`   z--viewz-vz.View rendered graphs with default image viewerc                  W  s   d S )Nr   )Úargsr   r   r   rY   Í   s    ztool.<locals>._printz...discoveredz{}.dot)ÚfilenameÚ	directoryz...wrote dot into)rd   re   ÚviewÚcleanupz...wrote image intoz...wrote image and dot intoN)ÚinsertÚargparseÚArgumentParserÚadd_argumentrM   ZFORMATSÚ
parse_argsZdot_directoryZimage_directoryÚquietr]   Z	asDigraphÚsaver   Z
image_typeÚrenderrf   )rU   rV   rW   rX   rY   ZDESCRIPTIONZEPILOGZargumentParserrc   ZexplicitlySaveDotr]   ÚmachinerP   Z	deleteDotÚmsgr   r   r   Útool‹   sŽ      ÿþ    ÿüüûû
ÿ
ürr   )"Ú
__future__r   ri   ÚsysÚ	functoolsr   Útypingr   r   rM   Z_corer   r	   r
   r   Z	_discoverr   Z_methodicalr   Z_typedr   r   r   r   r   r(   r9   r@   ÚreprrS   ÚargvÚpathÚprintrr   r   r   r   r   Ú<module>   s2   ü#
ü;ø