
    'hvK                     2   S SK r S SKrS SKJr  S SKJrJr  S SKJr  S SK	J
r
  SSKJr  SSKJr   " S	 S
5      r " S S5      r\ " S S5      5       r\ " S S5      5       r\ " S S5      5       r " S S5      r " S S5      r " S S5      r " S S5      rg)    N)	dataclass)AnyOptional)WebDriverException)command_builder   )LogEntryAdded)Sessionc                        \ rS rSrSrSrSrSrg)ResultOwnership   z/Represents the possible result ownership types.noneroot N)__name__
__module____qualname____firstlineno____doc__NONEROOT__static_attributes__r       n/var/www/home/psychics-availability/venv/lib/python3.13/site-packages/selenium/webdriver/common/bidi/script.pyr   r      s    9DDr   r   c                   8    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrg)	RealmType%   z$Represents the possible realm types.windowzdedicated-workerzshared-workerzservice-workerworkerzpaint-workletzaudio-workletworkletr   N)r   r   r   r   r   WINDOWDEDICATED_WORKERSHARED_WORKERSERVICE_WORKERWORKERPAINT_WORKLETAUDIO_WORKLETWORKLETr   r   r   r   r   r   %   s.    .F)#M%NF#M#MGr   r   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   Sr\\   \S'   Sr	\\   \S'   \
S	\\\4   S
S 4S j5       rSrg)	RealmInfo2   z%Represents information about a realm.realmorigintypeNcontextsandboxjsonreturnc           	          SU;  a  [        S5      eSU;  a  [        S5      eSU;  a  [        S5      eU " US   US   US   UR                  S5      UR                  S5      S	9$ )
zCreates a RealmInfo instance from a dictionary.

Parameters:
-----------
    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/   r0   )r,   r-   r.   r/   r0   
ValueErrorgetclsr1   s     r   	from_jsonRealmInfo.from_json<   sz     $JKK4KLLIJJw->fHHY'HHY'
 	
r   r   )r   r   r   r   r   str__annotations__r/   r   r0   classmethoddictr   r9   r   r   r   r   r*   r*   2   sX    /JK
I!GXc]!!GXc]!
T#s(^ 
 
 
r   r*   c                   ^    \ rS rSr% Sr\\S'   Sr\\   \S'   \	S\
\\4   SS 4S j5       rS	rg)
SourceX   z*Represents the source of a script message.r,   Nr/   r1   r2   c                 V    SU;  a  [        S5      eU " US   UR                  S5      S9$ )zCreates a Source instance from a dictionary.

Parameters:
-----------
    json: A dictionary containing the source information.

Returns:
-------
    Source: A new instance of Source.
r,   z(Missing required field 'realm' in Sourcer/   )r,   r/   r4   r7   s     r   r9   Source.from_json_   s8     $GHHw-HHY'
 	
r   r   )r   r   r   r   r   r;   r<   r/   r   r=   r>   r   r9   r   r   r   r   r@   r@   X   s@    4J!GXc]!
T#s(^ 
 
 
r   r@   c                   |    \ rS rSr% Sr\\S'   \\S'   Sr\\	   \S'   Sr
\\	   \S'   \S\	\\4   S	S 4S
 j5       rSrg)EvaluateResultt   z+Represents the result of script evaluation.r.   r,   Nresultexception_detailsr1   r2   c                     SU;  a  [        S5      eSU;  a  [        S5      eU " US   US   UR                  S5      UR                  S5      S9$ )zCreates an EvaluateResult instance from a dictionary.

Parameters:
-----------
    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 EvaluateResultrG   exceptionDetails)r.   r,   rG   rH   r4   r7   s     r   r9   EvaluateResult.from_json}   s`     $OPPNOOfw-88H%"hh'9:	
 	
r   r   )r   r   r   r   r   r;   r<   rG   r   r>   rH   r=   r   r9   r   r   r   r   rE   rE   t   sU    5
IJ!FHTN!(,x~,
T#s(^ 
0@ 
 
r   rE   c                   X    \ rS rSrSrSrS\S\S\4S jr	\
S\\\4   S	S 4S
 j5       rSrg)ScriptMessage   z"Represents a script message event.script.messagechanneldatasourcec                 (    Xl         X l        X0l        g NrP   rQ   rR   )selfrP   rQ   rR   s       r   __init__ScriptMessage.__init__   s    	r   r1   r2   c                     SU;  a  [        S5      eSU;  a  [        S5      eSU;  a  [        S5      eU " US   US   [        R                  US   5      S9$ )zCreates a ScriptMessage instance from a dictionary.

Parameters:
-----------
    json: A dictionary containing the script message.

Returns:
-------
    ScriptMessage: A new instance of ScriptMessage.
rP   z1Missing required field 'channel' in ScriptMessagerQ   z.Missing required field 'data' in ScriptMessagerR   z0Missing required field 'source' in ScriptMessagerU   )r5   r@   r9   r7   s     r   r9   ScriptMessage.from_json   sm     D PQQMNN4OPPOf##DN3
 	
r   rU   N)r   r   r   r   r   event_classr;   r>   r@   rW   r=   r   r9   r   r   r   r   rM   rM      sN    ,"K 4  
 
T#s(^ 
 
 
r   rM   c                   P    \ rS rSrSrSrS\4S jr\S\	\
\4   SS 4S j5       rS	rg
)RealmCreated   z!Represents a realm created event.script.realmCreated
realm_infoc                     Xl         g rT   r`   )rV   r`   s     r   rW   RealmCreated.__init__   s    $r   r1   r2   c                 4    U " [         R                  U5      S9$ )zCreates a RealmCreated instance from a dictionary.

Parameters:
-----------
    json: A dictionary containing the realm created event.

Returns:
-------
    RealmCreated: A new instance of RealmCreated.
rb   )r*   r9   r7   s     r   r9   RealmCreated.from_json   s     i11$788r   rb   N)r   r   r   r   r   r[   r*   rW   r=   r>   r;   r   r9   r   r   r   r   r]   r]      s@    +'K%9 % 9T#s(^ 9 9 9r   r]   c                   P    \ rS rSrSrSrS\4S jr\S\	\\
4   SS 4S j5       rS	rg
)RealmDestroyed   z#Represents a realm destroyed event.script.realmDestroyedr,   c                     Xl         g rT   r,   )rV   r,   s     r   rW   RealmDestroyed.__init__   s    
r   r1   r2   c                 6    SU;  a  [        S5      eU " US   S9$ )zCreates a RealmDestroyed instance from a dictionary.

Parameters:
-----------
    json: A dictionary containing the realm destroyed event.

Returns:
-------
    RealmDestroyed: A new instance of RealmDestroyed.
r,   z0Missing required field 'realm' in RealmDestroyedrk   )r5   r7   s     r   r9   RealmDestroyed.from_json   s'     $OPPg''r   rk   N)r   r   r   r   r   r[   r;   rW   r=   r>   r   r9   r   r   r   r   rg   rg      sA    -)Kc  (T#s(^ (0@ ( (r   rg   c                      \ rS rSrSrSSSS.rS,S jrS	 rS
 rS r	\	r
S\S\4S jrS\SS4S jrS\S\4S jrS\4S jr    S-S\S\\\\\4         S\\\      S\\\      S\\   S\4S jjrS\SS4S jrS\\   S\SS4S jr     S.S\S\S\S\\\      S\\   S\\   S \\   S!\S\4S" jjr   S/S#\S\S\S\\   S\\   S!\S\4S$ jjr  S0S%\\   S&\\   S\\   4S' jjrS( rS) rS* rS+r g)1Script   z)BiDi implementation of the script module.rO   r_   ri   )messagerealm_createdrealm_destroyedNc                 F    Xl         X l        SU l        0 U l        0 U l        g NF)conndriverlog_entry_subscribedsubscriptions	callbacks)rV   rw   rx   s      r   rW   Script.__init__   s#    	$)!r   c                     U R                  5         U R                  R                  [        U R	                  SU5      5      $ )Nconsole_subscribe_to_log_entriesrw   add_callbackr	   _handle_log_entryrV   handlers     r   add_console_message_handler"Script.add_console_message_handler   s3    &&(yy%%mT5K5KIW^5_``r   c                     U R                  5         U R                  R                  [        U R	                  SU5      5      $ )N
javascriptr   r   s     r   add_javascript_error_handler#Script.add_javascript_error_handler  s3    &&(yy%%mT5K5KLZa5bccr   c                 d    U R                   R                  [        U5        U R                  5         g rT   )rw   remove_callbackr	   _unsubscribe_from_log_entries)rV   ids     r   remove_console_message_handler%Script.remove_console_message_handler  s"    		!!-4**,r   scriptr2   c                 $    U R                  U5      $ )zPins a script to the current browsing context.

Parameters:
-----------
    script: The script to pin.

Returns:
-------
    str: The ID of the pinned script.
)_add_preload_script)rV   r   s     r   pin
Script.pin  s     ''//r   	script_idc                 &    U R                  U5        g)zUnpins a script from the current browsing context.

Parameters:
-----------
    script_id: The ID of the pinned script to unpin.
N)_remove_preload_script)rV   r   s     r   unpinScript.unpin  s     	##I.r   c                    U R                   c  [        S5      eU R                   R                  n/ nU H#  nUR                  U R	                  U5      5        M%     SU0nU R                  USXd(       a  UOSS9nUR                  S:X  a  UR                  b  UR                  $ 0 $ SnUR                  (       aK  SUR                  ;   a  US	UR                  S    3-  nO%S
UR                  ;   a  US	UR                  S
    3-  n[        U5      e)a<  Executes a script in the current browsing context.

Parameters:
-----------
    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: rr   )	rx   r   current_window_handleappend_Script__convert_to_local_value_call_functionr.   rG   rH   )	rV   r   argsbrowsing_context_idr   argr   rG   error_messages	            r   executeScript.execute!  s   " ;;$%XYY"kk?? 	CT::3?@  01$$!'tFclV_rv % 
 ;;)#$*MM$=6==E2E:M''V555!r&*B*B6*J)K%LLM&":"::!r&*B*B9*M)N%OOM$]33r   c           	         Uc  SS0$ [        U[        5      (       a  SUS.$ [        U[        [        45      (       a  [        U[        5      (       aq  [        R
                  " U5      (       a  SSS.$ [        R                  " U5      (       a  US:  a  SSS.$ SS	S.$ US
:X  a   [        R                  " SU5      S:  a  SSS.$ Sn[        U[        5      (       a  X:  d  X* :  a  S[        U5      S.$ SUS.$ [        U[        5      (       a  SUS.$ [        U[        R                  5      (       a5  SUR                  c  UR                  5       S-   S.$ UR                  5       S.$ [        U[        R                  5      (       ap  [        R                  R                  U[        R                  R                  5      R!                  [        R"                  R$                  S9nSUR                  5       S.$ [        U[&        5      (       a$  SU Vs/ s H  o@R)                  U5      PM     snS.$ [        U[*        [,        45      (       a$  SU Vs/ s H  o@R)                  U5      PM     snS.$ [        U[.        5      (       aH  SUR1                  5        VVs/ s H'  u  pVU R)                  U5      U R)                  U5      /PM)     snnS.$ S[        U5      S.$ s  snf s  snf s  snnf )z4
Converts 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copysignr;   datetimer   	isoformatr   combinetimeminreplacetimezoneutcr   r   listtupler>   items)rV   r   JS_MAX_SAFE_INTEGERdtitemkvs          r   __convert_to_local_valueScript.__convert_to_local_valueM  s    =F##t$$%66U|,,%''::e$$$,u==ZZ&&qy(0:FF(0;GGc\dmmC&?!&C$,t<<"2%%%5+F%RfJf (3u:>>$u55s##$u55x0011"H\U__->-Dttbgbqbqbsttx}}--""**5(--2C2CDLLT\TeTeTiTiLjB"R\\^<<s##!]b,c]bUY-J-J4-P]b,cdde}--#_d.e_dW[/L/LT/R_d.efft$$ ejepeperer]a]^T2215t7T7TUV7WXer  %s5z:: -d.es   KK.K"r   r   contextsuser_contextsr0   c                     Ub  Ub  [        S5      eSU0nUb  X&S'   Ub  X6S'   Ub  XFS'   Ub  XVS'   U R                  R                  [        SU5      5      nUS   $ )	a  Adds a preload script.

Parameters:
-----------
    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   userContextsr0   zscript.addPreloadScriptr   )r5   rw   r   r   )rV   r   r   r   r   r0   paramsrG   s           r   r   Script._add_preload_script  s    4 M$=MNN"79M!N "+;!):$%2>" '9""?3Lf#UVhr   c                 V    SU0nU R                   R                  [        SU5      5        g)zcRemoves a preload script.

Parameters:
-----------
    script_id: The preload script ID to remove.
r   zscript.removePreloadScriptNrw   r   r   )rV   r   r   s      r   r   Script._remove_preload_script  s(     I&		/*FOPr   handlesr   c                 X    UUS.nU R                   R                  [        SU5      5        g)zDisowns the given handles.

Parameters:
-----------
    handles: The handles to disown.
    target: The target realm or context.
)r   r   zscript.disownNr   )rV   r   r   r   s       r   _disownScript._disown  s,     
 			//6BCr   r   result_ownershipserialization_optionsthisuser_activationc	                     UUUUS.n	Ub  XIS'   Ub  XYS'   Ub  XiS'   Ub  XyS'   U R                   R                  [        SU	5      5      n
[        R	                  U
5      $ )aF  Calls a provided function with given arguments in a given realm.

Parameters:
-----------
    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rw   r   r   rE   r9   )rV   r   r   r   r   r   r   r   r   r   rG   s              r   r   Script._call_function  s    : $8)-	
  "+;'(8$% ,-B)*!6N""?3H&#QR''//r   
expressionc                     UUUUS.nUb  XGS'   Ub  XWS'   U R                   R                  [        SU5      5      n[        R	                  U5      $ )a  Evaluates a provided script in a given realm.

Parameters:
-----------
    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   )	rV   r   r   r   r   r   r   r   rG   s	            r   	_evaluateScript._evaluate  sd    2 %)-	
 '(8$% ,-B)*""?3Df#MN''//r   r/   r.   c                     0 nUb  XS'   Ub  X#S'   U R                   R                  [        SU5      5      nUS    Vs/ s H  n[        R	                  U5      PM     sn$ s  snf )zReturns a list of all realms, optionally filtered.

Parameters:
-----------
    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)rw   r   r   r*   r9   )rV   r/   r.   r   rG   r,   s         r   _get_realmsScript._get_realms  sj       '9!6N""?3Ev#NO8>x8HI8Hu	##E*8HIIIs   Ac                     U R                   (       dU  [        U R                  5      nU R                  R                  UR	                  [
        R                  5      5        SU l         g g )NT)ry   r
   rw   r   	subscriber	   r[   rV   sessions     r   r    Script._subscribe_to_log_entries3  sI    ((dii(GIIg//0I0IJK(,D% )r   c                 "   U R                   (       a~  [        R                  U R                  R                  ;  aU  [        U R                  5      nU R                  R                  UR                  [        R                  5      5        SU l         g g g rv   )ry   r	   r[   rw   r{   r
   r   unsubscriber   s     r   r   $Script._unsubscribe_from_log_entries9  sd    $$)B)B$))J]J])]dii(GIIg11-2K2KLM(-D% *^$r   c                    ^^ UU4S jnU$ )Nc                 8   > U R                   T:X  a	  T" U 5        g g rT   )type_)	log_entryr   r.   s    r   r   3Script._handle_log_entry.<locals>._handle_log_entry@  s    $&	" 'r   r   )rV   r.   r   r   s    `` r   r   Script._handle_log_entry?  s    	# ! r   )r{   rw   rx   ry   rz   rT   )NNNN)NNNNF)NNF)NN)!r   r   r   r   r   EVENTSrW   r   r   r   remove_javascript_error_handlerr;   r   r   r>   r   r   r   r   r   r   r   r   r   rE   r   r   r*   r   r   r   r   r   r   r   r   rp   rp      sT   3 $.2Fad- 'E#0# 0# 0/s /t /*4c *4T *4X0; 0;l 59(,-1!%) !)  Dc3h01)  49%	) 
  S	*)  #)  
) VQ Q QDtCy D$ D4 D& +/*.04# %-0!-0 -0 	-0
 DJ'-0 #3--0  (~-0 tn-0 -0 
-0h +/04 %%0%0 %0 	%0
 #3-%0  (~%0 %0 
%0R "&"J#J smJ 
i	J4-.!r   rp   )r   r   dataclassesr   typingr   r   selenium.common.exceptionsr   %selenium.webdriver.common.bidi.commonr   logr	   r   r
   r   r   r*   r@   rE   rM   r]   rg   rp   r   r   r   <module>r     s   $   !   9 A   
 
 "
 "
 "
J 
 
 
6 
 
 
B!
 !
H9 9.( (4Y! Y!r   