
    vKgH                        % S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
  S SK	Jr  S SKJr  S SKrS SKJr  S SKJr  \R&                  " S5      r\R*                  " S	5      rS
rSqSqS r\ R6                  " S5      q\ R6                  \S'   \ R6                  " S5      q\ R6                  \S'   S rS r \S 5       r!\S 5       r"S r#S r$ " S S\%5      r& " S S\5      r' " S S\%5      r(\ " S S5      5       r) " S S5      r* " S  S!\*5      r+ " S" S#\*\RX                  RZ                  5      r.\
S$\R^                  \.   4S% j5       r0S$\.4S& jr1g)'    N)defaultdict)asynccontextmanager)contextmanager)	dataclass)ConnectionClosed)connect_websocket_urltrio_cdpTi   c                    U q Sn [        R                  " U U  35      q[        $ ! [         a    [
        R                  " [        5      R                  S   R                  S5      n[        S UR                  5        5       5      n[        S U 5       5      n[        R                  " [        5      nUR!                  SU5        [        R                  " U U 35      q[        s $ f = f)zZAttempt to load the current latest available devtools into the module
cache for use later.z$selenium.webdriver.common.devtools.v   devtoolsc              3   d   #    U H'  oR                  5       (       d  M  UR                  v   M)     g 7fN)is_dirname).0fs     b/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/selenium/webdriver/common/bidi/cdp.py	<genexpr>"import_devtools.<locals>.<genexpr>A   s     O)@AHHJ)@s   00c              3   <   #    U H  n[        US S 5      v   M     g7f)r   N)int)r   xs     r   r   r   B   s     2AS12ZZs   z'Falling back to loading `devtools`: v%s)version	importlibimport_moduler   ModuleNotFoundErrorpathlibPath__file__parentsjoinpathtupleiterdirmaxlogging	getLogger__name__debug)verbasedevtools_pathversionslatestselenium_loggers         r   import_devtoolsr0   3   s    
 G1D**dVC5>: 	  X.66q9BB:NO)>)>)@OO222!++H5GP**dVF8+<=	s   & B>C'&C'connection_context_connection_contextsession_context_session_contextc                 d     [         R                  5       $ ! [         a    [        U  S35      ef = f)zuLook up the current connection.

If there is no current connection, raise a ``RuntimeError`` with a
helpful message.
z*() must be called in a connection context.)r2   getLookupErrorRuntimeErrorfn_names    r   get_connection_contextr;   M   s;    S"&&(( SgY&PQRRS    /c                 d     [         R                  5       $ ! [         a    [        U  S35      ef = f)zoLook up the current session.

If there is no current session, raise a ``RuntimeError`` with a
helpful message.
z'() must be called in a session context.)r4   r6   r7   r8   r9   s    r   get_session_contextr>   Y   s;    P##%% PgY&MNOOPr<   c              #      #    [         R                  U 5      n Sv   [         R                  U5        g! [         R                  U5        f = f7f)z^This context manager installs ``connection`` as the session context for
the current Trio task.N)r2   setreset)
connectiontokens     r   r1   r1   e   s<       ##J/E)!!%(!!%(   A3 AA

Ac              #      #    [         R                  U 5      n Sv   [         R                  U5        g! [         R                  U5        f = f7f)z[This context manager installs ``session`` as the session context for the
current Trio task.N)r4   r@   rA   )sessionrC   s     r   r3   r3   p   s<        )E&u%u%rD   c                 .    [         R                  " SU S9qg)zInstall ``connection`` in the root context so that it will become the
default connection for all tasks.

This is generally not recommended, except it may be necessary in
certain use cases such as running inside Jupyter notebook.
r2   defaultN)contextvars
ContextVarr2   )rB   s    r   set_global_connectionrL   {   s     &001FPZ[    c                 .    [         R                  " SU S9qg)zInstall ``session`` in the root context so that it will become the
default session for all tasks.

This is generally not recommended, except it may be necessary in
certain use cases such as running inside Jupyter notebook.
r4   rH   N)rJ   rK   r4   )rF   s    r   set_global_sessionrO      s     #--.@'RrM   c                   $    \ rS rSrSrS rS rSrg)BrowserError   zcThis exception is raised when the browser's response to a command
indicates that an error occurred.c                     UR                  S5      U l        UR                  S5      U l        UR                  S5      U l        g )Ncodemessagedata)r6   rT   rU   detail)selfobjs     r   __init__BrowserError.__init__   s0    GGFO	wwy)ggforM   c                 T    SU R                    SU R                   SU R                   3$ )NzBrowserError<code=z	 message=z> )rT   rU   rW   rX   s    r   __str__BrowserError.__str__   s'    #DII;i~R}UUrM   )rT   rW   rU   N)r(   
__module____qualname____firstlineno____doc__rZ   r^   __static_attributes__ rM   r   rQ   rQ      s    )&
VrM   rQ   c                   $    \ rS rSrSrS rS rSrg)CdpConnectionClosed   zARaised when a public method is called on a closed CDP connection.c                     Xl         g)zNConstructor.

:param reason:
:type reason: wsproto.frame_protocol.CloseReason
Nreason)rX   rk   s     r   rZ   CdpConnectionClosed.__init__   s	     rM   c                 N    U R                   R                   SU R                   S3$ )zReturn representation.<>)	__class__r(   rk   r]   s    r   __repr__CdpConnectionClosed.__repr__   s$    ..))*!DKK=::rM   rj   N)r(   r`   ra   rb   rc   rZ   rq   rd   re   rM   r   rg   rg      s    K;rM   rg   c                       \ rS rSrSrSrg)InternalError   zbThis exception is only raised when there is faulty logic in TrioCDP or
the integration with PyCDP.re   N)r(   r`   ra   rb   rc   rd   re   rM   r   rt   rt      s    #rM   rt   c                   <    \ rS rSr% SrSr\R                  \S'   Sr	g)CmEventProxy   zA proxy object returned by :meth:`CdpBase.wait_for()``.

After the context manager executes, this proxy object will have a
value set that contains the returned event.
Nvaluere   )
r(   r`   ra   rb   rc   ry   typingAny__annotations__rd   re   rM   r   rw   rw      s     E6::rM   rw   c                       \ rS rSrS rS\R                  \\\R                  4   S\4S jr
SS.S jr\SS	\R                  \   S\R                  \S
4   4S jj5       rS rS rS rSrg
)CdpBase   c                     Xl         X l        X0l        [        [        5      U l        [        R                  " 5       U l        0 U l	        0 U l
        g r   )ws
session_id	target_idr   r@   channels	itertoolscountid_iterinflight_cmdinflight_result)rX   r   r   r   s       r   rZ   CdpBase.__init__   s;    $"#C( (!rM   cmdreturnc                   #    [        U R                  5      n[        R                  " 5       nX4U R                  U'   [        U5      nX$S'   U R
                  (       a  U R
                  US'   [        R                  " U5      n[        R                  [        R                  5      (       a  [        R                  SU SU SU 35         U R                  R                  U5      I Sh  vN   UR%                  5       I Sh  vN   U R&                  R)                  U5      n[        R                  [        R                  5      (       a  [        R                  SU 35        [+        U[,        5      (       aX  [        R                  [        R                  5      (       a.  [        R                  SU S	[/        U5      R0                   35        UeU$  N! [         a  n[!        UR"                  5      SeSnAff = f N7f)
zmExecute a command on the server and wait for the result.

:param cmd: any CDP command
:returns: a CDP result
id	sessionIdzSending CDP message:  z: NzReceived CDP message: zException raised by z
 message: )nextr   trioEventr   r   jsondumpsloggerisEnabledForr&   DEBUGr)   r   send_messageWsConnectionClosedrg   rk   waitr   pop
isinstance	Exceptiontyper(   )rX   r   cmd_id	cmd_eventrequestrequest_strwccresponses           r   executeCdpBase.execute   s     dll#JJL	$'N&!s)??#'??GK jj)w}}--LL0)B{mTU	<''&&{333 nn''++F3w}}--LL1(<=h	**""7==113I;jhI`I`HabcN 4! 	<%cjj1t;	<sI   CG:	G 'G(G ,G:?G8 CG:G 
G5G00G55G:
   )buffer_sizec                    [         R                  " U5      u  p4U H!  nU R                  U   R                  U5        M#     U$ )zPReturn an async iterator that iterates over events matching the
indicated types.)r   open_memory_channelr   add)rX   r   event_typessenderreceiver
event_types         r   listenCdpBase.listen   s=      33K@%JMM*%))&1 &rM   r   Nc                J  #    [         R                  " U5      u  p4U R                  U   R                  U5        [	        5       nU7v   U ISh  vN   UR                  5       I Sh  vN nSSS5      ISh  vN   WUl        g N4 N N! , ISh  vN  (       d  f       N%= f7f)zWait for an event of the given type and return it.

This is an async context manager, so you should open it inside
an async with block. The block will not exit until the indicated
event is received.
N)r   r   r   r   rw   receivery   )rX   r   r   r   r   proxyevents          r   wait_forCdpBase.wait_for   st       33K@j!%%f-8"**,,E 8 , 888sZ   AB#BB#B	&B'B	+B#6B7B#B	B#	B BB B#c                 V    SU;   a  U R                  U5        gU R                  U5        g)zEHandle incoming WebSocket data.

:param dict data: a JSON dictionary
r   N)_handle_cmd_response_handle_event)rX   rV   s     r   _handle_dataCdpBase._handle_data   s'    
 4<%%d+t$rM   c                    US   n U R                   R                  U5      u  p4SU;   a  [        US   5      U R                  U'   O  UR                  US   5      n[        S5      eUR                  5         g! [         a    [        R	                  SU5         gf = f! [         a  nUR                  n SnAOSnAff = fXpR                  U'   Ni)zHandle a response to a command. This will set an event flag that
will return control to the task that called the command.

:param dict data: response as a JSON dictionary
r   z7Got a message with a command ID that does not exist: %sNerrorresultz<The command's generator function did not exit when expected!)r   r   KeyErrorr   warningrQ   r   sendrt   StopIterationry   r@   )rX   rV   r   r   r   _exitreturn_s           r   r   CdpBase._handle_cmd_response
  s     d	**..v6JC d? ,8W+FD  (%HHT(^,#$bcc 			!  	NNTVZ[	 ! %**%+2  (s)   A7 B 7 BB
B='B88B=c                    [         R                  R                  U5      n[        R	                  SU5        [        5       nU R                  [        U5          H  n UR                  U5        M     U(       a!  U R                  [        U5      ==   U-  ss'   gg! [        R                   a    [        R                  SX$5         Mo  [        R                   a    UR                  U5         M  f = f)z?Handle an event.

:param dict data: event as a JSON dictionary
zReceived event: %sz0Unable to send event "%r" due to full channel %sN)r   utilparse_json_eventr   r)   r@   r   r   send_nowaitr   
WouldBlockr   BrokenResourceErrorr   )rX   rV   r   	to_remover   s        r   r   CdpBase._handle_event%  s     ..t4)51E	mmDK0F&""5) 1 MM$u+&)3& 	 ?? `OQV_++ &f%&s   B*C/$C/.C/)r   r   r   r   r   r   r   )r   )r(   r`   ra   rb   rZ   rz   	Generatordictr
   r{   r   r   r   TypeAsyncGeneratorrw   r   r   r   r   rd   re   rM   r   r~   r~      s    "!1!1$6::2E!F 1 < 02  Q FLaLabnptbtLu  "%64rM   r~   c                   L   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       rSr	U =r
$ )
CdpSessioni9  zContains the state for a CDP session.

Generally you should not instantiate this object yourself; you should call
:meth:`CdpConnection.open_session`.
c                    > [         TU ]  XU5        SU l        [        R                  " 5       U l        SU l        [        R                  " 5       U l        g)zConstructor.

:param trio_websocket.WebSocketConnection ws:
:param devtools.target.SessionID session_id:
:param devtools.target.TargetID target_id:
r   N)superrZ   _dom_enable_countr   Lock_dom_enable_lock_page_enable_count_page_enable_lock)rX   r   r   r   rp   s       r   rZ   CdpSession.__init__@  s@     	3!" $		"#!%rM   c                  #    U R                    ISh  vN   U =R                  S-  sl        U R                  S:X  a5  U R                  [        R                  R                  5       5      I Sh  vN   SSS5      ISh  vN   S7v   U R                    ISh  vN   U =R                  S-  sl        U R                  S:X  a5  U R                  [        R                  R                  5       5      I Sh  vN   SSS5      ISh  vN   g N N N! , ISh  vN  (       d  f       N= f N N7 N)! , ISh  vN  (       d  f       g= f7f)zA context manager that executes ``dom.enable()`` when it enters and
then calls ``dom.disable()``.

This keeps track of concurrent callers and only disables DOM
events when all callers have exited.
Nr   r   )r   r   r   r   domenabledisabler]   s    r   
dom_enableCdpSession.dom_enableN  s      (((""a'"%%*ll8<<#6#6#8999 )(
 	(((""a'"%%*ll8<<#7#7#9::: )(( ) : )((( ) ; )(((   EDEAD-D	.D2E=D>ED'EAD-1D)2D-6ED+E	DED$DD$ E)D-+E-E3D64E Ec                  #    U R                    ISh  vN   U =R                  S-  sl        U R                  S:X  a5  U R                  [        R                  R                  5       5      I Sh  vN   SSS5      ISh  vN   S7v   U R                    ISh  vN   U =R                  S-  sl        U R                  S:X  a5  U R                  [        R                  R                  5       5      I Sh  vN   SSS5      ISh  vN   g N N N! , ISh  vN  (       d  f       N= f N N7 N)! , ISh  vN  (       d  f       g= f7f)zA context manager that executes ``page.enable()`` when it enters and
then calls ``page.disable()`` when it exits.

This keeps track of concurrent callers and only disables page
events when all callers have exited.
Nr   r   )r   r   r   r   pager   r   r]   s    r   page_enableCdpSession.page_enablec  s      )))##q(#&&!+ll8==#7#7#9::: *)
 	)))##q(#&&!+ll8==#8#8#:;;; *)) * ; *))) * < *)))r   )r   r   r   r   )r(   r`   ra   rb   rc   rZ   r   r   r   rd   __classcell__rp   s   @r   r   r   9  s5    - ; ;( < <rM   r   c                   t   ^  \ rS rSrSrU 4S jrS r\S\R                  \
   4S j5       rS
S jrS rS	rU =r$ )CdpConnectioniy  a  Contains the connection state for a Chrome DevTools Protocol server.

CDP can multiplex multiple "sessions" over a single connection. This
class corresponds to the "root" session, i.e. the implicitly created
session that has no session ID. This class is responsible for
reading incoming WebSocket messages and forwarding them to the
corresponding session, as well as handling messages targeted at the
root session itself. You should generally call the
:func:`open_cdp()` instead of instantiating this class directly.
c                 2   > [         TU ]  USSS9  0 U l        g)z<Constructor.

:param trio_websocket.WebSocketConnection ws:
N)r   r   )r   rZ   sessions)rX   r   rp   s     r   rZ   CdpConnection.__init__  s     
 	=rM   c                 T   #    U R                   R                  5       I Sh  vN   g N7f)aD  Close the underlying WebSocket connection.

This will cause the reader task to gracefully exit when it tries
to read the next message from the WebSocket. All of the public
APIs (``execute()``, ``listen()``, etc.) will raise
``CdpConnectionClosed`` after the CDP connection is closed. It
is safe to call this multiple times.
N)r   acloser]   s    r   r   CdpConnection.aclose  s      ggnns   (&(r   c                   #    U R                  U5      I Sh  vN n[        U5         U7v   SSS5        g N! , (       d  f       g= f7f)zThis context manager opens a session and enables the "simple" style
of calling CDP APIs.

For example, inside a session context, you can call ``await
dom.get_document()`` and it will execute on the current session
automatically.
N)connect_sessionr3   )rX   r   rF   s      r   open_sessionCdpConnection.open_session  s:      ,,Y77W%M &% 8%%s"   A	6A	8
A	
AA	c                    #    U R                  [        R                  R                  US5      5      I Sh  vN n[	        U R
                  X!5      nX0R                  U'   U$  N*7f)zDReturns a new :class:`CdpSession` connected to the specified
target.TN)r   r   targetattach_to_targetr   r   r   )rX   r   r   rF   s       r   r   CdpConnection.connect_session  sQ       <<(H(HTX(YZZ
TWWj<$+j! [s   3A"A +A"c                   #      U R                   R                  5       I Sh  vN n [        R                  " U5      n[        R                  SU5        SU;   aD  [        R                  R                  US   5      n U R                  U   nUR                  U5        OU R                  U5        M   N! [         a     OJf = f! [        R
                   a    [        SSUS.5      ef = f! [         a    [        SSU< S.5      ef = fU R                  R                  5        HA  u  pTUR                   R                  5        H  u  pVU H  nUR#                  5         M     M      MC     g7f)zpRuns in the background and handles incoming messages: dispatching
responses to commands and events to listeners.NiDzClient received invalid JSON)rT   rU   rV   zReceived message %rr   z-Browser sent a message for an invalid session)r   get_messager   r   loadsJSONDecodeErrorrQ   r   r)   r   r   	SessionIDr   r   r   itemsr   close)rX   rU   rV   r   rF   r   sendersr   s           r   _reader_taskCdpConnection._reader_task  sY      $ 3 3 55qzz'* LL.5d"%__66tK7HI
	"mmJ7G $$T*!!$'9 5% 
  '' q"F?]gn#oppq   &$*'V'1n  ----/JA%..446
%FLLN & 7 0sa   E)B3 B1B3 C >E)=C* %E)1B3 3
C =E)?C  E)$C''E)*DA#E))r   )r   r   )r(   r`   ra   rb   rc   rZ   r   r   rz   AsyncIteratorr   r   r   r  rd   r   r   s   @r   r   r   y  sG    		 
v/C/CJ/O 
 
%# %#rM   r   r   c                  #    [         R                  " 5        ISh  vN n[        X5      I Sh  vN n [        U5         U7v   SSS5        UR	                  5       I Sh  vN   SSS5      ISh  vN   g NZ NI! , (       d  f       N;= f N*! UR	                  5       I Sh  vN    f = f N:! , ISh  vN  (       d  f       g= f7f)a  This async context manager opens a connection to the browser specified
by ``url`` before entering the block, then closes the connection when the
block exits.

The context manager also sets the connection as the default
connection for the current task, so that commands like ``await
target.get_targets()`` will run on this connection automatically. If
you want to use multiple connections concurrently, it is recommended
to open each on in a separate task.
N)r   open_nurseryconnect_cdpr1   r   )urlnurseryconns      r   open_cdpr    s        ""g ..	 #D)
 * ++- #"".))  $++- #"""s   CA7CB.A9B.B A;BB.!B"B.&C1B,2C9B.;
B		BB.B)"B%
#B))B.,C.C4B75CCc                    #    [        X[        S9I Sh  vN n[        U5      nU R                  UR                  5        U$  N,7f)a  Connect to the browser specified by ``url`` and spawn a background task
in the specified nursery.

The ``open_cdp()`` context manager is preferred in most situations.
You should only use this function if you need to specify a custom
nursery. This connection is not automatically closed! You can either
use the connection object as a context manager (``async with
conn:``) or else call ``await conn.aclose()`` on it when you are
done with it. If ``set_context`` is True, then the returned
connection will be installed as the default connection for the
current task. This argument is for unusual use cases, such as
running inside of a notebook.
)max_message_sizeN)r   MAX_WS_MESSAGE_SIZEr   
start_soonr  )r  r
  r   cdp_conns       r   r	  r	    s@      %WDWX	XBR Hx,,-O 
Ys   AA-A)2rJ   r   r   r   r&   r   rz   collectionsr   
contextlibr   r   dataclassesr   r   trio_websocketr   r   r   r'   r   TypeVarr
   r  r   r   r0   rK   r2   r|   r4   r;   r>   r1   r3   rL   rO   r   rQ   rg   rt   rw   r~   r   abcAsyncResourcer   r  r  r	  re   rM   r   <module>r     s  6        # * % !  A 0			:	&
NN3 
, /:.D.DEY.Z [++ Z+6+A+ABS+T +(( T	S	P ) ) & &\S
V9 
V;, ; #I #
   x4 x4v=< =<@Z#GTXX33 Z#z  6//>    *} rM   