o
    @Bij+                  	   @   s  d dl mZ d dl mZ 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
y@   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d0d1d2d3 e$jd4d5d6d7d8 e"j#d9d:d,Z&e&jd-e%d;d/ e&jd<d1d2d3 e&jd4d5d6d7d8 e"j#d=d>d,Z'e'jd-e%d.d/ e'jd?e d d@dA e'jdBe%d1dCdD e"j#dEdFd,Z(e(jd-e%d.d/ e(jd?e d d@dA e(jdGe%dHdHdIgdJdK dLdM Z)edNdO Z*edPdQ Z+edRdS Z,edTdU Z-dVdW Z.edXdY Z/e0dZkrpe.  dS dS )[    )absolute_import)print_functionN)copy)	Componentz.The 'wamp' command-line tool requires Twisted.z  pip install autobahn[twisted]   )DeferredinlineCallbacks)react)ProcessProtocol)ApplicationError)PublishOptions)SubscribeOptionswamp)progz--urlstorezMA WAMP URL to connect to, like ws://127.0.0.1:8080/ws or rs://localhost:1234/T)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)	r0   
startswith
ValueErrordictprivate_keyr,   r-   r   r3   )optionskindr2    r:   Q/var/www/html/Trade-python/venv/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 r:   r:   .0kvr:   r:   r;   
<dictcomp>       zdo_call.<locals>.<dictcomp>z
result: {})listr   r6   keywordr   r   printformat)reactorsessionr8   r   call_kwargsresultsr:   r:   r;   do_call   s   

rL   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=   r:   r:   r>   r:   r:   r;   rB     rC   zdo_publish.<locals>.<dictcomp>r8   T)acknowledge)rD   r#   rE   r"   r   r   )rH   rI   r8   r#   publish_kwargsr:   r:   r;   
do_publish  s   
rO   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)rG   r>   r:   r:   r;   	<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datar:   r:   r;   outReceived)     z;do_register.<locals>.called.<locals>.DumpOutput.outReceivedc                 S   rX   rY   )r[   stderrr]   r^   r_   r:   r:   r;   errReceived,  rc   z;do_register.<locals>.called.<locals>.DumpOutput.errReceivedc                    s     |jj d S )N)callbackvalueexitCode)r`   reasondoner:   r;   processExited/  s   z=do_register.<locals>.called.<locals>.DumpOutput.processExitedN)__name__
__module____qualname__rb   re   rl   r:   rj   r:   r;   
DumpOutput(  s    rp   .)envpathzFailed with exit-code {}r   )rF   rG   r[   rd   r   osenvironjoinjsondumpsitemsrs   abspathr%   r   r
   spawnProcess	callLaterrf   )argskwrr   exerp   protocodeall_done	countdownr8   rH   rj   r;   called  s0   


zdo_register.<locals>.calledN)r   timesr   r$   r   )rH   rI   r8   r   r:   r   r;   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   )rF   rw   rx   r|   rf   )r}   r~   r   r   rH   r:   r;   	publishedL  s   zdo_subscribe.<locals>.published)match)r8   N)r   r   r   r&   r   r   r   )rH   rI   r8   r   r:   r   r;   do_subscribeC  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_mainr:   r:   r:   r;   _main]  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: {}rP   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={}rP   z
{}: {}
    r   )rF   rG   r-   
authmethodr[   rd   r   errorrv   r}   leave)rI   detailse)
command_fn	exit_coder8   rH   r:   r;   _  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)rF   rG   r!   max_failureslenstop)compfail)failuresr8   rH   r:   r;   r     s   
)top
parse_argsr<   r   rF   r[   exitr%   rt   rs   isabsrG   rd   existsrL   r   r   rO   on_joinr   on_connectfailurestart)rH   	componentr   r   r   r:   )r   r   r   r8   rH   r;   r   e  s8   






	r   __main__)1
__future__r   r   rt   r[   argparserw   r   autobahn.twisted.componentr   ImportErrorrF   r   twisted.internet.deferr   r   twisted.internet.taskr	   twisted.internet.protocolr
   autobahn.wamp.exceptionr   autobahn.wamp.typesr   r   txaiouse_twistedArgumentParserr   add_argumentru   getintadd_subparserssub
add_parserr   strr"   r$   r&   r<   rL   rO   r   r   r   r   rm   r:   r:   r:   r;   <module>   sD  	$


0


7
