a
    7ñ»jê(  ã                   @   s&  d 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 d5dd„Zd	d
„ Zdd„ Zd6dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd7d%d&„Zd'd(„ Z d)d*„ Z!d+d,„ Z"d-d.„ Z#d/d0„ Z$d1d2„ Z%d3d4„ Z&dS )8zShared utility methodsé    N)Úsuppress)Úconfig)Úsettings)Ú	constantsc              	   C   sÒ   dd l }dd l}| tjj¡}d}|rJdt|ƒ ¡ v sFdt|ƒ ¡ v rJd}|�p |sl| t	j
ddd| g¡ nD|s˜| d	 t|ƒ }| t	j
ddd|g¡ n| t	j
dddd
| g¡ W d   ƒ n1 sÄ0    Y  d S )Nr   FÚUÚLTz-mÚpipÚinstallz==z-U)Ú	fastenersÚ
subprocessZInterProcessLockr   Z
PipInstallZLOCKFILEÚstrÚupperÚ
check_callÚsysÚ
executable)ÚpackageÚversionr
   r   Zpip_install_lockZupgrade_to_latestZpackage_and_version© r   úHE:\soccer\ml_env\Lib\site-packages\seleniumbase/fixtures/shared_utils.pyÚpip_install   s4    ÿÿþþÿÿÿr   c                 C   s   dd„ |   d¡D ƒS )Nc                 S   s   g | ]}|  ¡ rt|ƒ‘qS r   )ÚisdigitÚint)Ú.0Úir   r   r   Ú
<listcomp>-   ó    z%make_version_list.<locals>.<listcomp>Ú.)Úsplit©Úversion_strr   r   r   Úmake_version_list,   s    r    c                 C   s   t t| ƒƒS ©N)Útupler    r   r   r   r   Úmake_version_tuple0   s    r#   c                 C   s”   ddl }| stj} t ¡ d }t|ƒt|ƒ }t|ƒdkr~tdƒD ]:}t d¡ t ¡ d }t|ƒt|ƒ }t|ƒdksB q~qB| | ¡}t	| 
¡ ƒS )a   Returns a time-based one-time password based on the
    Google Authenticator algorithm for multi-factor authentication.
    If the "totp_key" is not specified, this method defaults
    to using the one provided in [seleniumbase/config/settings.py].
    Google Authenticator codes expire & change at 30-sec intervals.
    If the fetched password expires in the next 1.2 seconds, waits
    for a new one before returning it (may take up to 1.2 seconds).
    See https://pyotp.readthedocs.io/en/latest/ for details.r   Ng      >@g¸…ëQ¸î?é   g{®Gáz¤?)Úpyotpr   ZTOTP_KEYÚtimeÚfloatr   ÚrangeÚsleepZTOTPr   Únow)Ztotp_keyr%   Zepoch_intervalZcycle_lifespanr   Ztotpr   r   r   Úget_mfa_code4   s    	

r+   c                   C   s   t  ¡ dkot  ¡ dkS )z Returns True if machine is ARM Linux.
    This will be useful once Google adds
    support for ARM Linux ChromeDriver.
    (Raspberry Pi uses ARM architecture.)ÚLinuxÚaarch64)ÚplatformÚsystemÚmachiner   r   r   r   Úis_arm_linuxP   s    
þr1   c                   C   s*   dt jv o(dt ¡  ¡ v p(dt ¡  ¡ v S )zMReturns True if machine is ARM Mac.
    (Eg. M1 / M2 Macs use ARM processors)ÚdarwinÚarmÚarm64)r   r.   Ú	processorÚlowerr   r   r   r   r   Ú
is_arm_mac[   s    
ür7   c                   C   s
   dt jv S )Nr2   ©r   r.   r   r   r   r   Úis_macg   s    r9   c                   C   s
   dt jv S )NÚlinuxr8   r   r   r   r   Úis_linuxk   s    r;   c                   C   s
   dt jv S )NÚwin32r8   r   r   r   r   Ú
is_windowso   s    r=   c                 C   s   | j d  ¡ dkS )NZbrowserNameZsafari)Zcapabilitiesr6   ©Zdriverr   r   r   Ú	is_safaris   s    r?   c                  C   sV   d} zt  ¡ j} W n> tyP   zdd l}| d¡j} W n tyJ   Y n0 Y n0 | S )NéP   r   )r@   é   )ÚosÚget_terminal_sizeÚcolumnsÚ	ExceptionÚshutil)ÚwidthrF   r   r   r   Úget_terminal_widthw   s    rH   c                   C   s   t ƒ rt ¡  d S r!   )r=   ÚcoloramaZjust_fix_windows_consoler   r   r   r   Úfix_colorama_if_windows…   s    rJ   c                 C   s2   | s
d} n$|   d¡rd|  } nd| vr.d|  } | S )Nzdata:,z//zhttps:ú:zhttps://)Ú
startswith)Úurlr   r   r   Úfix_url_as_neededŠ   s    

rN   c                 C   sR   t | dd ƒrNt| dƒrN|  ¡ sNttƒ� |  ¡  W d   ƒ n1 sD0    Y  d S )NZ_is_using_ucÚis_connected)ÚgetattrÚhasattrrO   r   rE   Úconnectr>   r   r   r   Úreconnect_if_disconnected”   s    
ÿþý
rS   c                 C   s&   t | dƒo$|  ¡ o$t | dƒo$|  ¡  S )aS  
    When someone is using CDP Mode with a disconnected webdriver,
    but they forget to reconnect before calling a webdriver method,
    this method is used to substitute the webdriver method for a
    CDP Mode method instead, which keeps CDP Stealth Mode enabled.
    For other webdriver methods, SeleniumBase will reconnect first.
    Úis_cdp_mode_activerO   )rQ   rT   rO   r>   r   r   r   Úis_cdp_swap_neededž   s    	
ÿþürU   c                 C   sˆ   ddl m} ttƒ�` |s&| d¡}n
| |¡}|rft|ƒdkrft| d¡d ƒdkrfW d   ƒ dS W d   ƒ n1 sz0    Y  dS )	Nr   )Údetect_b_verzgoogle-chromeé   r   é‚   TF)	Zseleniumbase.corerV   r   rE   Zget_browser_version_from_osZget_browser_version_from_binaryÚlenr   r   )ÚselfZbinary_locationrV   Úverr   r   r   Úis_chrome_130_or_newer®   s    
ÿ&.r\   c              	   C   sr   t  | ¡ d¡D ]\}| ¡ rt |¡j}||d@ d? O }ttƒ� t 	||¡ W d   ƒ q1 sb0    Y  qd S )NÚ*é$  é   )
ÚpathlibÚPathÚglobÚis_filerB   ÚstatÚst_moder   rE   Úchmod)Zdir_pathÚ	file_pathÚmoder   r   r   Úmake_dir_files_writable¿   s    
ri   c                 C   s,   t  | ¡j}||d@ d? O }t  | |¡ d S )Nr^   r_   ©rB   rd   re   rf   ©rg   rh   r   r   r   Úmake_writableÉ   s    rl   c                 C   s,   t  | ¡j}||d@ d? O }t  | |¡ d S )Nr^   é   rj   rk   r   r   r   Úmake_executableÐ   s    rn   c                 C   sf  ddl m} ddl m} ddl m} ddl m} ddl m} ddl m} ddlm} dd	lm	}	 dd
lm
}
 ddlm} ddlm} | tu r˜t}||fS | |u rª|j}�nŒ| dkr¼|j}�nz| |u rÎ|j}�nh| dkrà|j}�nV| |u rò|j}�nD| dk�r|j}�n0| |u �r|j}�n| dk�r.|j}�n| |u �r@|j}nö| dk�rR|j}nä| |u �rd|j}nÒ| dk�rv|j}nÀ| |u �rˆ|j}n®| dk�rš|j}nœ| |u �r¬|j}nŠ| dk�r¾|j}nx| |	u �rÐ|j	}nf| dk�râ|j	}nT| |
u �rô|j
}nB| dk�r|j
}n0t| tƒ�r*t}d| |f }||fS t}||fS t|ƒ}z
||_W n t�y\   Y n0 ||fS )z8Formats an exception message to make the output cleaner.r   )ÚElementNotVisibleException)ÚNoAlertPresentException)ÚNoSuchAttributeException)ÚNoSuchElementException)ÚNoSuchFrameException)ÚNoSuchWindowException)ÚLinkTextNotFoundException)ÚNoSuchFileException)ÚNoSuchOptionException)ÚTextNotVisibleException)Ú
exceptionsro   ru   rr   rx   rp   rq   rs   rt   rv   rw   z%s: %s)Zselenium.common.exceptionsro   rp   rq   rr   rs   rt   Úseleniumbase.common.exceptionsru   rv   rw   rx   Zseleniumbase.commonry   rE   Ú
isinstancer   Ú_format_messageÚmessage)Ú	exceptionr}   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   Úexcr   r   r   Ú
format_exc×   s„    























r€   c                 C   s   d|  } | S )Nz
 r   )r}   r   r   r   r|     s    r|   c                 C   s   ddl m} || ƒ‚d S )Nr   )ÚTimeLimitExceededException)rz   r�   )r}   r�   r   r   r   Ú__time_limit_exceeded$  s    r‚   c                  C   sˆ   t tdd ƒr„tjs„tj} tt ¡ d ƒ}|tjtj kr„| }d}tt| ƒƒt| ƒkrht| ƒ}|dkrhd}d||f }t	|ƒ}t
|ƒ d S )NÚ
time_limitiè  Úsr_   Ú z5This test has exceeded the time limit of %s second%s!)rP   Ú	sb_configZrecorder_moderƒ   r   r&   Zstart_time_msZtime_limit_msr'   r|   r‚   )rƒ   Znow_msZdisplay_time_limitÚpluralr}   r   r   r   Úcheck_if_time_limit_exceeded*  s&    
ÿþÿÿrˆ   )N)N)N)'Ú__doc__rI   rB   r`   r.   r   r&   Ú
contextlibr   Zseleniumbaser   r†   Zseleniumbase.configr   Zseleniumbase.fixturesr   r   r    r#   r+   r1   r7   r9   r;   r=   r?   rH   rJ   rN   rS   rU   r\   ri   rl   rn   r€   r|   r‚   rˆ   r   r   r   r   Ú<module>   sB   





H