
    vKg{I                        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rS SKrS SK	J
r
Jr  S SKJrJr  S SKJrJrJrJrJr  S SKJrJrJr  \ R0                  " \5      rSrSS.S	\4S
 jjr " S S5      r " S S5      r " S S5      r g)    N)OrderedDictdefaultdict)datetime	timedelta)DefaultDictIteratorListOptionalUnion)RequestResponseWebSocketMessage   F)memory_onlyr   c                     U (       a=  [         R                  S5        [        UR                  S5      UR                  S5      S9$ [         R                  S5        [	        UR                  S5      S9$ )a  Create a new storage instance.

Args:
    memory_only: When True, an in-memory implementation will be used which stores
        request data in memory only and nothing on disk. Default False.
    kwargs: Any arguments to initialise the storage with:
        - base_dir: The base directory under which requests are stored
        - maxsize: The maximum number of requests the storage can hold
Returns: A request storage implementation, currently either RequestStorage (default)
    or InMemoryRequestStorage when memory_only is set to True.
zUsing in-memory request storagebase_dirmaxsize)r   r   zUsing default request storage)r   )loginfoInMemoryRequestStoragegetRequestStorage)r   kwargss     T/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/seleniumwire/storage.pycreater      sU     23%vzz*/EvzzZcOdeeHH,-6::j#9::    c                   *    \ rS rSrS\S\S\4S jrSrg)_IndexedRequest*   idurlhas_responsec                 (    Xl         X l        X0l        g Nr    r!   r"   )selfr    r!   r"   s       r   __init___IndexedRequest.__init__+   s    (r   )r"   r    r!   N)__name__
__module____qualname____firstlineno__strboolr'   __static_attributes__ r   r   r   r   *   s    )3 )S ) )r   r   c                      \ rS rSrSrS#S\\   4S jjrS\SS4S jr	S	\
\\\4   S
\S\SS4S jrS\S\SS4S jrS\S\\   4S jrS\S\SS4S jrS\S\SS4S jrS\\   4S jrS\S\\   4S jrS rS\\   4S jrS\\   4S jrS\\   4S jrS$S jrS%S\S\S\\   4S jjrS\S\4S jrS$S  jr S$S! jr!S"r"g)&r   1   zResponsible for persistence of request and response data to disk.

This implementation writes the request and response data to disk, but keeps an in-memory
index for sequencing and fast retrieval.

Instances are designed to be threadsafe.
Nr   c           	         Uc  [         R                  " 5       n[        R                  R	                  US5      U l        [        R                  R	                  U R
                  SR                  [        [        R                  " 5       5      5      5      U l
        [        R                  " U R                  SS9  U R                  5         / U l        [        [        5      U l        ["        R$                  " 5       U l        g)zInitialises a new RequestStorage using an optional base directory.

Args:
    base_dir: The directory where request and response data is stored.
        If not specified, the system temp folder is used.
N.seleniumwirez
storage-{}T)exist_ok)tempfile
gettempdirospathjoinhome_dirformatr-   uuiduuid4session_dirmakedirs_cleanup_old_dirs_indexr   list_ws_messages	threadingLock_lock)r&   r   s     r   r'   RequestStorage.__init__:   s     **,HWW\\(OD "T]]L<O<OPSTXT^T^T`Pa<b c
D$$t4  .0 5@4E^^%
r   requestreturnc           	      d   [        [        R                  " 5       5      nU R                  U5      n[        R
                  " U5        X!l        U R                  XS5        U R                     U R                  R                  [        X!R                  SS95        SSS5        g! , (       d  f       g= f)DSave a request to storage.

Args:
    request: The request to save.
rI   Fr%   N)r-   r=   r>   _get_request_dirr8   mkdirr    _saverG   rB   appendr   r!   )r&   rI   
request_idrequest_dirs       r   save_requestRequestStorage.save_requestR   sr     &
++J7



73ZZKK*++\abc ZZs   *.B!!
B/objdirnamefilenamec                     [        [        R                  R                  X#5      S5       n[        R
                  " X5        S S S 5        g ! , (       d  f       g = f)Nwb)openr8   r9   r:   pickledump)r&   rU   rV   rW   outs        r   rO   RequestStorage._saveb   s3    "'',,w148CKK! 988s   A


ArQ   responsec                     U R                  U5      nUc  [        R                  SU5        gU R                  U5      nU R	                  X$S5        SUl        g)zSave a response to storage against a request with the specified id.

Args:
    request_id: The id of the original request.
    response: The response to save.
N6Cannot save response as request %s is no longer storedr_   T)_get_indexed_requestr   debugrM   rO   r"   )r&   rQ   r_   indexed_requestrR   s        r   save_responseRequestStorage.save_responsef   sS     33J?"IINPZ[++J7

8*5'+$r   c                     U R                      U R                  S S  nS S S 5        W H  nUR                  U:X  d  M  Us  $    g ! , (       d  f       N,= fr$   )rG   rB   r    )r&   rQ   indexrd   s       r   rb   #RequestStorage._get_indexed_requesty   sH    ZZKKNE   %O!!Z/&&  %  Zs   A
Amessagec                     U R                      U R                  U   R                  U5        SSS5        g! , (       d  f       g= fzSave a websocket message against a request with the specified id.

Args:
    request_id: The id of the original handshake request.
    message: The websocket message to save.
N)rG   rD   rP   )r&   rQ   rj   s      r   save_ws_messageRequestStorage.save_ws_message   s.     ZZj)009 ZZs	   5
Aentryc                     U R                  U5      nUc  [        R                  SU5        gU R                  U5      nU R	                  X$S5        g)Save a HAR entry to storage against a request with the specified id.

Args:
    request_id: The id of the original request.
    entry: The HAR entry to save.
N7Cannot save HAR entry as request %s is no longer stored	har_entry)rb   r   rc   rM   rO   )r&   rQ   ro   rd   rR   s        r   save_har_entryRequestStorage.save_har_entry   sK     33J?"IIOQ[\++J7

5{3r   c                     U R                      U R                  SS nSSS5        / nW H4  nU R                  UR                  5      nUc  M#  UR	                  U5        M6     U$ ! , (       d  f       NL= f)a0  Load all previously saved requests known to the storage (known to its index).

The requests are returned as a list of request objects in the order in which they
were saved. Each request will have any associated response and websocket messages
attached if they exist.

Returns: A list of request objects.
N)rG   rB   _load_requestr    rP   )r&   rh   loadedrd   rI   s        r   load_requestsRequestStorage.load_requests   se     ZZKKNE  $O((););<G"g&	  %  Zs   A##
A1c                    U R                  U5      n[        [        R                  R	                  US5      S5       nU R                  U5      nUc
   S S S 5        g U R                  R                  UR                  5      nU(       a  XTl	         [        [        R                  R	                  US5      S5       nU R                  U5      nUb*  Xtl
        [        US5      (       a  UR                  Ul        U?S S S 5        S S S 5        U$ ! , (       d  f       N= f! [        [        4 a     N-f = f! , (       d  f       W$ = f)NrI   rbr_   cert)rM   rZ   r8   r9   r:   	_unpicklerD   r   r    ws_messagesr_   hasattrr}   FileNotFoundErrorEOFError)r&   rQ   rR   reqrI   r   resr_   s           r   rw   RequestStorage._load_request   s   ++J7"'',,{I6=nnS)G	 >= ++//

;K&1#"'',,{J?F##~~c2H++3( #8V44+3==GL ( G >8  GF &x0 3 >=8 sS   D02D0*D8?D	7D	
D	DD0DD-*D0,D--D00
D?c                      [         R                  " U5      $ ! [         a@    [        R	                  [
        R                  5      (       a  [        R                  S5         gf = f)zOUnpickle the object specified by the file f.

If unpickling fails return None.
zError unpickling objectN)r[   load	Exceptionr   isEnabledForloggingDEBUG	exception)r&   fs     r   r~   RequestStorage._unpickle   sK    
		;;q>! 	 ..78	s    AA"!A"c                     U R                      U R                  (       a  U R                  S   nO
 SSS5        g SSS5        U R                  WR                  5      $ ! , (       d  f       N)= f)oLoad the last saved request.

Returns: The last saved request or None if no requests have
    yet been stored.
NrG   rB   rw   r    )r&   last_requests     r   load_last_request RequestStorage.load_last_request   sP     ZZ{{#{{2	 Z  !!,//22 Zs   #A
A+c                    U R                      U R                  SS nSSS5        / nW Hw  nU R                  UR                  5      n [	        [
        R                  R                  US5      S5       nU R                  U5      nUb  UR                  U5        SSS5        My     U$ ! , (       d  f       N= f! , (       d  f       M  = f! [         a     M  f = f)zMLoad all HAR entries known to this storage.

Returns: A list of HAR entries.
Nrs   r|   )rG   rB   rM   r    rZ   r8   r9   r:   r~   rP   r   )r&   rh   entriesrd   rR   r   ro   s          r   load_har_entriesRequestStorage.load_har_entries   s    
 ZZKKNE  $O//0B0BCK"'',,{K@$G1 NN1-E(u-	 HG	  % % Z HG
 % s;   B&*C	2&B7C	&
B47
C	C	C		
CCc              #      #    U R                      U R                  SS nSSS5        W H   nU R                  UR                  5      v   M"     g! , (       d  f       N5= f7f)z_Return an iterator of requests known to the storage.

Returns: An iterator of request objects.
Nr   r&   rh   rd   s      r   iter_requestsRequestStorage.iter_requests  sJ     
 ZZKKNE   %O$$_%7%788  % Zs   AA/A
AAc                 D   U R                      U R                  SS nU R                  R                  5         U R                  R                  5         SSS5        W H1  n[        R
                  " U R                  UR                  5      SS9  M3     g! , (       d  f       NF= f)z3Clear all requests currently known to this storage.NTignore_errors)rG   rB   clearrD   shutilrmtreerM   r    r   s      r   clear_requestsRequestStorage.clear_requests  sq    ZZKKNEKK##% 
  %OMM$//0B0BCSWX  % Zs   AB
Bpatcheck_responsec                 F   U R                      U R                  SS nSSS5        W Hf  n[        R                  " XR                  5      (       d  M*  U(       a  UR
                  (       d	  U(       a  MK  U R                  UR                  5      s  $    g! , (       d  f       N{= f)  Find the first request that matches the specified pattern.

Requests are searched in chronological order.

Args:
    pat: A pattern that will be searched in the request URL.
    check_response: When a match is found, whether to check that the request has
        a corresponding response. Where check_response=True and no response has
        been received, this method will skip the request and continue searching.

Returns: The first request in the storage that matches the pattern,
    or None if no requests match.
N)rG   rB   researchr!   r"   rw   r    )r&   r   r   rh   rd   s        r   findRequestStorage.find!  sq     ZZKKNE   %Oyy1122"'C'CNN--o.@.@AA  %
  Zs   B
B c                 t    [         R                  R                  U R                  SR	                  U5      5      $ )Nz
request-{})r8   r9   r:   r?   r<   r&   rQ   s     r   rM   RequestStorage._get_request_dir9  s(    ww||D,,l.A.A*.MNNr   c                 >   [         R                  SU R                  5        U R                  5         [        R
                  " U R                  SS9   [        R                  " [        R                  R                  U R                  5      5        g! [         a     gf = f)zRemove all stored requests, the storage directory containing those
requests, and if that is the only storage directory, also the top level
parent directory.
zCleaning up %sTr   N)r   rc   r?   r   r   r   r8   rmdirr9   rV   OSErrorr&   s    r   cleanupRequestStorage.cleanup<  so    
 			"D$4$45d&&d;	HHRWW__T%5%567 		s   =B 
BBc                    [         R                  R                  U R                  5      n[         R                  " U5       H  n[         R                  R                  X5      n [         R                  R                  U5      [        R                  " 5       [        [        S9-
  R                  5       :  a  [        R                  " USS9  M  M     g! [         a     M  f = f)zkClean up and remove any old storage directories that were not previously
cleaned up properly by cleanup().
)daysTr   N)r8   r9   rV   r?   listdirr:   getmtimer   nowr   REMOVE_DATA_OLDER_THAN_DAYS	timestampr   r   r   )r&   
parent_dirstorage_dirs      r   rA    RequestStorage._cleanup_old_dirsK  s     WW__T%5%56
::j1K'',,z?KGG$$[1||~	7R(SS^^`a MM+TBa	 2 % s   #A'C
CC)rB   rG   rD   r;   r?   r$   rJ   NT)#r)   r*   r+   r,   __doc__r
   r-   r'   r   rS   r   r   dictrO   re   r   rb   r   rm   rt   r	   ry   rw   r~   r   r   r   r   r   r.   r   rM   r   rA   r/   r0   r   r   r   r   1   sh   &# &0dG d d "w$67 "# "QT "Y] ", ,x ,D ,&s x7P :# :8H :T :4 4T 4d 4"tG} , 0A B 38G#4 3$t* 2	9x0 	9Y T Xg=N 0O3 O3 Or   r   c                   *   \ rS rSrSr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4S jrS
\S\SS4S jrS
\S\\	   4S jrS\\	   4S jrS\\	   4S jrS\\   4S jrS\\	   4S jrSS jrSS\S\S\\	   4S jjrSS jrSrg)r   i]  zKeeps request and response data in memory only.

By default there is no limit on the number of requests that will be stored. This can
be adjusted with the 'maxsize' attribute when creating a new instance.

Instances are designed to be threadsafe.
Nr   r   c                    Uc  [         R                  " 5       n[        R                  R	                  US5      U l        Uc  [        R                  OUU l        [        5       U l
        [        R                  " 5       U l        g)a  Initialise a new InMemoryRequestStorage.

Args:
    base_dir: The directory where certificate data is stored.
        If not specified, the system temp folder is used.
    maxsize: The maximum number of requests to store. Default no limit.
        When this attribute is set and the storage reaches the specified maximum
        size, old requests are discarded sequentially as new requests arrive.
Nr4   )r6   r7   r8   r9   r:   r;   sysr   _maxsizer   	_requestsrE   rF   rG   )r&   r   r   s      r   r'   InMemoryRequestStorage.__init__f  sV     **,HWW\\(OD'.G$^^%
r   rI   rJ   c                    [        [        R                  " 5       5      Ul        U R                     U R
                  S:  a|  [        U R                  5      U R
                  :  a>  U R                  R                  SS9  [        U R                  5      U R
                  :  a  M>  SU0U R                  UR                  '   SSS5        g! , (       d  f       g= f)rL   r   F)lastrI   N)	r-   r=   r>   r    rG   r   lenr   popitem)r&   rI   s     r   rS   #InMemoryRequestStorage.save_requestz  s     &
ZZ}}q $..)T]]:NN***6 $..)T]]: w.wzz* ZZs   A0C"C
CrQ   r_   c                     U R                  U5      nUb,  X#l        [        US5      (       a  UR                  Ul        U?gg[        R                  SU-  5        g)a	  Save a response to storage against a request with the specified id.

Any certificate information will be attached to the original request
against the request.cert attribute.

Args:
    request_id: The id of the original request.
    response: The response to save.
Nr}   ra   )_get_requestr_   r   r}   r   rc   )r&   rQ   r_   rI   s       r   re   $InMemoryRequestStorage.save_response  sY     ##J/' x(('}}M ) IINQ[[\r   rj   c                 d    U R                  U5      nUb  UR                  R                  U5        ggrl   )r   r   rP   )r&   rQ   rj   rI   s       r   rm   &InMemoryRequestStorage.save_ws_message  s3     ##J/&&w/ r   ro   c                     U R                       U R                  U   nX#S'   SSS5        g! [         a    [        R	                  SU5         N+f = f! , (       d  f       g= f)rq   rs   rr   N)rG   r   KeyErrorr   rc   )r&   rQ   ro   vs       r   rt   %InMemoryRequestStorage.save_har_entry  sX     ZZaNN:.!&+ Z  a		SU_`a	 Zs&   A+ AAAA
Ac                     U R                       U R                  U   S   sSSS5        $ ! [         a     SSS5        gf = f! , (       d  f       g= f)z@Get a request with the specified id or None if no request found.rI   N)rG   r   r   r   s     r   r   #InMemoryRequestStorage._get_request  sI    ZZ~~j1)< Z  	 Z Zs!   A*
?A?A
Ac                     U R                      U R                  R                  5        Vs/ sH  oS   PM	     snsSSS5        $ s  snf ! , (       d  f       g= f)a5  Load all previously saved requests.

The requests are returned as a list of request objects in the order in which they
were saved.

Note that for efficiency request objects are not copied when returned, so any
change made to a request will also affect the stored version.

Returns: A list of request objects.
rI   NrG   r   valuesr&   r   s     r   ry   $InMemoryRequestStorage.load_requests  s@     ZZ*...*?*?*AB*AQiL*AB ZB Zs   AAAA
Ac                     U R                       [        [        U R                  R	                  5       5      5      S   sSSS5        $ ! [
        [        4 a     SSS5        gf = f! , (       d  f       g= f)r   rI   N)rG   nextreversedr   r   StopIterationr   r   s    r   r   (InMemoryRequestStorage.load_last_request  s^     ZZHT^^%:%:%<=>yI Z "8, 	 Z Zs(   A%.AA"A%!A""A%%
A3c                     U R                      U R                  R                  5        Vs/ sH  nSU;   d  M  US   PM     snsSSS5        $ s  snf ! , (       d  f       g= f)zHLoad all previously saved HAR entries.

Returns: A list of HAR entries.
rs   Nr   r   s     r   r   'InMemoryRequestStorage.load_har_entries  sL    
 ZZ,0NN,A,A,CX,Cq{VWGWNAkN,CX ZX Zs!   A	A	A AA
Ac              #      #    U R                      [        U R                  R                  5       5      nSSS5        W H
  nUS   v   M     g! , (       d  f       N= f7f)zVReturn an iterator over the saved requests.

Returns: An iterator of request objects.
NrI   )rG   rC   r   r   )r&   r   r   s      r   r   $InMemoryRequestStorage.iter_requests  sG     
 ZZ$..//12F  AI,  Zs   A$AA
AAc                     U R                      U R                  R                  5         SSS5        g! , (       d  f       g= fz$Clear all previously saved requests.N)rG   r   r   r   s    r   r   %InMemoryRequestStorage.clear_requests  s#    ZZNN  " ZZs   1
?r   r   c                 B   U R                      U R                  R                  5        H[  nUS   n[        R                  " XR
                  5      (       d  M/  U(       a  UR                  (       d	  U(       a  MP  Us  sSSS5        $    SSS5        g! , (       d  f       g= f)r   rI   N)rG   r   r   r   r   r!   r_   )r&   r   r   r   rI   s        r   r   InMemoryRequestStorage.find  sn     ZZ^^**,I,99S++..&7+;+;NN& Z,   Z s   ABB8BB
Bc                 $    U R                  5         gr   )r   r   s    r   r   InMemoryRequestStorage.cleanup  s    r   )rG   r   r   r;   )NNr   r   )r)   r*   r+   r,   r   r
   r-   intr'   r   rS   r   re   r   rm   r   rt   r   r	   ry   r   r   r   r   r   r.   r   r   r/   r0   r   r   r   r   ]  s   &# & &(G  "] ]x ]D ],
0# 
08H 
0T 
0a aT ad as x/@ CtG} C
8G#4 
Y$t* Y	x0 	#
 T Xg=N 0r   r   )!r   r8   r[   r   r   r   r6   rE   r=   collectionsr   r   r   r   typingr   r   r	   r
   r   seleniumwire.requestr   r   r   	getLoggerr)   r   r   r.   r   r   r   r   r0   r   r   <module>r      s~     	  	  
    0 ( ? ? D D!    #( ;4 ;() )i iX	r rr   