U
    g                     @   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mZ d dl	m
Z
 d dl	mZ d dlmZ eeZG dd dZdS )    N)Path)List)Optional)WebDriverExceptionc                   @   s`   e Zd ZdZeedddZeedddZ	eee
 eddd	Zeee d
ddZdS )SeleniumManagerzWrapper for getting information from the Selenium Manager binaries.

    This implementation is still in beta, and may change.
    )argsreturnc                 C   s\   t |  g| }t tjkr*|d |d |d |d |d | |S )zDetermines the locations of the requested assets.

        :Args:
         - args: the commands to send to the selenium manager binary.
        :Returns: dictionary of assets and their path
        z--debugz--language-bindingpythonz--outputjson)str_get_binaryloggergetEffectiveLevelloggingDEBUGappend_run)selfr    r   N/tmp/pip-unpacked-wheel-3ysq0mf8/selenium/webdriver/common/selenium_manager.pybinary_paths'   s    




zSeleniumManager.binary_paths)r   c                  C   s  t tjd} td}|dk	r,| |} d}td }dk	rXt	
d| t |}n|  rf| }n~ddddddd	}tjd
krt nd}tjdkrt	dtj |tj|f}|dkrtdtj d| t tj|}|dks| std| t	
d| |S )zDetermines the path of the correct Selenium Manager binary.

        :Returns: The Selenium Manager executable location

        :Raises: WebDriverException if the platform is unsupported
        zselenium-managerEXENZSE_MANAGER_PATHz2Selenium Manager set by env SE_MANAGER_PATH to: %szmacos/selenium-managerzwindows/selenium-manager.exezlinux/selenium-manager))darwinany)win32r   )cygwinr   )linuxx86_64)freebsdr   )openbsdr   )r   r   r   r   )r   r   zFSelenium Manager binary may not be compatible with %s; verify settingsz/Unsupported platform/architecture combination: /z2Unable to obtain working Selenium Manager binary; z$Selenium Manager binary found at: %s)r   __file__parentjoinpath	sysconfigget_config_varwith_suffixosgetenvr   debugexistssysplatformmachinewarninggetr   is_file)Zcompiled_pathZexepathZenv_pathallowedarchlocationr   r   r   r   9   s8    	


	
zSeleniumManager._get_binaryc              
   C   s   d | }td| zptjdkr6tj| dtjd}ntj| dd}|j	d
d}|j	d
d}|d	krzt|ng i d
}W n2 tk
r } ztd| |W 5 d}~X Y nX t|d  |d }|jrtd| d|j d| d| |S )zExecutes the Selenium Manager Binary.

        :Args:
         - args: the components of the command being executed.
        :Returns: The log string containing the driver location.
         zExecuting process: %sr   T)capture_outputcreationflags)r6   zutf-8
 )logsresultzUnsuccessful command executed: Nr:   r;   z; code: )joinr   r)   r+   r,   
subprocessrunZCREATE_NO_WINDOWstdoutdecoderstripstderrr
   loads	Exceptionr   r   _process_logs
returncode)r   commandZcompleted_procr?   rB   outputerrr;   r   r   r   r   i   s$    

 "zSeleniumManager._run)	log_itemsc                 C   sD   | D ]:}|d dkr$t |d  q|d dkrt |d  qd S )NlevelWARNmessage)r   INFO)r   r.   r)   )rJ   itemr   r   r   rE      s
    zSeleniumManager._process_logsN)__name__
__module____qualname____doc__r   dictr   staticmethodr   r   r   r   rE   r   r   r   r   r   !   s   /r   )r
   r   r'   r,   r=   r+   r$   pathlibr   typingr   r   Zselenium.commonr   	getLoggerrP   r   r   r   r   r   r   <module>   s   
