B
    @\                 @   s   d dl mZmZmZmZ d dlZeeZd dl	m
Z
 d dlmZmZ d dlmZ d dlmZ dZdd	d
ZdddZdd ZdddZG dd deZdS )    )absolute_importdivisionprint_functionunicode_literalsN)IOLoop)AsyncHTTPClientHTTPRequest)websocket_connect)Server)http_getManagedServerLoopurlwebsocket_openws_url c             C   s   dt | j | d S )Nzhttp://localhost:/)strport)serverprefix r   7lib/python3.7/site-packages/bokeh/server/tests/utils.pyr   2   s    r   c             C   s   dt | j | d S )Nzws://localhost:z/ws)r   r   )r   r   r   r   r   r   5   s    r   c                st   i  fdd} t  ks"tt }t }|j|||d    dkrVtdd }|jrl|jn|S d S )Nc                s   | d<     d S )Nresponse)stop)r   )io_loopresultr   r   handle_request:   s    z http_get.<locals>.handle_request)headersr   zFailed to http get)	r   ZcurrentAssertionErrorr   dictZfetchstartRuntimeErrorerror)r   r   r   Zhttp_clientr   r   r   )r   r   r   r   8   s    r   c                s~   i  fdd}t |}|d k	r,||jd< t||d    dkrPtdd }| rj| n|   d S d S )Nc                s   | d<     d S )N
connection)r   )future)r   r   r   r   handle_connectionO   s    z)websocket_open.<locals>.handle_connectionZOrigin)callbackr#   z"Failed to handle websocket connect)r   r   r	   r    r!   Z	exceptionr   close)r   r   originr%   Zrequestr$   r   )r   r   r   r   M   s    

r   c               @   s0   e Zd Zdd Zdd Zdd Zedd Zd	S )
r   c             K   s(   t  }|  ||d< t|f|| _d S )Nr   )r   Zmake_currentr
   _server)selfZapplicationZserver_kwargsZloopr   r   r   __init__f   s    zManagedServerLoop.__init__c             C   s$   | j   | j   | j j  d S )N)r)   Zunlistenr   r   r'   )r*   typevalue	tracebackr   r   r   __exit__k   s    

zManagedServerLoop.__exit__c             C   s   | j   | j S )N)r)   r    )r*   r   r   r   	__enter__o   s    
zManagedServerLoop.__enter__c             C   s   | j jS )N)Zs_serverr   )r*   r   r   r   r   r   s    zManagedServerLoop.io_loopN)__name__
__module____qualname__r+   r/   r0   propertyr   r   r   r   r   r   e   s   r   )r   )r   )N)Z
__future__r   r   r   r   ZloggingZ	getLoggerr1   logZtornado.ioloopr   Ztornado.httpclientr   r   Ztornado.websocketr	   Zbokeh.server.serverr
   __all__r   r   r   r   objectr   r   r   r   r   <module>   s   
	


