
    ,iMI                     2   d dl Z d dlZd dlmZ d dlmZ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Optional)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     t/var/www/system.tvplus.app.br/jogos/scrap/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e   ed<   dZee   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   r*   classmethoddictr   r3   r   r   r   r%   r%   1   sX    /JK
I!GXc]!!GXc]!
T#s(^ 
 
 
r   r%   c                   T    e Zd ZU dZeed<   dZee   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.   r1   s     r   r3   zSource.from_json\   s8     $GHHw-HHY'
 	
r   )r   r   r   r   r4   r5   r)   r   r6   r7   r   r3   r   r   r   r9   r9   U   s@    4J!GXc]!
T#s(^ 
 
 
r   r9   c                   r    e Zd ZU dZeed<   eed<   dZee   ed<   dZ	ee   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.   r1   s     r   r3   zEvaluateResult.from_jsonx   s`     $OPPNOOfw-88H%"hh'9:	
 	
r   )r   r   r   r   r4   r5   r=   r   r7   r>   r6   r   r3   r   r   r   r<   r<   o   sU    5
IJ!FHTN!(,x~,
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rD   rE   rF   )selfrD   rE   rF   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.
        rD   z1Missing required field 'channel' in ScriptMessagerE   z.Missing required field 'data' in ScriptMessagerF   z0Missing required field 'source' in ScriptMessagerI   )r/   r9   r3   r1   s     r   r3   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_classr4   r7   r9   rK   r6   r   r3   r   r   r   rB   rB      sN    ,"K 4  
 
T#s(^ 
 
 
r   rB   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 rH   rQ   )rJ   rQ   s     r   rK   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.
        rS   )r%   r3   r1   s     r   r3   zRealmCreated.from_json   s     i11$788r   N)r   r   r   r   rM   r%   rK   r6   r7   r4   r   r3   r   r   r   rO   rO      s@    +'K%9 % 	9T#s(^ 	9 	9 	9r   rO   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 rH   r&   )rJ   r&   s     r   rK   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 RealmDestroyedrY   )r/   r1   s     r   r3   zRealmDestroyed.from_json   s'     $OPPg''r   N)r   r   r   r   rM   r4   rK   r6   r7   r   r3   r   r   r   rV   rV      sA    -)Kc  (T#s(^ (0@ ( (r   rV   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
ef         deee
      deee
      dee
   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e      dee
   dee   dee   d edefd!Z	 	 	 d-d"e
dededee
   dee   d edefd#Z	 	 d.d$ee
   d%ee
   dee   fd&Zd' Zd( Zd) Zy)/Scriptz)BiDi implementation of the script module.rC   rP   rW   )messagerealm_createdrealm_destroyedNc                 J    || _         || _        d| _        i | _        i | _        y NF)conndriverlog_entry_subscribedsubscriptions	callbacks)rJ   rb   rc   s      r   rK   zScript.__init__   s'    	$)!r   c                     | j                          | j                  j                  t        | j	                  d|            S )Nconsole_subscribe_to_log_entriesrb   add_callbackr   _handle_log_entryrJ   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
javascriptri   rm   s     r   add_javascript_error_handlerz#Script.add_javascript_error_handler   s3    &&(yy%%mT5K5KLZa5bccr   c                 d    | j                   j                  t        |       | j                          y rH   )rb   remove_callbackr   _unsubscribe_from_log_entries)rJ   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)rJ   rx   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)rJ   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]   )	rc   r   current_window_handleappend_Script__convert_to_local_value_call_functionr(   r=   r>   )	rJ   rx   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 )zD
        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copysignr4   datetimer   	isoformatr   combinetimeminreplacetimezoneutcr   r   listtupler7   items)rJ   r   JS_MAX_SAFE_INTEGERdtitemkvs          r   __convert_to_local_valuezScript.__convert_to_local_value:  sg    =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.addPreloadScriptrx   )r/   rb   r   r   )rJ   r   r   r   r   r*   paramsr=   s           r   rz   zScript._add_preload_scriptm  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.
        rx   zscript.removePreloadScriptNrb   r   r   )rJ   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   )rJ   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rb   r   r   r<   r3   )rJ   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   )	rJ   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)rb   r   r   r%   r3   )rJ   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)rd   r	   rb   r   	subscriber   rM   rJ   sessions     r   rj   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 ra   )rd   r   rM   rb   rf   r	   r   unsubscriber   s     r   ru   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 rH   )type_)	log_entryrn   r(   s    r   rl   z3Script._handle_log_entry.<locals>._handle_log_entry"  s    $&	" 'r   r   )rJ   r(   rn   rl   s    `` r   rl   zScript._handle_log_entry!  s    	# ! r   rH   )NNNN)NNNNF)NNF)NN)r   r   r   r   EVENTSrK   ro   rr   rw   remove_javascript_error_handlerr4   r{   r   r7   r   r   r   r   r   rz   r~   r   r   r<   r   r   r%   r   rj   ru   rl   r   r   r   r\   r\      sT   3 $.2Fad- 'E#	0# 	0# 	0/s /t /'4c '4T '4R0; 0;l 59(,-1!%& !&  Dc3h01&  49%	& 
  S	*&  #&  
& PQ Q QDtCy D$ D4 D$ +/*.04# %+0!+0 +0 	+0
 DJ'+0 #3-+0  (~+0 tn+0 +0 
+0d +/04 %#0#0 #0 	#0
 #3-#0  (~#0 #0 
#0N "&"J#J smJ 
i	J0-.!r   r\   )r   r   dataclassesr   typingr   r   selenium.common.exceptionsr   %selenium.webdriver.common.bidi.commonr   "selenium.webdriver.common.bidi.logr   &selenium.webdriver.common.bidi.sessionr	   r   r   r%   r9   r<   rB   rO   rV   r\   r   r   r   <module>r      s   $   !   9 A < : 
 
  
  
  
F 
 
 
2 
 
 
>
 
D9 9*( (0H! H!r   