
    >fh?%                         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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mZmZmZ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j>                  e       Z! G d de      Z"y)    N)ABCabstractmethod)Mapping)IOBase)PIPE)sleep)IOAnyOptionalUnioncast)request)URLError)WebDriverException)SubprocessStdAlias)utilsc                   2   e Zd ZdZ	 	 	 	 	 ddee   dedee   deee	e	f      dee   ddfd	Z
edefd
       Zedee   fd       Zedefd       Zej"                  deddfd       ZddZddZdefdZddZddZddZddZdeddfdZdee   fdZy)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`.
    :param driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.
    Nexecutable_pathport
log_outputenvdriver_path_env_keyreturnc                 X   |  t        |t              r"t        t        t	        |dd            | _        nh|t        j                  k(  rd | _        nM||t        j                  k(  rt        j                  | _        n"t        t        t        t        f   |      | _        |xs t        j                         | _        |j                  di       | _        | j                  j                  dd      | _        |xs t"        j$                  | _        || _        | j+                         xs || _        y )Nza+zutf-8)encodingpopen_kwcreation_flagsr   )
isinstancestrr   r   openr   
subprocessSTDOUTDEVNULLr   intr   	free_portr   popr   r   osenvironr   DRIVER_PATH_ENV_KEYenv_path_path)selfr   r   r   r   r   kwargss          T/var/www/zara/venv/lib/python3.12/site-packages/selenium/webdriver/common/service.py__init__zService.__init__3   s     	j#&"64
D7+STDO:,,,"DO:1C1C#C(00DO"5f#5zBDO-EOO-	

:r2"mm//0@!D$"**#6 ]]_7
    c                 H    dt        j                  d| j                         S )zGets the url of the Service.zhttp://	localhost)r   join_host_portr   r-   s    r/   service_urlzService.service_urlN   s#     --k499EFGGr1   c                     t        d      )z7A List of program arguments (excluding the executable).z2This method needs to be implemented in a sub class)NotImplementedErrorr5   s    r/   command_line_argszService.command_line_argsS   s     ""VWWr1   c                 "    | j                   xs dS )N )r,   r5   s    r/   pathzService.pathX   s    zzRr1   valuec                 $    t        |      | _        y N)r    r,   )r-   r=   s     r/   r<   zService.path\   s    Z
r1   c                 *   | j                   t        d      | j                  | j                          d}	 | j                          | j	                         ryt        t        dd|z  z   d             |dz  }|dk(  rt        d	| j                          _)
zStarts the Service.

        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        NService path cannot be None.r   g{Gz?g?g      ?   F   zCan not connect to the Service )r,   r   _start_processassert_process_still_runningis_connectabler   min)r-   counts     r/   startzService.start`   s     ::$%CDDDJJ'--/""$#dTE\)3/0QJE{(+J4::,)WXX r1   c                 r    | j                   j                         }|rt        d| j                   d|       y)z1Check if the underlying process is still running.zService z' unexpectedly exited. Status code was: N)processpollr   r,   )r-   return_codes     r/   rE   z$Service.assert_process_still_runningv   s;    ll'')$x

|;bcnbo%pqq r1   c                 @    t        j                  | j                        S )zfEstablishes a socket connection to determine if the service running
        on the port is accessible.)r   rF   r   r5   s    r/   rF   zService.is_connectable|   s     ##DII..r1   c                     	 t        j                  | j                   d       t	        d      D ]  }| j                         s yt        d       ! y# t        $ r Y yw xY w)zcDispatch an HTTP request to the shutdown endpoint for the service in
        an attempt to stop it.z	/shutdownN   rB   )r   urlopenr6   r   rangerF   r   )r-   _s     r/   send_remote_shutdown_commandz$Service.send_remote_shutdown_command   s`    	OOt//0	:; r 	A&&(!H	  		s   "A 	AAc                    | j                   t        t        j                  hvrnt	        | j                   t
              r| j                   j                          n9t	        | j                   t              rt        j                  | j                          | j                  =| j                  j                         "	 | j                          | j                          yyy# t        $ r Y w xY w# | j                          w xY w)zStops the service.N)r   r   r"   r$   r   r   closer%   r(   rK   rL   rT   	TypeError_terminate_processr5   s    r/   stopzService.stop   s     ??4););"<<$//62%%'DOOS1)<<#(9(9(;(C*113 '') )D#   '')s$   8C 	C'$C* &C''C* *C<c                 *   	 | j                   j                  | j                   j                  | j                   j                  }}}|||fD ]  }	 |j	                           | j                   j                          	 | j                   j                  d       y# t
        $ r Y Xw xY w# t        j                  $ r4 t        j                  dd       | j                   j                          Y yw xY w# t        $ r t        j                  dd       Y yw xY 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)rK   stdinstdoutstderrrV   AttributeError	terminatewaitr"   TimeoutExpiredloggererrorkillOSError)r-   r]   r^   r_   streams        r/   rX   zService._terminate_process   s    	N""#### "6E
  / LLN
 LL""$$!!"%	 & 
 ,, $j!   !!#$  	NLL=LM	NsU   A
C/ BC/ :B% 	B"C/ !B""C/ %AC,)C/ +C,,C/ / DDc                 D    	 | j                          y # t        $ r Y y w xY wr?   )rY   	Exceptionr5   s    r/   __del__zService.__del__   s"    	IIK 		s    	r<   c                 d   |g}|j                  | j                                | j                  j                  dt        j
                  dk7        }	 d}t        j
                  dk(  rOt        j                         }t        j                  t        j                  z  |_
        t        j                  |_        t        j                  |f| j                  |t        t         t"        t$        t&        t(           f      | j*                        t        t         t"        t$        t&        t(           f      | j*                        t,        | j.                  |d| j                  | _        t2        j5                  d| j6                  | j0                  j8                  | j.                  | j*                         y# t:        $ r  t<        $ rq}|j>                  t>        j@                  k(  rN| j6                  tC        d      tC        dtD        jF                  jI                  | j6                         d      | d}~ww xY w)	zmCreates a subprocess by executing the command provided.

        :param cmd: full command to execute
        	close_fdswin32N)r   rm   r^   r_   r]   creationflagsstartupinfozNStarted executable: `%s` in a child process with pid: %s using %s to output %srA   'z(' executable may have wrong permissions.)%extendr9   r   r'   sysplatformr"   STARTUPINFOCREATE_NEW_CONSOLESTARTF_USESHOWWINDOWdwFlagsSW_HIDEwShowWindowPopenr   r   r   r   r%   r	   r
   r   r   r   rK   rd   debugr,   pidrW   rg   errnoEACCESr   r(   r<   basename)r-   r<   cmdclose_file_descriptors
start_infoerrs         r/   rD   zService._start_process   s   
 f

4))+,!%!2!2;PW@W!X"	J||w&'335
%/%B%BZEdEd%d
")3););
&%++
HH0HU33<%894??KHU33<%894??K"11&
 --
DL LL`

  ##  	 	yyELL(::%,-KLL(((455]^ 	s   EF, ,H/>A,H**H/c                 \    | j                   r t        j                  | j                   d       S y r?   )r*   r(   getenvr5   s    r/   r+   zService.env_path   s%    ##99T55t<<r1   )Nr   NNN)r   N)__name__
__module____qualname____doc__r   r    r%   r   r   r
   r0   propertyr6   r   listr9   r<   setterrI   rE   boolrF   rT   rY   rX   rk   rD   r+    r1   r/   r   r   '   sE   	 *.37+/-18!#8 8 /0	8
 gc3h'(8 &c]8 
86 HS H H X49 X X  c     
[[ #  $    Y,r/ /
*"N>	*3 *4 *X(3- r1   r   )#r~   loggingr(   r"   rs   abcr   r   collections.abcr   ior   r   timer   typingr	   r
   r   r   r   urllibr   urllib.errorr   selenium.common.exceptionsr   selenium.typesr   selenium.webdriver.commonr   	getLoggerr   rd   r   r   r1   r/   <module>r      sZ   $   	  
 # #    1 1  ! 9 - +			8	$Qc Qr1   