o
    >h+                  	   @   s  d dl Z d dlZd dlZd dlZd dlmZ zd dlmZ W n ey4   ed ed e	d Y nw d dl
mZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlZe  ejddZejddddd ejdddddd ejdddde jddd ejddddd ejddd dd ejd!d"ded#d d$ ejd%d&d'Zej d(d)d*Z!e!jd+e"d,d- e!jd.d/d0d1 e!jd2d3d4d5d6 ej d7d8d*Z#e#jd+e"d9d- e#jd:d/d0d1 e#jd2d3d4d5d6 ej d;d<d*Z$e$jd+e"d,d- e$jd=ed d>d? e$jd@e"d/dAdB ej dCdDd*Z%e%jd+e"d,d- e%jd=ed d>d? e%jdEe"dFdFdGgdHdI dJdK Z&edLdM Z'edNdO Z(edPdQ Z)edRdS Z*dTdU Z+edVdW Z,e-dXkrde+  dS dS )Y    N)copy)	Componentz.The 'wamp' command-line tool requires Twisted.z  pip install autobahn[twisted]   )DeferredinlineCallbacks)react)ProcessProtocol)ApplicationError)PublishOptions)SubscribeOptionswamp)progz--urlstorezLA WAMP URL to connect to, like ws://127.0.0.1:8080/ws or rs://localhost:1234T)actionhelprequiredz--realmz-rzThe realm to joindefault)r   r   r   z--private-keyz-kzCHex-encoded private key (via WAMP_PRIVATE_KEY if not provided here)WAMP_PRIVATE_KEYz--authidz$The authid to use, if authenticatingz
--authrolez"The role to use, if authenticatingz--max-failuresz-mz%Failures before giving up (0 forever))r   typer   r   subcommandssubcommand_name)titledestcallz&Do a WAMP call() and print any results)r   urizA WAMP URI to call)r   r   	call_args*z,All additional arguments are positional args)nargsr   z	--keyword   appendz.Specify a keyword argument to send: name value)r   r   r   publishz/Do a WAMP publish() with the given args, kwargszA WAMP URI to publishpublish_argsregisterz2Do a WAMP register() and run a command when calledz--timesz=Listen for this number of events, then exit. Default: forever)r   r   r   commandzTakes one or more args: the executable to call, and any positional arguments. As well, the following environment variables are set: WAMP_ARGS, WAMP_KWARGS and _JSON variants.)r   r   r   	subscribez:Do a WAMP subscribe() and print one line of JSON per eventz--matchexactprefixz4Massed in the SubscribeOptions, how to match the URI)r   r   choicesr   c                 C   s~   | j dr	d}n| j drd}ntdt }| jr.| js#td| j| j| jd|d< t|| j d	g|r9|nd
| jdS )zX
    Configure and return a Component instance according to the given
    `options`
    zws://	websocketzrs://	rawsocketz$URL should start with ws:// or rs://zRRequire --authid and --authrole if --private-key (or WAMP_PRIVATE_KEY) is provided)authidauthroleprivkey
cryptosign)r   urlN)
transportsauthenticationrealm)	r.   
startswith
ValueErrordictprivate_keyr*   r+   r   r1   )optionskindr0    r8   q/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/__main__.py_create_component   s0   

r:   c                 c   sZ    t |j}t }|jd urdd |jD }|j|jg|R i |V }td| d S )Nc                 S      i | ]\}}||qS r8   r8   .0kvr8   r8   r9   
<dictcomp>       zdo_call.<locals>.<dictcomp>z
result: {})listr   r4   keywordr   r   printformat)reactorsessionr6   r   call_kwargsresultsr8   r8   r9   do_call   s   

rJ   c                 c   sT    t |j}|jd u ri ndd |jD }|j|jg|R dtddi|V  d S )Nc                 S   r;   r8   r8   r<   r8   r8   r9   r@     rA   zdo_publish.<locals>.<dictcomp>r6   T)acknowledge)rB   r!   rC   r    r   r
   )rF   rG   r6   r!   publish_kwargsr8   r8   r9   
do_publish   s   
rM   c                 #   s@    t   jgt fdd}||jV   V  dS )z-
    run a command-line upon an RPC call
    c                  ?   s   t d| |tjd ttj}d| |d< t	| |d< ddd |
 D |d< t	||d	< tjjd
 }j} t  G  fdddt}| }j||| |dd  V }|d
krht d| d
 rd
  d8  < d
 d
krd
jd  d S d S d S )Nzcalled: args={}, kwargs={}file 	WAMP_ARGSWAMP_ARGS_JSONc                 s   s     | ]\}}d  ||V  qdS )z{}={}N)rE   r<   r8   r8   r9   	<genexpr>  s    z.do_register.<locals>.called.<locals>.<genexpr>WAMP_KWARGSWAMP_KWARGS_JSONr   c                       s(   e Zd Zdd Zdd Z fddZdS )z/do_register.<locals>.called.<locals>.DumpOutputc                 S      t j|d d S Nutf8)sysstdoutwritedecodeselfdatar8   r8   r9   outReceived%     z;do_register.<locals>.called.<locals>.DumpOutput.outReceivedc                 S   rV   rW   )rY   stderrr[   r\   r]   r8   r8   r9   errReceived(  ra   z;do_register.<locals>.called.<locals>.DumpOutput.errReceivedc                    s     |jj d S )N)callbackvalueexitCode)r^   reasondoner8   r9   processExited+  s   z=do_register.<locals>.called.<locals>.DumpOutput.processExitedN)__name__
__module____qualname__r`   rc   rj   r8   rh   r8   r9   
DumpOutput$  s    rn   .)envpathzFailed with exit-code {}r   )rD   rE   rY   rb   r   osenvironjoinjsondumpsitemsrq   abspathr#   r   r   spawnProcess	callLaterrd   )argskwrp   exern   protocodeall_done	countdownr6   rF   rh   r9   called  s0   


zdo_register.<locals>.calledN)r   timesr   r"   r   )rF   rG   r6   r   r8   r   r9   do_register  s   #
r   c                 #   sJ    t   |jgt fdd}|j||jt|jddV   V  dS )z3
    print events (one line of JSON per event)
    c                     sT   t t| |d d r&d  d8  < d dkr(d jd  d S d S d S )N)r{   kwargsr   r   )rD   ru   rv   rz   rd   )r{   r|   r   r   rF   r8   r9   	publishedH  s   zdo_subscribe.<locals>.published)match)r6   N)r   r   r   r$   r   r   r   )rF   rG   r6   r   r8   r   r9   do_subscribe?  s   
r   c                   C   s   t t dS )zi
    This is a magic name for `python -m autobahn`, and specified as
    our entry_point in setup.py
    N)r   
_real_mainr8   r8   r8   r9   _mainY  s   r   c                 #   s    t  t}jdu rtd td jdkrKjd }tj	
|s6td|tjd td tj	|sKtd|tjd td ttttd	}|j  dg|jt fd
d}g |jfdd}|V  dS )zJ
    Sanity check options, create a connection and run our subcommand
    NzMust select a subcommandr   r"   r   z/Full path to the executable required. Found: {}rN   zExecutable not found: {})r   r"   r$   r    c              
   3   s    t d|j|jtjd z	 | V  W n# ty: } zt d|jd|j	 dd< W Y d }~nd }~ww | 
 V  d S )Nz$connected: authrole={} authmethod={}rN   z
{}: {}
    r   )rD   rE   r+   
authmethodrY   rb   r	   errorrt   r{   leave)rG   detailse)
command_fn	exit_coder6   rF   r8   r9   _  s   z_real_main.<locals>._c                    sV   t d|  | jdkr't jkr)t dt    d S d S d S )Nzconnect failure: {}r   zToo many failures ({}). Exiting)rD   rE   r   max_failureslenstop)compfail)failuresr6   rF   r8   r9   r     s   
)top
parse_argsr:   r   rD   rY   exitr#   rr   rq   isabsrE   rb   existsrJ   r   r   rM   on_joinr   on_connectfailurestart)rF   	componentr}   r   r   r8   )r   r   r   r6   rF   r9   r   a  s8   






	r   __main__).rr   rY   argparseru   r   autobahn.twisted.componentr   ImportErrorrD   r   twisted.internet.deferr   r   twisted.internet.taskr   twisted.internet.protocolr   autobahn.wamp.exceptionr	   autobahn.wamp.typesr
   r   txaiouse_twistedArgumentParserr   add_argumentrs   getintadd_subparserssub
add_parserr   strr    r"   r$   r:   rJ   rM   r   r   r   r   rk   r8   r8   r8   r9   <module>   s@  	$


0


7
