B
    `                 @   s   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ dd Zdd
dZG dd deZdS )zE
This module contains some helpers to deal with the real http
world.
    N)http_client)TcpWSGIServerc              C   sD   t  t jt j} | d |  \}}|   tjdd}||fS )N) r   ZWEBTEST_SERVER_BINDz	127.0.0.1)	socketAF_INETSOCK_STREAMbindgetsocknamecloseosenvironget)sipport r   T/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/webtest/http.pyget_free_port   s    
r   /      c          
   C   s   |dk rdS t d xft|D ]Z}y.tj| t||d}|d| | }|jS  t	j
tjfk
rx   t d Y q X q W dS )z(Perform a request until the server replyr   g333333?)timeoutGET)timesleepranger   HTTPConnectionintrequestgetresponsestatusr   errorHTTPException)hostr   Z	path_infor   retriesiconnresr   r   r   check_server   s    
r(   c                   sR   e Zd ZdZdZ fddZdd Zdd Zd	d
 Ze	dd Z
dddZ  ZS )StopableWSGIServerzStopableWSGIServer is a TcpWSGIServer which run in a separated thread.
    This allow to use tools like casperjs or selenium.

    Server instance have an ``application_url`` attribute formated with the
    server host and port.
    Fc                s@   t t| j| jf|| d | _|| _d| jj| jjf | _	d S )Nzhttp://%s:%s/)
superr)   __init__wrapperrunnertest_appadjr#   r   Zapplication_url)selfapplicationargskwargs)	__class__r   r   r+   7   s    zStopableWSGIServer.__init__c             C   s   d|d krt |}t  }d|_|jd}tj|rpt	|d
 }|tdtd|j }||_nd|_|||S d|d krt d	||S | ||S )
zWrap the wsgi application to override some path:

        ``/__application__``: allow to ping the server.

        ``/__file__?__file__={path}``: serve the file found at ``path``
        __file__Z	PATH_INFOztext/html; charset=UTF-8rbzhttp://localhost/z
http://%s/z404 Not FoundZ__application__zserver started)webobRequestResponsecontent_typeparamsr   r   pathisfileopenreadreplacesixbr#   bodyr    r.   )r0   r   Zstart_responsereqrespfilenamerC   r   r   r   r,   =   s    

zStopableWSGIServer.wrapperc             C   s:   y| j jd| jd W n tjk
r4   | js0 Y nX dS )zRun the serverg      ?)mapN)Zasyncoreloop_mapselectr!   was_shutdown)r0   r   r   r   runU   s
    zStopableWSGIServer.runc             C   s\   d| _ | jtj x.| jrBt| j }x|D ]}|  q0W qW | 	d | j
  dS )zShutdown the serverTr   )rK   loggersetLevelloggingFATALrI   listvaluesZhandle_closeZmaintenanceZtask_dispatchershutdown)r0   triggerstriggerr   r   r   rS   ]   s    


zStopableWSGIServer.shutdownc             K   sl   t  \}}d|kr||d< d|kr*||d< d|kr:d|d< | |f|}tj|jd|_d|j_|j  |S )zJStart a server to serve ``application``. Return a server
        instance.r   r#   Zexpose_tracebacksT)target)r   	threadingThreadrL   r-   daemonstart)clsr1   r3   r#   r   serverr   r   r   createj   s    

zStopableWSGIServer.creater   c             C   s2   t | jj| jjd|d}|r dS z|   W ddS )z Wait until the server is startedz/__application__)r$   TNF)r(   r/   r#   r   rS   )r0   r$   runningr   r   r   wait{   s    
zStopableWSGIServer.wait)r   )__name__
__module____qualname____doc__rK   r+   r,   rL   rS   classmethodr]   r_   __classcell__r   r   )r4   r   r)   -   s   r)   )r   r   r   )rc   rW   rO   rJ   r   r   r   rA   r7   Z	six.movesr   Zwaitress.serverr   r   r(   r)   r   r   r   r   <module>   s   	
