
    /_iH                     .   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  G d d	      Z G d
 d      Ze G d d             Ze G d d             Ze G d d             Z G d d      Z G d d      Z G d d      Z G d d      Zy)    N)	dataclass)Any)WebDriverException)command_builder)LogEntryAdded)Sessionc                       e Zd ZdZdZdZy)ResultOwnershipz/Represents the possible result ownership types.nonerootN)__name__
__module____qualname____doc__NONEROOT     d/var/www/html/land_sniper/venv/lib/python3.12/site-packages/selenium/webdriver/common/bidi/script.pyr
   r
      s    9DDr   r
   c                   0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)	RealmTypez$Represents the possible realm types.windowzdedicated-workerzshared-workerzservice-workerworkerzpaint-workletzaudio-workletworkletN)r   r   r   r   WINDOWDEDICATED_WORKERSHARED_WORKERSERVICE_WORKERWORKERPAINT_WORKLETAUDIO_WORKLETWORKLETr   r   r   r   r   $   s.    .F)#M%NF#M#MGr   r   c                   |    e Zd ZU dZeed<   eed<   eed<   dZedz  ed<   dZedz  ed<   ede	ee
f   d	d fd
       Zy)	RealmInfoz%Represents information about a realm.realmorigintypeNcontextsandboxjsonreturnc           	          d|vrt        d      d|vrt        d      d|vrt        d       | |d   |d   |d   |j                  d      |j                  d      	      S )
zCreates a RealmInfo instance from a dictionary.

        Args:
            json: A dictionary containing the realm information.

        Returns:
            RealmInfo: A new instance of RealmInfo.
        r%   z+Missing required field 'realm' in RealmInfor&   z,Missing required field 'origin' in RealmInfor'   z*Missing required field 'type' in RealmInfor(   r)   )r%   r&   r'   r(   r)   
ValueErrorgetclsr*   s     r   	from_jsonzRealmInfo.from_json;   sz     $JKK4KLLIJJw->fHHY'HHY'
 	
r   )r   r   r   r   str__annotations__r(   r)   classmethoddictr   r2   r   r   r   r$   r$   1   sX    /JK
IGS4ZGS4Z
T#s(^ 
 
 
r   r$   c                   T    e Zd ZU dZeed<   dZedz  ed<   edeee	f   dd fd       Z
y)Sourcez*Represents the source of a script message.r%   Nr(   r*   r+   c                 X    d|vrt        d       | |d   |j                  d            S )zCreates a Source instance from a dictionary.

        Args:
            json: A dictionary containing the source information.

        Returns:
            Source: A new instance of Source.
        r%   z(Missing required field 'realm' in Sourcer(   )r%   r(   r-   r0   s     r   r2   zSource.from_json\   s8     $GHHw-HHY'
 	
r   )r   r   r   r   r3   r4   r(   r5   r6   r   r2   r   r   r   r8   r8   U   s@    4JGS4Z
T#s(^ 
 
 
r   r8   c                   r    e Zd ZU dZeed<   eed<   dZedz  ed<   dZedz  ed<   e	deee
f   dd fd	       Zy)
EvaluateResultz+Represents the result of script evaluation.r'   r%   Nresultexception_detailsr*   r+   c                     d|vrt        d      d|vrt        d       | |d   |d   |j                  d      |j                  d            S )zCreates an EvaluateResult instance from a dictionary.

        Args:
            json: A dictionary containing the evaluation result.

        Returns:
            EvaluateResult: A new instance of EvaluateResult.
        r%   z0Missing required field 'realm' in EvaluateResultr'   z/Missing required field 'type' in EvaluateResultr<   exceptionDetails)r'   r%   r<   r=   r-   r0   s     r   r2   zEvaluateResult.from_jsonx   s`     $OPPNOOfw-88H%"hh'9:	
 	
r   )r   r   r   r   r3   r4   r<   r6   r=   r5   r   r2   r   r   r   r;   r;   o   sU    5
IJFD4K%)td{)
T#s(^ 
0@ 
 
r   r;   c                   L    e Zd ZdZdZdededefdZe	deee
f   dd fd	       Zy
)ScriptMessagez"Represents a script message event.script.messagechanneldatasourcec                 .    || _         || _        || _        y NrC   rD   rE   )selfrC   rD   rE   s       r   __init__zScriptMessage.__init__   s    	r   r*   r+   c                     d|vrt        d      d|vrt        d      d|vrt        d       | |d   |d   t        j                  |d               S )zCreates a ScriptMessage instance from a dictionary.

        Args:
            json: A dictionary containing the script message.

        Returns:
            ScriptMessage: A new instance of ScriptMessage.
        rC   z1Missing required field 'channel' in ScriptMessagerD   z.Missing required field 'data' in ScriptMessagerE   z0Missing required field 'source' in ScriptMessagerH   )r.   r8   r2   r0   s     r   r2   zScriptMessage.from_json   sm     D PQQMNN4OPPOf##DN3
 	
r   N)r   r   r   r   event_classr3   r6   r8   rJ   r5   r   r2   r   r   r   rA   rA      sN    ,"K 4  
 
T#s(^ 
 
 
r   rA   c                   D    e Zd ZdZdZdefdZedee	e
f   dd fd       Zy)	RealmCreatedz!Represents a realm created event.script.realmCreated
realm_infoc                     || _         y rG   rP   )rI   rP   s     r   rJ   zRealmCreated.__init__   s	    $r   r*   r+   c                 :     | t         j                  |            S )zCreates a RealmCreated instance from a dictionary.

        Args:
            json: A dictionary containing the realm created event.

        Returns:
            RealmCreated: A new instance of RealmCreated.
        rR   )r$   r2   r0   s     r   r2   zRealmCreated.from_json   s     i11$788r   N)r   r   r   r   rL   r$   rJ   r5   r6   r3   r   r2   r   r   r   rN   rN      s@    +'K%9 % 	9T#s(^ 	9 	9 	9r   rN   c                   D    e Zd ZdZdZdefdZedeee	f   dd fd       Z
y)	RealmDestroyedz#Represents a realm destroyed event.script.realmDestroyedr%   c                     || _         y rG   r%   )rI   r%   s     r   rJ   zRealmDestroyed.__init__   s	    
r   r*   r+   c                 8    d|vrt        d       | |d         S )zCreates a RealmDestroyed instance from a dictionary.

        Args:
            json: A dictionary containing the realm destroyed event.

        Returns:
            RealmDestroyed: A new instance of RealmDestroyed.
        r%   z0Missing required field 'realm' in RealmDestroyedrX   )r.   r0   s     r   r2   zRealmDestroyed.from_json   s'     $OPPg''r   N)r   r   r   r   rL   r3   rJ   r5   r6   r   r2   r   r   r   rU   rU      sA    -)Kc  (T#s(^ (0@ ( (r   rU   c                      e Zd ZdZddddZd*dZd Zd	 Zd
 ZeZ	de
de
fdZde
ddfdZde
defdZdefdZ	 	 	 	 d+de
deee
ef      dz  dee
   dz  dee
   dz  de
dz  de
fdZde
ddfdZdee
   deddfdZ	 	 	 	 	 d,de
dededee   dz  de
dz  dedz  dedz  d edefd!Z	 	 	 d-d"e
dedede
dz  dedz  d edefd#Z	 	 d.d$e
dz  d%e
dz  dee   fd&Zd' Zd( Zd) Zy)/Scriptz)BiDi implementation of the script module.rB   rO   rV   )messagerealm_createdrealm_destroyedNc                 J    || _         || _        d| _        i | _        i | _        y NF)conndriverlog_entry_subscribedsubscriptions	callbacks)rI   ra   rb   s      r   rJ   zScript.__init__   s'    	$)!r   c                     | j                          | j                  j                  t        | j	                  d|            S )Nconsole_subscribe_to_log_entriesra   add_callbackr   _handle_log_entryrI   handlers     r   add_console_message_handlerz"Script.add_console_message_handler   s3    &&(yy%%mT5K5KIW^5_``r   c                     | j                          | j                  j                  t        | j	                  d|            S )N
javascriptrh   rl   s     r   add_javascript_error_handlerz#Script.add_javascript_error_handler   s3    &&(yy%%mT5K5KLZa5bccr   c                 d    | j                   j                  t        |       | j                          y rG   )ra   remove_callbackr   _unsubscribe_from_log_entries)rI   ids     r   remove_console_message_handlerz%Script.remove_console_message_handler   s"    		!!-4**,r   scriptr+   c                 $    | j                  |      S )zPins a script to the current browsing context.

        Args:
            script: The script to pin.

        Returns:
            str: The ID of the pinned script.
        )_add_preload_script)rI   rw   s     r   pinz
Script.pin   s     ''//r   	script_idc                 &    | j                  |       y)zUnpins a script from the current browsing context.

        Args:
            script_id: The ID of the pinned script to unpin.
        N)_remove_preload_script)rI   r{   s     r   unpinzScript.unpin	  s     	##I.r   c                    | j                   t        d      | j                   j                  }g }|D ]"  }|j                  | j	                  |             $ d|i}| j                  |d||r|nd      }|j                  dk(  r|j                  |j                  S i S d}|j                  rQd|j                  v r |d	|j                  d    z  }t        |      d
|j                  v r|d	|j                  d
    z  }t        |      )a[  Executes a script in the current browsing context.

        Args:
            script: The script function to execute.
            *args: Arguments to pass to the script function.

        Returns:
            dict: The result value from the script execution.

        Raises:
            WebDriverException: If the script execution fails.
        Nz1Driver reference is required for script executionr(   T)function_declarationawait_promisetarget	argumentssuccesszError while executing scripttextz: r\   )	rb   r   current_window_handleappend_Script__convert_to_local_value_call_functionr'   r<   r=   )	rI   rw   argsbrowsing_context_idr   argr   r<   error_messages	            r   executezScript.execute  s1    ;;$%XYY"kk?? 	 	ACT::3?@	A 01$$!'tFclV_rv % 
 ;;)#$*MM$=6==E2E:M''V555!r&*B*B6*J)K%LLM %]33 &":"::!r&*B*B9*M)N%OOM$]33r   c           	      B   |ddiS t        |t              rd|dS t        |t        t        f      rt        |t              rat	        j
                  |      rdddS t	        j                  |      r|dkD  rdddS dd	dS |d
k(  rt	        j                  d|      dk  rdddS d}t        |t              r||kD  s|| k  rdt        |      dS d|dS t        |t              rd|dS t        |t        j                        r4d|j                  |j                         dz   dS |j                         dS t        |t        j                        rst        j                  j                  |t        j                  j                        j!                  t        j"                  j$                        }d|j                         dS t        |t&              r"d|D cg c]  }| j)                  |       c}dS t        |t*        t,        f      r"d|D cg c]  }| j)                  |       c}dS t        |t.              rFd|j1                         D cg c]'  \  }}| j)                  |      | j)                  |      g) c}}dS dt        |      dS c c}w c c}w c c}}w )z2Converts a Python value to BiDi LocalValue format.r'   nullboolean)r'   valuenumberNaNr   Infinityz	-Infinityg        g      ?z-0l    bigintstringdateZ)tzinfosetarrayobject)
isinstanceboolintfloatmathisnanisinfcopysignr3   datetimer   	isoformatr   combinetimeminreplacetimezoneutcr   r   listtupler6   items)rI   r   JS_MAX_SAFE_INTEGERdtitemkvs          r   __convert_to_local_valuezScript.__convert_to_local_value9  se   =F##t$%66U|,%'::e$$,u==ZZ&qy(0:FF(0;GGc\dmmC&?!&C$,t<<"2%%53F+F%SfRfJf (3u:>>$u55s#$u55x001"H\U__->-Dttbgbqbqbsttx}}-""**5(--2C2CDLLT\TeTeTiTiLjB"R\\^<<s#!]b,cUYT-J-J4-P,cdde}-#_d.eW[t/L/LT/R.efft$ ejepeper]a]^`aT2215t7T7TUV7WX  %s5z:: -d.es   JJ,Jr   r   contextsuser_contextsr)   c                     ||t        d      d|i}|||d<   |||d<   |||d<   |||d<   | j                  j                  t        d|            }|d   S )	a  Adds a preload script.

        Args:
            function_declaration: The function declaration to preload.
            arguments: The arguments to pass to the function.
            contexts: The browsing context IDs to apply the script to.
            user_contexts: The user context IDs to apply the script to.
            sandbox: The sandbox name to apply the script to.

        Returns:
            str: The preload script ID.

        Raises:
            ValueError: If both contexts and user_contexts are provided.
        z.Cannot specify both contexts and user_contextsfunctionDeclarationr   r   userContextsr)   zscript.addPreloadScriptrw   )r.   ra   r   r   )rI   r   r   r   r   r)   paramsr<   s           r   ry   zScript._add_preload_scriptj  s    . M$=MNN"79M!N "+F;!)F:$%2F>" 'F9""?3Lf#UVhr   c                 V    d|i}| j                   j                  t        d|             y)ziRemoves a preload script.

        Args:
            script_id: The preload script ID to remove.
        rw   zscript.removePreloadScriptNra   r   r   )rI   r{   r   s      r   r}   zScript._remove_preload_script  s(     I&		/*FOPr   handlesr   c                 X    ||d}| j                   j                  t        d|             y)zDisowns the given handles.

        Args:
            handles: The handles to disown.
            target: The target realm or context.
        )r   r   zscript.disownNr   )rI   r   r   r   s       r   _disownzScript._disown  s,     
 			//6BCr   r   result_ownershipserialization_optionsthisuser_activationc	                     ||||d}	|||	d<   |||	d<   |||	d<   |||	d<   | j                   j                  t        d|	            }
t        j	                  |
      S )a  Calls a provided function with given arguments in a given realm.

        Args:
            function_declaration: The function declaration to call.
            await_promise: Whether to await promise resolution.
            target: The target realm or context.
            arguments: The arguments to pass to the function.
            result_ownership: The result ownership type.
            serialization_options: The serialization options.
            this: The 'this' value for the function call.
            user_activation: Whether to trigger user activation.

        Returns:
            EvaluateResult: The result of the function call.
        )r   awaitPromiser   userActivationr   resultOwnershipserializationOptionsr   zscript.callFunctionra   r   r   r;   r2   )rI   r   r   r   r   r   r   r   r   r   r<   s              r   r   zScript._call_function  s    6 $8)-	
  "+F;'(8F$% ,-BF)*!F6N""?3H&#QR''//r   
expressionc                     ||||d}|||d<   |||d<   | j                   j                  t        d|            }t        j	                  |      S )a  Evaluates a provided script in a given realm.

        Args:
            expression: The script expression to evaluate.
            target: The target realm or context.
            await_promise: Whether to await promise resolution.
            result_ownership: The result ownership type.
            serialization_options: The serialization options.
            user_activation: Whether to trigger user activation.

        Returns:
            EvaluateResult: The result of the script evaluation.
        )r   r   r   r   r   r   zscript.evaluater   )	rI   r   r   r   r   r   r   r   r<   s	            r   	_evaluatezScript._evaluate  sh    . %)-	
 '(8F$% ,-BF)*""?3Df#MN''//r   r(   r'   c                     i }|||d<   |||d<   | j                   j                  t        d|            }|d   D cg c]  }t        j	                  |       c}S c c}w )a   Returns a list of all realms, optionally filtered.

        Args:
            context: The browsing context ID to filter by.
            type: The realm type to filter by.

        Returns:
            List[RealmInfo]: A list of realm information.
        r(   r'   zscript.getRealmsrealms)ra   r   r   r$   r2   )rI   r(   r'   r   r<   r%   s         r   _get_realmszScript._get_realms  sh      'F9!F6N""?3Ev#NO8>x8HIu	##E*IIIs   Ac                     | j                   sUt        | j                        }| j                  j                  |j	                  t
        j                               d| _         y y )NT)rc   r   ra   r   	subscriber   rL   rI   sessions     r   ri   z Script._subscribe_to_log_entries  sI    ((dii(GIIg//0I0IJK(,D% )r   c                    | j                   r|t        j                  | j                  j                  vrUt        | j                        }| j                  j                  |j                  t        j                               d| _         y y y r`   )rc   r   rL   ra   re   r   r   unsubscriber   s     r   rt   z$Script._unsubscribe_from_log_entries  sd    $$)B)B$))J]J])]dii(GIIg11-2K2KLM(-D% *^$r   c                     fd}|S )Nc                 6    | j                   k(  r	 |        y y rG   )type_)	log_entryrm   r'   s    r   rk   z3Script._handle_log_entry.<locals>._handle_log_entry  s    $&	" 'r   r   )rI   r'   rm   rk   s    `` r   rk   zScript._handle_log_entry  s    	# ! r   rG   )NNNN)NNNNF)NNF)NN)r   r   r   r   EVENTSrJ   rn   rq   rv   remove_javascript_error_handlerr3   rz   r~   r6   r   r   r   r   ry   r}   r   r   r;   r   r   r$   r   ri   rt   rk   r   r   r   r[   r[      sT   3 $.2Fad- 'E#	0# 	0# 	0/s /t /&4c &4T &4P.; .;h 26%)*."& !&  S#X'$.&  s)d"	& 
 Cy4'&  t&  
& PQ Q QDtCy D$ D4 D$ (,'+-1  %+0!+0 +0 	+0
 :$+0 *+0  $d{+0 Tk+0 +0 
+0d (,-1 %#0#0 #0 	#0
 *#0  $d{#0 #0 
#0N #JtJ DjJ 
i	J0-.!r   r[   )r   r   dataclassesr   typingr   selenium.common.exceptionsr   %selenium.webdriver.common.bidi.commonr   "selenium.webdriver.common.bidi.logr   &selenium.webdriver.common.bidi.sessionr   r
   r   r$   r8   r;   rA   rN   rU   r[   r   r   r   <module>r      s   $   !  9 A < : 
 
  
  
  
F 
 
 
2 
 
 
>
 
D9 9*( (0E! E!r   