o
    Ñó·ip  ã                   @  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 d6dd„Zd6dd„Zd7dd„Zefd8dd„Zd9d d!„Zeeefd:d*d+„Zejd  ejdd… ejee fd;d4d5„Z!dS )<é    )ÚannotationsN©Úwraps)ÚCallableÚIteratoré   )Ú	AutomatonÚInputÚOutputÚState)ÚfindMachines)ÚMethodicalMachine)ÚTypeMachineÚInputProtocolÚCoreÚsÚstrÚreturnc                 C  s   d  |  dd¡¡S )Nz"{}"ú"z\")ÚformatÚreplace©r   © r   úXC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\automat/_visualize.pyÚ_gvquote   s   r   c                 C  s
   d  | ¡S )Nz<{}>)r   r   r   r   r   Ú_gvhtml   s   
r   ÚnameÚchildrenÚattrsc                 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   N)ÚjoinÚsortedÚitemsr   )r   r   r   ZformattedAttrsZformattedChildrenr   r   r   ÚelementMaker   s   

þ
ÿr(   Ú
inputLabelÚoutputLabelsú	list[str]ÚportÚ_EúCallable[..., str]c                   sŒ   i }|rt t|ƒƒ|d< ˆ dˆ d| dd�fd|dœ|¤Ž}dd	i‰‡ ‡fd
d„|D ƒ}ˆ d|ƒg}|r>| ˆ dg|¢R Ž ¡ ˆ dg|¢R Ž 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i ˆ¤Ždd�‘qS )r0   r1   Zpink)r3   r   )r    ZoutputLabel©r-   Z	pointSizer   r   Ú
<listcomp><   s    ÿÿztableMaker.<locals>.<listcomp>ÚtrÚtableN)r   ÚlenÚappend)r)   r*   r,   r-   r/   ZinputLabelCellZoutputLabelCellsÚrowsr   r5   r   Ú
tableMaker&   s(   	þüûþr<   ÚxúCallable[[State], str]c                   s   t ˆ ƒd‡ fdd„ƒ}|S )NÚtr   r   r   c                   s   ˆ | ƒ  dd¡  dd¡S )Nú<z&lt;ú>z&gt;)r   )r?   ©r=   r   r   ÚimplJ   s   zescapify.<locals>.impl)r?   r   r   r   r   )r=   rC   r   rB   r   ÚescapifyI   s   rD   Ú	automatonúAutomaton[State, Input, Output]ÚinputAsStringúCallable[[Input], str]ÚoutputAsStringúCallable[[Output], str]ÚstateAsStringúgraphviz.Digraphc                   s  t |ƒ}t ˆ ƒ‰ t |ƒ}tjdddœddiddid�}|  ¡ D ]}|| ju r+d}d}nd	}d}|j||ƒ|d
|dd� qt|  ¡ ƒD ]H\}}	|	\}
}}}d |¡}||ƒ}d}t	|‡ fdd„|D ƒ|d�}|j|t
|ƒddd� |j||
ƒd ||¡dd� | d ||¡||ƒ¡ qB|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ellipseÚblue)ZfontameÚshapeÚstyler3   zt{}Z	tableportc                   s   g | ]}ˆ |ƒ‘qS r   r   )r    ZoutputSymbol©rI   r   r   r6   {   s    zmakeDigraph.<locals>.<listcomp>)r,   z0.2Únone)ÚlabelÚmarginrP   z{}:{}:w)Z	arrowheadz{}:{}:eN)rD   ÚgraphvizZDigraphZstatesZinitialStateÚnodeÚ	enumerateZallTransitionsr   r<   r   Zedge)rE   rG   rI   rK   ÚdigraphÚstateZ
stateShapeZfontNameÚnZeachTransitionZinStateZinputSymbolZoutStateZoutputSymbolsZthisTransitionr)   r,   r8   r   rR   r   ÚmakeDigraphQ   sN   
ý
û
ý
ýr\   Ú	_prognameÚ_argvÚ_syspathÚ_findMachinesú[Callable[[str], Iterator[tuple[str, MethodicalMachine | TypeMachine[InputProtocol, Core]]]]Ú_printúCallable[..., None]ÚNonec                 C  sr  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	rgdd „ }||j
ƒD ]J\}
}||
d!ƒ | ¡ }|	r�|jd" |
¡|jd#� ||
d$|jƒ |jr¶|j p•|	}|j|_|jd" |
¡|j|j|d%� |r­d&}nd'}||
||jƒ qld(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)ri   ÚdefaultÚactionz--dot-directoryz-dzWhere to write out .dot files.z.automat_visualize)ri   rk   z--image-directoryz-izWhere to write out image files.z--image-typez-tzThe image format.Zpng)ri   Úchoicesrk   z--viewz-vz.View rendered graphs with default image viewerc                  W  s   d S )Nr   )Úargsr   r   r   rb   Í   s   ztool.<locals>._printz...discoveredz{}.dot)ÚfilenameÚ	directoryz...wrote dot into)ro   rp   ÚviewÚcleanupz...wrote image intoz...wrote image and dot intoN)ÚinsertÚargparseÚArgumentParserÚadd_argumentrV   ZFORMATSÚ
parse_argsZdot_directoryZimage_directoryÚquietrh   Z	asDigraphÚsaver   Z
image_typeÚrenderrq   )r]   r^   r_   r`   rb   ZDESCRIPTIONZEPILOGZargumentParserrn   ZexplicitlySaveDotrh   ÚmachinerY   Z	deleteDotÚmsgr   r   r   Útool‹   s†   ÿþ
ÿüüûû
ÿ
ü€êr}   )r   r   r   r   )r   r   r   r   r   r   r   r   )
r)   r   r*   r+   r,   r   r-   r.   r   r   )r=   r>   r   r>   )
rE   rF   rG   rH   rI   rJ   rK   r>   r   rL   )r]   r   r^   r+   r_   r+   r`   ra   rb   rc   r   rd   )"Ú
__future__r   rt   ÚsysÚ	functoolsr   Útypingr   r   rV   Z_corer   r	   r
   r   Z	_discoverr   Z_methodicalr   Z_typedr   r   r   r   r   r(   r<   rD   Úreprr\   ÚargvÚpathÚprintr}   r   r   r   r   Ú<module>   s4    


ü
#
ü;ø