
    g%                     :   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 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j         e!          Z" G d de          Z#dS )    N)ABC)abstractmethod)IOBase)system)PIPE)sleep)IO)Any)List)Mapping)Optional)Union)cast)request)URLError)WebDriverException)SubprocessStdAlias)utilsc                   H   e Zd ZdZ	 	 	 	 	 ddedededeee	e	f                  d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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`.
    :param driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.
    Nr   executable_pathport
log_outputenvdriver_path_env_keyreturnc                    t          |t                    r+t          t          t	          |dd                    | _        n|t          j        k    r8t          t          t          t          t          f                  d           | _        n[||t          j        k    rBt          t          t          t          t          f                  t          j                  | _        n|| _        |pt          j                    | _        |                    di           | _        | j                            dd          | _        |pt$          j        | _        || _        |                                 p|| _        d S )Nza+zutf-8)encodingpopen_kwcreation_flagsr   )
isinstancestrr   r   openr   
subprocessSTDOUTr   r   intDEVNULLr   	free_portr   popr   r    osenvironr   DRIVER_PATH_ENV_KEYenv_path_path)selfr   r   r   r   r   kwargss          m/var/www/html/nodeJS/PythonScripts/venv3.11/lib/python3.11/site-packages/selenium/webdriver/common/service.py__init__zService.__init__8   s    j#&& 	)"64
D7+S+S+STTDOO:,,,"8E#v+,>#?FFDOO:1C#C#C"8E#v+,>#?ASTTDOO(DO-EO--	

:r22"m//0@!DD$"*#6 ]]__7


    c                 <    dt          j        d| j                   S )zGets the url of the Service.zhttp://	localhost)r   join_host_portr   r/   s    r1   service_urlzService.service_urlR   s"     H-k49EEGGGr3   c                      t          d          )z7A List of program arguments (excluding the executable).z2This method needs to be implemented in a sub class)NotImplementedErrorr7   s    r1   command_line_argszService.command_line_argsW   s     ""VWWWr3   c                     | j         pdS )N )r.   r7   s    r1   pathzService.path\   s    zRr3   valuec                 .    t          |          | _        d S N)r"   r.   )r/   r?   s     r1   r>   zService.path`   s    ZZ


r3   c                 H   | j         t          d          |                     | j                    d}	 |                                  |                                 rdS t          t          dd|z  z   d                     |dz  }|d	k    rt          d
| j                    p)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   Tg{Gz?g?g      ?   F   zCan not connect to the Service )r.   r   _start_processassert_process_still_runningis_connectabler   min)r/   counts     r1   startzService.startd   s     :$%CDDDDJ'''	Y--///""$$ #dTE\)3//000QJE{{()W4:)W)WXXX	Yr3   c                 p    | 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_codes     r1   rG   z$Service.assert_process_still_runningz   sL    l'')) 	r$%p
%p%pcn%p%pqqq	r 	rr3   c                 4    t          j        | j                  S )zfEstablishes a socket connection to determine if the service running
        on the port is accessible.)r   rH   r   r7   s    r1   rH   zService.is_connectable   s     #DI...r3   c                     	 t          j        | j         d           n# t          $ r Y dS w xY wt	          d          D ](}|                                 s dS t          d           )dS )zcDispatch an HTTP request to the shutdown endpoint for the service in
        an attempt to stop it.z	/shutdownN   rD   )r   urlopenr8   r   rangerH   r   )r/   _s     r1   send_remote_shutdown_commandz$Service.send_remote_shutdown_command   s    	Ot/:::;;;; 	 	 	FF	 r 	 	A&&(( !HHHH	 	s    
--c                    | j         t          t          j        hvrgt	          | j         t
                    r| j                                          n3t	          | j         t                    rt          j        | j                    | j	        <	 | 
                                 n# t          $ r Y nw xY w|                                  dS dS )zStops the service.N)r   r   r$   r'   r!   r   closer&   r*   rM   rV   	TypeError_terminate_processr7   s    r1   stopzService.stop   s     ?4);"<<<$/622 *%%''''DOS11 *)))<#113333   ##%%%%% $#s   
B 
B,+B,c                    	 | j         j        | j         j        | j         j        }}}|||fD ]'}	 |                                 # t
          $ r Y $w xY w| j                                          	 | j                             d           dS # t          j	        $ r9 t                              dd           | j                                          Y dS w xY w# t          $ r  t                              dd           Y dS 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)rM   stdinstdoutstderrrX   AttributeError	terminatewaitr$   TimeoutExpiredloggererrorkillOSError)r/   r_   r`   ra   streams        r1   rZ   zService._terminate_process   sI   	N"## "6E
  /  LLNNNN%   DL""$$$$!!"%%%%%, $ $ $j!     !!######$  	N 	N 	NLL=LMMMMMM	NsS   +C AC 
AC AC -B	 	ACC CC &C>=C>c                 R    	 |                                   d S # t          $ r Y d S w xY wrA   )r[   	Exceptionr7   s    r1   __del__zService.__del__   s:    	IIKKKKK 	 	 	DD	s    
&&r>   c                 .   |g}|                     |                                            | j                            dt	                      dk              }	 d}t	                      dk    rBt          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        | _        t0                              d| j        | j        j        | j        | j                   dS # t8          $ r  t:          $ rg}|j        t<          j        k    rL| j        tA          d          tA          dtB          j"        #                    | j                   d          | d}~ww xY w)	zmCreates a subprocess by executing the command provided.

        :param cmd: full command to execute
        	close_fdsWindowsN)r   ro   r`   ra   r_   creationflagsstartupinfozNStarted executable: `%s` in a child process with pid: %s using %s to output %srC   '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   r&   r	   r
   r   r   r    rM   rf   debugr.   pidrY   ri   errnoEACCESr   r*   r>   basename)r/   r>   cmdclose_file_descriptors
start_infoerrs         r1   rF   zService._start_process   s   
 f

4))++,,,!%!2!2;I@U!V!V"	Jxx9$$'355
%/%BZEd%d
")3);
&%+
H0HU33<%894?KKHU33<%894?KK"1&
 
 -
 
DL LL`
 #      	 	 	 	 	 	yEL((:%,-KLLL(^((44^^^  	s   E F H-A"HHc                 H    | j         rt          j        | j         d           S d S rA   )r,   r*   getenvr7   s    r1   r-   zService.env_path   s'    # 	=9T5t<<<tr3   )Nr   NNN)r   N)__name__
__module____qualname____doc__r"   r&   r   r   r   r
   r2   propertyr8   r   r   r;   r>   setterrK   rG   boolrH   rV   r[   rZ   rm   rF   r-    r3   r1   r   r   ,   s$       	 	  $)-+/#'8 88 8 '	8
 gc3h'(8 !8 
8 8 8 84 HS H H H XH X49 X X X ^X  c       X  
[ #  $       [ Y Y Y Y,r r r r/ / / / /
   & & & & N N N N>	 	 	 	*3 *4 * * * *X(3-      r3   r   )$r~   loggingr*   r$   abcr   r   ior   platformr   r   timer   typingr	   r
   r   r   r   r   r   urllibr   urllib.errorr   selenium.common.exceptionsr   selenium.typesr   selenium.webdriver.commonr   	getLoggerr   rf   r   r   r3   r1   <module>r      s  "   				                                                                                         ! ! ! ! ! ! 9 9 9 9 9 9 - - - - - - + + + + + +		8	$	$O O O O Oc O O O O Or3   