U
    b                     @   sv   d dl m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lmZ d dl	m
Z
 d dlmZ dZG d	d
 d
ZdS )    )DEVNULLN)system)PIPE)sleep)WebDriverException)utilsTc                   @   sb   e Zd ZdeddfddZedd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )Servicer   N c                 C   sZ   || _ || _| jdkr t | _ts8|tkr8ttjd}|| _	|| _
d| _|pRtj| _d S )Nr   wb)pathportr   Z	free_port_HAS_NATIVE_DEVNULLr   openosdevnullstart_error_messagelog_filecreationflagsenvironenv)self
executabler   r   r   r    r   E/tmp/pip-unpacked-wheel-3sqd7awy/selenium/webdriver/common/service.py__init__"   s    

zService.__init__c                 C   s   dt d| j S )z-
        Gets the url of the Service
        z	http://%s	localhost)r   Zjoin_host_portr   r   r   r   r   service_url2   s    zService.service_urlc                 C   s   t dd S )Nz2This method needs to be implemented in a sub class)NotImplementedErrorr   r   r   r   command_line_args9   s    zService.command_line_argsc              
   C   s^  zB| j g}||   tj|| jt dk| j| jt| j	d| _
W n tk
rX    Y n tk
r } zZ|jtjkrtdtj | j | jn,|jtjkrtdtj | j | jn W 5 d}~X Y nF tk
r } z&tdtj | j | jt|f W 5 d}~X Y nX d}|   |  r.qZ|d7 }td	 |d
krtd| j  qdS )z
        Starts the Service.

        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        Windows)r   	close_fdsstdoutstderrstdinr   z''{}' executable needs to be in PATH. {}z.'{}' executable may have wrong permissions. {}Nz:The executable %s needs to be available in the path. %s
%sr      g      ?<   z!Can not connect to the Service %s)r   extendr   
subprocessPopenr   r   r   r   r   process	TypeErrorOSErrorerrnoENOENTr   formatr   basenamer   EACCES	Exceptionstrassert_process_still_runningis_connectabler   )r   cmderrecountr   r   r   start<   sT    
  

zService.startc                 C   s$   | j  }|r td| j|f d S )Nz3Service %s unexpectedly exited. Status code was: %s)r*   pollr   r   )r   Zreturn_coder   r   r   r4   k   s    
z$Service.assert_process_still_runningc                 C   s   t | jS N)r   r5   r   r   r   r   r   r5   s   s    zService.is_connectablec                 C   sl   ddl m} ddlm} z|d| j  W n |k
rB   Y d S X tdD ]}|  s^ qhqLtd qLd S )Nr   )request)URLErrorz%s/shutdown   r%   )	urllibr=   urllib.errorr>   urlopenr   ranger5   r   )r   r=   r>   xr   r   r   send_remote_shutdown_commandv   s    z$Service.send_remote_shutdown_commandc              	   C   s   | j tkr<| j tkrts<z| j   W n tk
r:   Y nX | jsFdS z|   W n tk
rf   Y nX zl| jr| jj	| jj
| jjfD ]&}z|  W q tk
r   Y qX q| j  | j  | j  d| _W n tk
r   Y nX dS )z$
        Stops the service.
        N)r   r   r   r   closer2   r*   rE   r+   r$   r"   r#   AttributeError	terminatewaitkillr,   )r   streamr   r   r   stop   s6    



zService.stopc                 C   s&   z|    W n tk
r    Y nX d S r<   )rL   r2   r   r   r   r   __del__   s    zService.__del__)__name__
__module____qualname__r   r   propertyr   r   r:   r4   r5   rE   rL   rM   r   r   r   r   r       s   
/"r   )r(   r   r-   r   platformr   r   timer   Zselenium.common.exceptionsr   Zselenium.webdriver.commonr   r   r   r   r   r   r   <module>   s   