o
    5@Hh                     @   s^   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	 G dd dZ
dS )    N)SeleniumManagerc                       sz   e Zd ZdZdddZdd Zd	d
 Z fddZ fddZdd Z	dd Z
dddZdddZdd Zdd Z  ZS )Servera  Manage a Selenium Grid (Remote) Server in standalone mode.

    This class contains functionality for downloading the server and starting/stopping it.

    For more information on Selenium Grid, see:
        - https://www.selenium.dev/documentation/grid/getting_started/

    Parameters:
    -----------
    host : str
        Hostname or IP address to bind to (determined automatically if not specified)
    port : int or str
        Port to listen on (4444 if not specified)
    path : str
        Path/filename of existing server .jar file (Selenium Manager is used if not specified)
    version : str
        Version of server to download (latest version if not specified)
    log_level : str
        Logging level to control logging output ("INFO" if not specified)
        Available levels: "SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST"
    env: collections.abc.Mapping
        Mapping that defines the environment variables for the server process
    N\  INFOc                 C   sf   |r|rt d|| _| || _| || _| || _| || _	| 
|| _d | _|  | _d S )NzCNot allowed to specify a version when using an existing server path)	TypeErrorhost_validate_portport_validate_pathpath_validate_versionversion_validate_log_level	log_level_validate_envenvprocess_get_status_url
status_url)selfr   r	   r   r   r   r    r   `/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/selenium/webdriver/remote/server.py__init__7   s   zServer.__init__c                 C   s(   | j d ur| j nd}d| d| j dS )N	localhostzhttp://:z/status)r   r	   )r   r   r   r   r   r   E   s   zServer._get_status_urlc                 C   s"   |rt j|std| |S )Nz"Can't find server .jar located at )osr   existsOSError)r   r   r   r   r   r
   I   s   zServer._validate_pathc                    sZ   zt |}W n ty   t j d| dw d|  kr&dks+td td|S )Nz".__init__() got an invalid port: ''r   i  zport must be 0-65535)int
ValueErrorr   __name__)r   r	   	__class__r   r   r   N   s   zServer._validate_portc                    s.   |rt dt|st j d| d|S )Nz^\d+\.\d+\.\d+$z%.__init__() got an invalid version: 'r   )rematchstrr   r!   )r   r   r"   r   r   r   W   s   zServer._validate_versionc                 C   s$   d}||vrt dd| |S )N)SEVEREWARNINGr   CONFIGFINEFINERFINESTzlog_level must be one of: z, )r   join)r   r   levelsr   r   r   r   ]   s   zServer._validate_log_levelc                 C   s"   |d urt |tjjstd|S )Nz.env must be a mapping of environment variables)
isinstancecollectionsabcMappingr   )r   r   r   r   r   r   c   s   zServer._validate_env
   c                 C   sb   t   }t   | |k r/z
tj| j W dS  tjjy&   t d Y nw t   | |k sdS )NTg?F)timeurllibrequesturlopenr   errorURLErrorsleep)r   timeoutstartr   r   r   _wait_for_serverh   s   zServer._wait_for_serverc                 C   s(   dg}|dur| | t |d S )zqDownload the server if it doesn't already exist.

        Latest version is downloaded unless specified.
        z--gridNdriver_path)appendr   binary_paths)r   r   argsr   r   r   download_if_neededr   s   
zServer.download_if_neededc                 C   s<  | j du r| | jn| j }td}|du rtd|d|ddt| jd| jdd	d
d	g}| j	dur:|
d| j	g | j	durB| j	nd}z(ttjtj}||| jf W d   n1 s`w   Y  td| j  ty   td tj|| jd| _td| jj  |  std| j td Y | jS w )zStart the server.

        Selenium Manager will detect the server location and download it if necessary,
        unless an existing server path was specified.
        NjavazJCan't find java on system PATH. JRE is required to run the Selenium serverz-jar
standalonez--portz--log-levelz--selenium-managertruez--enable-managed-downloadsz--hostr   zDSelenium server is already running, or something else is using port zStarting Selenium server...)r   z$Selenium server running as process: z)Timed out waiting for Selenium server at zSelenium server is ready)r   rB   r   shutilwhichr   r&   r	   r   r   extendsocketAF_INETSOCK_STREAMconnectConnectionErrorConnectionRefusedErrorprint
subprocessPopenr   r   pidr=   TimeoutErrorr   )r   r   	java_pathcommandr   sockr   r   r   r<   |   sD   


zServer.startc                 C   sF   | j du r	td| j  du r| j   | j   d| _ td dS )zStop the server.NzSelenium server isn't runningz#Selenium server has been terminated)r   RuntimeErrorpoll	terminatewaitrO   )r   r   r   r   stop   s   


zServer.stop)Nr   NNr   N)r3   )N)r!   
__module____qualname____doc__r   r   r
   r   r   r   r   r=   rB   r<   r[   __classcell__r   r   r"   r   r      s    
	



,r   )r0   r   r$   rF   rI   rP   r4   r5   *selenium.webdriver.common.selenium_managerr   r   r   r   r   r   <module>   s   