3
Õ@q^þ(  ã               @   sV  d dl Z ddlmZ d dlZddlmZ d dlZejdƒZejdƒej	dd„ ƒƒZ
d	d
„ Zdd„ Ze
jdƒejddejƒ ddd�ejddddd�ejddFd�dd„ ƒƒƒƒZe
jdƒejddd d!d�ejd"d#d$ejd$gƒd%d&�ejd'd(d)d*d�ejd+d,dd-d�ejddddd�ejd.ƒd/d0„ ƒƒƒƒƒƒƒZe
jd1ƒejd'd2d3d4d�ejd5d6d d7d�ejd8d9d:d;d�ejddddd�ejddGd�d<d=„ ƒƒƒƒƒƒZe
jd>ƒejd'd2d3d?d�ejd5d6d d7d�ejddddd�ejddHd�d@dA„ ƒƒƒƒƒZe
jdBƒejd'd2d3d4d�ejddd dCd�ejd8d9d:d;d�ejddddd�ejddId�dDdE„ ƒƒƒƒƒƒZdS )Jé    Né   )Újsonnet)ÚrulesÚzioÚflowc             C   s   dS )z)
    ZIO FLOW command line interface
    N© )Úctxr   r   ú+/home/yuhw/zmq/zio/python/zio/mains/flow.pyÚcli
   s    r
   c             C   s@   yt | ƒS  tk
r   Y nX yt| ƒS  tk
r:   Y nX | S )N)ÚintÚ
ValueErrorÚfloat)Úvr   r   r	   Útypeit   s    r   c             C   s`   t ƒ }xT| D ]L}y|jddƒ\}}W n& tk
rJ   tjd|› �ƒ wY nX t|ƒ||< qW |S )zR
    Return dictionary of attribute values parsed from list of "k=v" strings.
    ú=é   zfailed to parse )ÚdictÚsplitr   ÚlogÚerrorr   )ÚattrsÚretÚattrÚkr   r   r   r	   Úattrify   s    
r   ztest-rulesetz-rz	--rulesetTz6A file in JSON or Jsonnet format providing the ruleset)ÚtypeÚrequiredÚhelpz-vz--verbosityÚinfoz9Set logging level (debug, info, warning, error, critical))Údefaultr   r   r   )Únargsc          6      sÚ  t t|jƒ dƒt_t|ƒ}tj| ƒ}�x®t|ƒD �] \}}t	|j
di ƒf|Ž‰ tjdˆ › �ƒ ‡ fdd„}ytj|fˆ Ž}W n. tk
rª }	 z|d|	ƒ w0W Y dd}	~	X nX tjd|› �ƒ tj|ƒ}
tjd	|
› �ƒ |
jƒ }tjd
|› �ƒ |d }y|jf ˆ Ž}W n8 tk
�r> }	 z|d|› d�|	ƒ w0W Y dd}	~	X nX |d }y|jf ˆ Ž}W n8 tk
�r� }	 z|d|› d�|	ƒ w0W Y dd}	~	X nX |d }|�r¤dnd}tjd|› d|d›d|› d|› d|› �
ƒ q0W dS )zL
    Test a rule set by giving attributes as key=value on command line.
    ÚINFOr   zattr: c                s$   t j| › d|› dt› dˆ › �ƒ d S )Nz "z"
Ú
)r   r   Zrule)ZerrtypeÚerr)r   r   r	   Údump?   s    ztest_ruleset.<locals>.dumpz	key errorNzparsed expression: zrule expression: zrule evaluation: Úfilepatzkey error "ú"ÚgrouppatÚrwÚTRUEÚFALSEú#ú Z5sz:/)ÚgetattrÚloggingÚupperr   Úlevelr   r   ÚloadÚ	enumerater   ÚgetÚdebugr   ÚparseÚKeyErrorZRuleÚmatchÚformatr   )ÚrulesetÚ	verbosityr   Úmsg_attrZrsÚindZrobjr$   ÚparsedZkeÚexprZtfr%   Úpathr'   Úgroupr(   r   )r   r	   Útest_ruleset+   s@    



rA   zflow-file-serverz-bz--bindztcp://127.0.0.1:5555z)An address to which the server shall bindz-fz--formatÚhdfzFile format)r   r   r   z-nz--namezfile-serverz!The Zyre node name for the serverz-pz--portz#The port name for the server socketr9   c             C   s¤  ddl }ddlm}m}m}	 ddlm}
 ddlm} ddl	m
} ddlm}m} |dks\t‚tt|jƒ d	ƒt_||ƒ}|jƒ }||||jd
| d f|j| fff|j| ffd�}|	|ƒ}|j||jƒ}|j| ƒ |jƒ  tjd|› d|› d| › �ƒ |
||ƒ}tjd|› d�ƒ x†y|jdƒ W np tk
�r`   |jj ƒ  tj!d|› d�ƒ tj!|jj"ƒ Y n2 t#k
�r� } ztj$|ƒ �wW Y dd}~X nX �qW |j%ƒ  dS )a%  Source and sink ZIO flow protocol to file

    This brings back-end reader and writer handlers to external
    clients via the flow broker.  A ruleset factory dynamically spawns
    handlers based on incoming BOT flow messages.  The file format
    maps to different flavors of handlers.

    r   N)ÚPortÚMessageÚNode)ÚBroker)ÚRuleset)r1   )ÚwriterÚreaderrB   r!   z	inproc://zwriter{port})ZwactorsZractorzdomo-broker ú:z online at z entering loopi'  z
 is lonely)&Úzmqr   rC   rD   rE   Zzio.flow.brokerrF   Zzio.flow.factoriesrG   Zzio.jsonnetr1   Zzio.flow.hdfrH   rI   ÚAssertionErrorr-   r.   r/   r   r0   ÚContextZfile_handlerZclient_handlerÚhandlerÚportÚSERVERÚbindÚonliner   ÚpollÚTimeoutErrorÚpeerÚdrainr4   ÚpeersÚ	Exceptionr   Ústop)rQ   r8   ÚnamerO   r:   r9   rK   rC   rD   rE   rF   ZFactoryZjsonnet_loadrH   rI   r   ÚfactoryÚnodeÚsportÚbrokerÚer   r   r	   Úfile_servere   sD    



r`   zflow-send-tensz--numberé
   z#Number of TENS messages to generatez-cz	--connectz-An address to which this client shall connectz-sz--shapez6000,800z4Comma separated list of integers giving tensor shapec             C   s¬  ddl }ddlm}m}m} ddlm}	 tt|j	ƒ dƒt
_t|ƒ}
|dƒ}|jd|jƒ}|j|ƒ |jƒ  |	|ƒ}ttt|jdƒƒƒ}d	}x|D ]}||9 }q�W tf d
ddœ|
—Ž}|tj|ƒd�}|j|ƒ |jdƒ}t
jdƒ |sêt‚t|d	dd�}t|gtdd�d�|d< tj|ƒ}d| g}x>t| ƒD ]2}|||d�}|j|ƒ t
jd|› d|› �ƒ �q.W t
jdƒ |j|ƒ ƒ t
jdƒ |jƒ  t
jdƒ |j ƒ  t
jdƒ dS )z/
    Generate and flow some TENS messages.
    r   N)rC   rD   rE   )ÚFlowr!   zflow-send-tensÚoutputú,r   r   Úextract)ÚcreditÚ	direction)Úlabeliˆ  z"flow-send-tens: BOT handshake doneÚu)ÚshapeÚwordÚdtypezgen-tens)Úsource)ZtensorsÚmetadataZTENSó   X)rh   Úpayloadzflow-send-tens: z: zflow-send-tens: send EOTz"flow-send-tens: recv EOT (waiting)zflow-send-tens: going offlinezflow-send-tens: end)!rK   r   rC   rD   rE   Úzio.flowrb   r-   r.   r/   r   r0   r   rO   ÚCLIENTÚconnectrR   ÚlistÚmapr   r   r   ÚjsonÚdumpsÚsend_botÚrecv_botr4   rL   ÚrangeÚputÚsend_eotÚrecv_eotÚoffline)Únumberrs   rj   r:   r   rK   rC   rD   rE   rb   r;   ÚcnodeÚcportÚcflowÚsizeÚsr   ÚbotZ	tens_attrrh   rp   ÚcountÚmsgr   r   r	   Ú	send_tens©   sF    










rˆ   zflow-recv-tensz9Number of TENS messages to recv before exiting, 0=foreverc             C   sl  ddl }ddlm}m}m} ddlm} tt|j	ƒ dƒt
_t|ƒ}	|dƒ}
|
jd|jƒ}|j|ƒ |
jƒ  ||ƒ}tf dd	d
œ|	—Ž}|tj|ƒd�}|j|ƒ |jdƒ}t
jdƒ |s¼t‚d}xb| dkrÔ|| krÔP |
 
  |jƒ }t
jd|› d|› �ƒ |dkrÂ|jƒ  |
jƒ  t
jdƒ dS qÂW t
jdƒ |j|ƒ ƒ t
jdƒ |jƒ  t
jdƒ |
jƒ  t
jdƒ dS )z/
    Client to recv flow of TENS messages.
    r   N)rC   rD   rE   )rb   r!   zflow-recv-tensÚinputr   Úinject)rf   rg   )rh   iˆ  z"flow-recv-tens: BOT handshake donezflow-recv-tens: z: z$flow-recv-tens: EOT whille receivingzflow-recv-tens: send EOTz"flow-recv-tens: recv EOT (waiting)zflow-recv-tens: going offlinezflow-recv-tens: end)rK   r   rC   rD   rE   rq   rb   r-   r.   r/   r   r0   r   rO   rr   rs   rR   r   rv   rw   rx   ry   r4   rL   r3   r   r|   r~   r}   )r   rs   r:   r   rK   rC   rD   rE   rb   r;   r€   r�   r‚   r   r…   r†   r‡   r   r   r	   Ú	recv_tensã   sF    







r‹   zflow-recv-serverz*An address to which this client shall bindc             C   s  ddl }ddlm}m}m} ddlm}	 tt|j	ƒ dƒt
_t|ƒ}
|dƒ}|jd|jƒ}|j|ƒ |jƒ  |	|ƒ}|jƒ }|s‚t‚|j}d|d	< ||_|j|ƒ t
jd
ƒ |jƒ  t
jdƒ xF|jdƒ}t
jd|› �ƒ | sìd|jd kr¾t
jdƒ |jƒ  P q¾W |jƒ  t
jdƒ dS )zN
    A simple server to catch some TENS messages from flow and dump them.
    r   N)rC   rD   rE   )rb   r!   Úserverr]   rŠ   rg   z$flow-recv-server: BOT handshake donezflow-recv-server: loopingiè  zflow-recv-server: ÚEOTr   zflow-recv-server: got EOTzflow-recv-server: end)rK   r   rC   rD   rE   rq   rb   r-   r.   r/   r   r0   r   rO   rP   rQ   rR   ry   rL   Úlabel_objectrx   r4   Ú	flush_payr3   r   r|   r~   )r   rQ   rj   r:   r   rK   rC   rD   rE   rb   r;   Zsnoder]   Zsflowr…   Zlobjr‡   r   r   r	   Úrecv_server  s8    





r�   éÿÿÿÿr‘   r‘   r‘   )rv   Ú r   Úclickr   r.   Ú	getLoggerr   r@   Úpass_contextr
   r   r   ÚcommandÚoptionÚPathÚargumentrA   ÚChoicer`   rˆ   r‹   r�   r   r   r   r	   Ú<module>   sn   

5



9



 1


2



