o
    Vh!                     @   s   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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mZ d d
lmZ d dlmZ d dlmZ eeZG dd deZdS )    N)ABC)abstractmethod)IOBase)system)PIPE)sleep)request)URLError)WebDriverException)SubprocessStdAlias)utilsc                   @   s   e Zd ZdZ				d$dedededejej	ej
ej
f  ddf
d	d
ZedefddZedeje fddZedefddZejdeddfddZd%ddZd%ddZdefddZd%ddZd%ddZd%ddZd%dd Zd!eddfd"d#ZdS )&Servicea(  The abstract base class for all service objects.  Services typically
    launch a child program in a new process as an interim process to
    communicate with a browser.

    :param executable: install path of the executable.
    :param port: Port for the service to run on, defaults to 0 where the operating system will decide.
    :param log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.
    :param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
    Nr   executable_pathport
log_outputenvreturnc                 K   s   t |trt|ddd| _n|tjkrd | _n|d u s |tjkr%tj| _n|| _|| _|p0t	 | _
|di | _| jdd| _|pEtj| _d S )Nza+zutf-8)encodingpopen_kwcreation_flagsr   )
isinstancestropenr   
subprocessSTDOUTDEVNULL_pathr   	free_portr   popr   r   osenvironr   )selfr   r   r   r   kwargs r#   ^/home/air/yokohama/back/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py__init__1   s   


zService.__init__c                 C   s   dt d| j S )zGets the url of the Service.zhttp://	localhost)r   join_host_portr   r!   r#   r#   r$   service_urlI   s   zService.service_urlc                 C   s   t d)z7A List of program arguments (excluding the executable).z2This method needs to be implemented in a sub class)NotImplementedErrorr(   r#   r#   r$   command_line_argsN   s   zService.command_line_argsc                 C   s   | j S N)r   r(   r#   r#   r$   pathS   s   zService.pathvaluec                 C   s   t || _d S r,   )r   r   )r!   r.   r#   r#   r$   r-   W   s   c                 C   s^   |  | j d}	 |   |  rd	S ttdd|  d |d7 }|dkr.td| j q	)
zStarts the Service.

        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        r   Tg{Gz?g?g      ?   F   zCan not connect to the Service N)_start_processr   assert_process_still_runningis_connectabler   minr
   )r!   countr#   r#   r$   start[   s   zService.startc                 C   s(   | j  }|rtd| j d| dS )z1Check if the underlying process is still running.zService z' unexpectedly exited. Status code was: N)processpollr
   r   )r!   return_coder#   r#   r$   r2   o   s   
z$Service.assert_process_still_runningc                 C   s   t | jS )zfEstablishes a socket connection to determine if the service running
        on the port is accessible.)r   r3   r   r(   r#   r#   r$   r3   u   s   zService.is_connectablec                 C   sT   zt | j d W n
 ty   Y dS w tdD ]}|  s# dS td qdS )zcDispatch an HTTP request to the shutdown endpoint for the service in
        an attempt to stop it.z	/shutdownN   r/   )r   urlopenr)   r	   ranger3   r   )r!   _r#   r#   r$   send_remote_shutdown_commandz   s   
z$Service.send_remote_shutdown_commandc                 C   sz   | j ttjhvr t| j tr| j   nt| j tr t| j  | j	dur;z| 
  W n	 ty4   Y nw |   dS dS )zStops the service.N)r   r   r   r   r   r   closeintr   r7   r>   	TypeError_terminate_processr(   r#   r#   r$   stop   s   
zService.stopc              	   C   s   zM| j j| j j| j j}}}|||fD ]}z|  W q ty%   Y qw | j   z
| j d W W dS  tj	yM   t
jddd | j   Y W dS w  ty^   t
jddd Y dS w )a#  Terminate the child process.

        On POSIX this attempts a graceful SIGTERM followed by a SIGKILL,
        on a Windows OS kill is an alias to terminate.  Terminating does
        not raise itself if something has gone wrong but (currently)
        silently ignores errors here.
        <   zTService process refused to terminate gracefully with SIGTERM, escalating to SIGKILL.T)exc_infoz"Error terminating service process.N)r7   stdinstdoutstderrr?   AttributeError	terminatewaitr   TimeoutExpiredloggererrorkillOSError)r!   rF   rG   rH   streamr#   r#   r$   rB      s0   

zService._terminate_processc                 C   s$   z|    W d S  ty   Y d S w r,   )rC   	Exceptionr(   r#   r#   r$   __del__   s
   zService.__del__r-   c              
   C   s   |g}| |   | jdt dk}z>d}t dkr+t }tjtjB |_	tj
|_tj|f| j|| j| jt| j|d| j| _td| j| jj| j| j W dS  tyZ     tyz } z|jtjkrutdtj| j d| d}~ww )zmCreates a subprocess by executing the command provided.

        :param cmd: full command to execute
        	close_fdsWindowsN)r   rT   rG   rH   rF   creationflagsstartupinfozNStarted executable: `%s` in a child process with pid: %s using %s to output %s'z(' executable may have wrong permissions.)extendr+   r   r   r   r   STARTUPINFOCREATE_NEW_CONSOLESTARTF_USESHOWWINDOWdwFlagsSW_HIDEwShowWindowPopenr   r   r   r   r7   rM   debugr   pidrA   rP   errnoEACCESr
   r   r-   basename)r!   r-   cmdclose_file_descriptors
start_infoerrr#   r#   r$   r1      sR   
	
zService._start_process)Nr   NN)r   N)__name__
__module____qualname____doc__r   r@   r   typingOptionalMappingAnyr%   propertyr)   r   Listr+   r-   setterr6   r2   boolr3   r>   rC   rB   rS   r1   r#   r#   r#   r$   r   &   sB    






r   )rc   loggingr   r   rn   abcr   r   ior   platformr   r   timer   urllibr   urllib.errorr	   selenium.common.exceptionsr
   selenium.typesr   selenium.webdriver.commonr   	getLoggerrj   rM   r   r#   r#   r#   r$   <module>   s$   
