a
    >^»b°#  ã                	   @   sF  d Z ddlZddlZddlZddlmZ ddlZddlZddlm	Z	 ddl
mZ ddlmZ eeddgd	�d
�Zejd,i e¤Žejdd„ ƒƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zd-dd„Zdd„ Zdd„ Ze d¡ejdddd �ejd!dd"d �ejd#dd$d �ed%ƒe d&¡ejd'd(„ ƒƒƒƒƒƒƒZd)d*„ Zd+e k�rBeƒ  dS ).z+
Fixme: make this into a proper click main
é    N)Údefaultdict)Úunits)Újsio)Újsonnet_loaderz-hz--help)Zhelp_option_names)Zcontext_settingsÚpgraphc                 C   s   dS )z.
    Wire Cell Signal Processing Features
    N© )Úctxr   r   ú@/home/yuhw/wc/wire-cell-python-pr-11/wirecell/pgraph/__main__.pyÚcli   s    r
   c                   @   sF   e Zd Zddd„Zedd„ ƒZdd„ Zdd	d
„Zddd„Zdd„ Z	dS )ÚNodeTc                 K   sl   |st d| ƒ || _|| _| d¡}|d | _z|d | _W n tyV   d| _Y n0 ttƒ| _	|| _
d S )NzNode(%s) with no attributesú:r   é   Ú )ÚprintÚtnÚ_paramsÚsplitÚtypeÚnameÚ
IndexErrorr   ÚsetÚportsÚattrs)Úselfr   Úparamsr   r   r   r	   Ú__init__   s    


zNode.__init__c                 C   s   | j rd| j  S dS )Nz[%s]z	(unnamed))r   ©r   r   r   r	   Údisplay_name*   s    
zNode.display_namec                 C   s   | j |  |¡ dS )zH
        Add a port of end "head" or "tail" and ident (number).
        N)r   Úadd)r   ÚendÚidentr   r   r	   Úadd_port0   s    zNode.add_portNc                 C   s   | j  dd¡S )Nr   Ú_)r   Úreplace)r   Úportr   r   r	   Údot_name6   s    zNode.dot_namec                    s¦   t |tƒrpt|ƒ}t|dƒ}d}||kr,d}|rPd ‡ fdd„|d |… D ƒ¡}|r\||k r`d}d||f }|S t |tƒržd	t|ƒˆ  t| ¡ ƒd
¡f }|S t|ƒS )Né   r   z...ú,c                    s   g | ]}ˆ   |d ¡‘qS )F)Údot_label_one)Ú.0Zvvr   r   r	   Ú
<listcomp>A   ó    z&Node.dot_label_one.<locals>.<listcomp>z,...zlist(%d):[%s]zdict(%d):[%s]F)	Ú
isinstanceÚlistÚlenÚminÚjoinÚdictr(   ÚkeysÚstr)r   ÚvZrecurZsizÚpsizeZlastZvstrr   r   r	   r(   9   s     

 
 zNode.dot_label_onec                 C   sì   t ƒ }d| jv r>dd dd„ t| jd ƒD ƒ¡f }| |¡ | j| jg}| jrˆt| j 	¡ ƒD ](\}}|  
|¡}d||f }| |¡ q^d |¡}d| }| |¡ d| jv rÜdd d	d„ t| jd ƒD ƒ¡f }| |¡ dd |¡f S )
NÚheadz{%s}ú|c                 S   s   g | ]}d ||f ‘qS )z<in%d>%dr   ©r)   Znumr   r   r	   r*   O   r+   z"Node.dot_label.<locals>.<listcomp>z%s = %sz\nÚtailc                 S   s   g | ]}d ||f ‘qS )z	<out%d>%dr   r8   r   r   r	   r*   ]   r+   )r-   r   r0   ÚsortedÚappendr   r   r   r   Úitemsr(   )r   Úretr6   ÚbodyÚkr4   Úoner9   r   r   r	   Ú	dot_labelL   s"    
$




$
zNode.dot_label)T)N)T)
Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r!   r%   r(   rA   r   r   r   r	   r      s   



r   c                 C   s   t | ƒt dƒt dƒfv S )Nr   )r   ©Úxr   r   r	   Ú	is_stringc   s    rH   c                 C   s   t | ƒtfv S )N)r   r-   rF   r   r   r	   Úis_liste   s    rI   c                 C   s   t | ƒsdS ttt| ƒƒS )NF)rI   ÚallÚmaprH   rF   r   r   r	   Úis_list_of_stringg   s    rL   Tc              
      sŒ  t ƒ ‰‡ ‡‡fdd„}tƒ }| D ]B}||dƒ\}}||dƒ\}	}
d| ¡ ||	 ¡ |
f }| |¡ q |�r tˆ ¡ ƒD ]¨\}}|j ¡ D ]”\}}d}t|ƒr¢|g}t|ƒr®|}|s´qˆ|D ]b}|ˆ vrÆq¸ˆ |d¡}|du rüt	|ˆfi ˆ  |i ¡¤Ž}|ˆ|< d| ¡ | ¡ f }| |¡ q¸qˆqvg d¢}t
ˆ ¡ ƒD ](\}}d	| ¡ | ¡ f }| |¡ �q4|D ]}| d
| ¡ �qb| d¡ d |¡S )zÏ
    Return GraphViz text.

    If attrs is a dictionary, append to the node a list of its items.

    If params is True, show the attributes.

    If services is True, include non DFP node components.

    c              
      sr   | | d }zˆ| }W n4 t yL   t|ˆfi ˆ  |i ¡¤Ž}|ˆ|< Y n0 | |  dd¡}| ||¡ ||fS )NÚnoder$   r   )ÚKeyErrorr   Úgetr!   )Úedger   r   ÚnÚp©r   Znodesr   r   r	   rO   x   s    zdotify.<locals>.getr9   r6   z"%s":out%d -> "%s":in%dNz%"%s" -> "%s"[style=dashed,color=gray])zdigraph pgraph {zrankdir=LR;z	node[shape=record];z	"%s"[label="%s"];z	%s;ú}Ú
)r1   r-   r%   r;   r<   r   rH   rL   rO   r   r:   rA   r0   )Zedge_datr   r   ÚservicesrO   ÚedgesrP   ÚtÚtpÚhZhpÚer   rQ   r?   r4   ZtocheckZmaybeZcnr=   ZnnrM   Znodestrr   rS   r	   Údotifyk   sF    
r\   c              	   C   sH   |  d¡}|D ]4}|s qDzt|ƒ}W n ty8   Y n0 | | } q| S )z¯
    Select out a part of obj based on a "."-separated path.  Any
    element of the path that looks like an integer will be cast to
    one assuming it indexes an array.
    Ú.)r   ÚintÚ
ValueError)ÚobjZjpathr@   r   r   r	   Úresolve_pathË   s    

ra   c                 C   sj   t ƒ }| D ]Z}t|ƒt kr(tt|ƒ|ƒ |d }d|v rT|d rT|dt|d ƒ 7 }| di ¡||< q
|S )zo
    Given a list of nodes, return a dictionary of their "data" entries
    keyed by 'type' or 'type:name'
    r   r   r   Údata)r1   r   r   r3   rO   )Úusesr=   r@   r   r   r   r	   Úuses_to_paramsÝ   s    rd   z--dpathz-1zJA dot-delimited path into the data structure to locate a graph-like object)ÚdefaultÚhelpz--params/--no-paramszDEnable/disable the inclusion of contents of configuration parametersz--services/--no-serviceszAEnable/disable the inclusion 'service' (non-node) type componentszin-filezout-filec                 C   sð   |}zt ||ƒ}W n* ty<   t d| ¡ t d¡ Y n0 d}| dd¡|v rxtd|t|ƒf ƒ |d d }	|}
n|d }	| d	t	ƒ ¡}
t
|
ƒ}t|	|||ƒ}tj |¡d dd
… }d||f }tj|dtjd�}|j| d¡d� d
S )aà  Convert a WCT cfg to a GraphViz dot or rendered file.

    The config file may be JSON of Jsonnet.  It is expected to be in
    the form of a WCT configuration sequence with one (typically last)
    entry holding an object with a .data.edges key (eg a TbbFlow or
    Pgraph config object).  If a non-standard structure is given, the
    object with .edges can be located with --dpath

    Example bash command assuming WIRECELL_PATH properly set

    $ wirecell-pgraph dotify mycfg.jsonnet mycfg.pdf

    Or piecewise

    $ wcsonnet mycfg.jsonnet > mycfg.json

    $ wirecell-pgraph dotify mycfg.json mycfg.dot

    $ dot -Tpdf -o mycfg.pdf mycfg.dot

    If jsonnet is given the usual: -A/--tla, -J/--jpath args can be
    given.
    z'failed to resolve path "%s" in object:
r   )ZPgrapherZTbbFlowr   r   z2Pgrapher object found at dpath: "%s" with %d nodesrb   rW   rc   Nzdot -T %s -o %sT)ÚshellÚstdinzutf-8)Úinput)ra   Ú	ExceptionÚclickZechoÚsysÚexitrO   r   r.   r-   rd   r\   ÚosÚpathÚsplitextÚ
subprocessÚPopenÚPIPEZcommunicateÚencode)r   Zdpathr   rV   Zin_fileZout_fileZdatZcfgZ
graph_appsrW   rc   r   ZdtextÚextÚdotÚprocr   r   r	   Ú
cmd_dotifyî   s(    "rx   c                   C   s   t tƒ d� d S )N)r`   )r
   r1   r   r   r   r	   Úmain.  s    ry   Ú__main__)r   )TT)!Ú__doc__rn   rl   ZjsonÚcollectionsr   rq   rk   Úwirecellr   Zwirecell.utilr   Zwirecell.util.clir   r1   ZcmddefÚgroupZpass_contextr
   Úobjectr   rH   rI   rL   r\   ra   rd   ZcommandÚoptionZargumentrx   ry   rB   r   r   r   r	   Ú<module>   sJ   I
`ÿÿÿ6
