U
    ^»{jc	  ã                   @  s�   d dl mZ d dlZd dlmZ d dlmZ d dlZd dlm	Z	 d dl
mZ ertd dlmZ d dlmZ d d	lmZ e e¡ZG d
d„ dƒZdS )é    )ÚannotationsN)ÚThread)ÚTYPE_CHECKING)Úprotocol)ÚChannelClosedError)ÚAny)ÚBrowserSync)ÚChannelInterfacec                   @  sb   e Zd ZU dZded< ded< ddddœdd	„Zd
ddœdd„Zdddœdd„Zddœdd„ZdS )Ú
BrokerSynczCBrokerSync is a middleware implementation for synchronous browsers.r   Ú_browserr	   Ú_channelÚNone)ÚbrowserÚchannelÚreturnc                 C  s   || _ || _dS )zÔ
        Construct a broker for a synchronous arragenment w/ both ends.

        Args:
            browser: The sync browser implementation.
            channel: The channel the browser uses to talk on.

        N)r   r   )Úselfr   r   © r   ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/choreographer/_brokers/_sync.pyÚ__init__    s    	zBrokerSync.__init__r   )Úkwargsr   c                   s0   ddœ‡ ‡fdd„}t  d¡ t|d� ¡  dS )z¿
        Run a thread which dumps all browser messages. kwargs is passed to print.

        Raises:
            ChannelClosedError: When the channel is closed, this error is raised.

        r   ©r   c                    sT   z0ˆj  ¡ } | D ]}ttj|dd�fˆ Ž qqW n tk
rN   tdˆ Ž Y nX d S )Né   )ÚindentúChannelClosedError caught.)r   )r   Z
read_jsonsÚprintÚjsonÚdumpsr   )Ú	responsesÚresponse©r   r   r   r   Ú	run_print5   s    
z/BrokerSync.run_output_thread.<locals>.run_printz)Starting thread to dump output to stdout.)ÚtargetN)Ú_loggerÚinfor   Ústart)r   r   r    r   r   r   Úrun_output_thread,   s    		
zBrokerSync.run_output_threadzprotocol.BrowserCommandzprotocol.MessageKey | None)Úobjr   c                 C  s$   t  |¡ t  |¡}| j |¡ |S )z’
        Send an object down the channel.

        Args:
            obj: An object to be serialized to json and written to the channel.

        )r   Zverify_paramsZcalculate_message_keyr   Ú
write_json)r   r&   Úkeyr   r   r   r'   A   s    

zBrokerSync.write_jsonr   c                 C  s   d S )Nr   )r   r   r   r   ÚcleanN   s    zBrokerSync.cleanN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r%   r'   r)   r   r   r   r   r
      s   
r
   )Ú
__future__r   r   Ú	threadingr   Útypingr   ZlogistroZchoreographerr   Zchoreographer.channelsr   r   Zchoreographer.browser_syncr   Z&choreographer.channels._interface_typer	   Ú	getLoggerr*   r"   r
   r   r   r   r   Ú<module>   s   
