
    vKgm                        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
Jr  S SKJrJrJrJrJrJrJrJrJr  \R,                  " \5      r " S S5      r " S	 S
\5      r " S S\5      r " S S\5      r " S S\5      r " S S\
5      r " S S\5      r " S S\5      r  " S S\5      r! " S S\5      r" " S S\5      r# " S S\5      r$ " S S \5      r% " S! S"5      r& " S# S$5      r'g)%    Nseekable)RetriesExceededErrorIN_MEMORY_DOWNLOAD_TAG)SubmissionTaskTask)	S3_RETRYABLE_DOWNLOAD_ERRORSCountCallbackInvokerDeferredOpenFileFunctionContainerStreamReaderProgresscalculate_num_partscalculate_range_parameterget_callbacksinvoke_progress_callbacksc                   R    \ rS rSrSrS r\S 5       rS rS r	S r
S rS	 rS
 rSrg)DownloadOutputManager$   a  Base manager class for handling various types of files for downloads

This class is typically used for the DownloadSubmissionTask class to help
determine the following:

    * Provides the fileobj to write to downloads to
    * Get a task to complete once everything downloaded has been written

The answers/implementations differ for the various types of file outputs
that may be accepted. All implementations must subclass and override
public methods from this class.
c                 (    Xl         X l        X0l        g N)_osutil_transfer_coordinator_io_executor)selfosutiltransfer_coordinatorio_executors       S/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/s3transfer/download.py__init__DownloadOutputManager.__init__2   s    %9"'    c                     [        S5      e)a5  Determines if the target for the download is compatible with manager

:param download_target: The target for which the upload will write
    data to.

:param osutil: The os utility to be used for the transfer

:returns: True if the manager can handle the type of target specified
    otherwise returns False.
zmust implement is_compatible()NotImplementedErrorclsdownload_targetr   s      r   is_compatible#DownloadOutputManager.is_compatible7   s     ""BCCr"   c                     g)zGet the tag (if any) to associate all GetObjectTasks

:rtype: s3transfer.futures.TaskTag
:returns: The tag to associate all GetObjectTasks with
N r   s    r   get_download_task_tag+DownloadOutputManager.get_download_task_tagE   s     r"   c                     [        S5      e)zGet file-like object to use for io writes in the io executor

:type transfer_future: s3transfer.futures.TransferFuture
:param transfer_future: The future associated with upload request

returns: A file-like object to write to
z*must implement get_fileobj_for_io_writes()r$   r   transfer_futures     r   get_fileobj_for_io_writes/DownloadOutputManager.get_fileobj_for_io_writesM   s     ""NOOr"   c                 p    U R                   R                  U R                  U R                  XU5      5        g)zQueue IO write for submission to the IO executor.

This method accepts an IO executor and information about the
downloaded data, and handles submitting this to the IO executor.

This method may defer submission to the IO executor if necessary.

N)r   submitr   get_io_write_taskr   fileobjdataoffsets       r   queue_file_io_task(DownloadOutputManager.queue_file_io_taskW   s0     	""))t55gVL	
r"   c                 2    [        U R                  UUUS.S9$ )a  Get an IO write task for the requested set of data

This task can be ran immediately or be submitted to the IO executor
for it to run.

:type fileobj: file-like object
:param fileobj: The file-like object to write to

:type data: bytes
:param data: The data to write out

:type offset: integer
:param offset: The offset to write the data to in the file-like object

:returns: An IO task to be used to write data to a file-like object
)r9   r:   r;   main_kwargs)IOWriteTaskr   r8   s       r   r7   'DownloadOutputManager.get_io_write_taskd   s(    " &&" 
 	
r"   c                     [        S5      e)aq  Get the final io task to complete the download

This is needed because based on the architecture of the TransferManager
the final tasks will be sent to the IO executor, but the executor
needs a final task for it to signal that the transfer is done and
all done callbacks can be run.

:rtype: s3transfer.tasks.Task
:returns: A final task to completed in the io executor
z"must implement get_final_io_task()r$   r-   s    r   get_final_io_task'DownloadOutputManager.get_final_io_task~   s     ""FGGr"   c                     [        USU R                  R                  S9nU R                  R	                  UR
                  5        U$ )Nwb)modeopen_function)r   r   openr   add_failure_cleanupclose)r   filenamefs      r   _get_fileobj_from_filename0DownloadOutputManager._get_fileobj_from_filename   s<    4t||/@/@

 	""66qww?r"   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__r    classmethodr)   r.   r3   r<   r7   rD   rO   __static_attributes__r,   r"   r   r   r   $   sB    (
 D DP

4Hr"   r   c                   J   ^  \ rS rSrU 4S jr\S 5       rS rS rS r	Sr
U =r$ )DownloadFilenameOutputManager   c                 P   > [         TU ]  XU5        S U l        S U l        S U l        g r   )superr    _final_filename_temp_filename_temp_fileobj)r   r   r   r   	__class__s       r   r    &DownloadFilenameOutputManager.__init__   s*    {C#"!r"   c                 "    [        U[        5      $ r   )
isinstancestrr&   s      r   r)   +DownloadFilenameOutputManager.is_compatible   s    /3//r"   c                     UR                   R                  R                  nX l        U R                  R                  U5      U l        U R                  5       U l        U R                  $ r   )	meta	call_argsr9   r]   r   get_temp_filenamer^   _get_temp_fileobjr_   )r   r2   r9   s      r   r3   7DownloadFilenameOutputManager.get_fileobj_for_io_writes   sS    !&&0088&"ll<<WE!335!!!r"   c                 p    [        U R                  U R                  U R                  U R                  S.SS9$ )N)r9   final_filenamer   T)r   r@   is_final)IORenameFileTaskr   r_   r]   r   r-   s    r   rD   /DownloadFilenameOutputManager.get_final_io_task   s=      !%!;!;--"&"6"6,,
 
 	
r"   c                     U R                  U R                  5      nU R                  R                  U R                  R
                  U R                  5        U$ r   )rO   r^   r   rK   r   remove_file)r   rN   s     r   rj   /DownloadFilenameOutputManager._get_temp_fileobj   sH    ++D,?,?@""66LL$$d&9&9	
 r"   )r]   r^   r_   )rQ   rR   rS   rT   r    rV   r)   r3   rD   rj   rW   __classcell__r`   s   @r   rY   rY      s0    " 0 0"
 r"   rY   c                   0    \ rS rSr\S 5       rS rS rSrg)DownloadSeekableOutputManager   c                     [        U5      $ r   r   r&   s      r   r)   +DownloadSeekableOutputManager.is_compatible   s    ((r"   c                 B    UR                   R                  R                  $ r   rg   rh   r9   r1   s     r   r3   7DownloadSeekableOutputManager.get_fileobj_for_io_writes   s    ##--555r"   c                 (    [        U R                  S9$ N)r   CompleteDownloadNOOPTaskr   r-   s    r   rD   /DownloadSeekableOutputManager.get_final_io_task   s     (!%!;!;
 	
r"   r,   N)	rQ   rR   rS   rT   rV   r)   r3   rD   rW   r,   r"   r   rw   rw      s    ) )6
r"   rw   c                   b   ^  \ rS rSr S
U 4S jjr\S 5       rS rS rS r	U 4S jr
S rS	rU =r$ ) DownloadNonSeekableOutputManager   c                    > [         TU ]  XU5        Uc
  [        5       nX@l        [        R
                  " 5       U l        g r   )r\   r    
DeferQueue_defer_queue	threadingLock_io_submit_lockr   r   r   r   defer_queuer`   s        r   r    )DownloadNonSeekableOutputManager.__init__   s6     	{C$,K'(~~/r"   c                     [        US5      $ )Nwrite)hasattrr&   s      r   r)   .DownloadNonSeekableOutputManager.is_compatible   s    00r"   c                     [         $ r   r   r-   s    r   r.   6DownloadNonSeekableOutputManager.get_download_task_tag   s    %%r"   c                 B    UR                   R                  R                  $ r   r|   r1   s     r   r3   :DownloadNonSeekableOutputManager.get_fileobj_for_io_writes   s    ##--555r"   c                 (    [        U R                  S9$ r   r   r-   s    r   rD   2DownloadNonSeekableOutputManager.get_final_io_task   s    '!%!;!;
 	
r"   c                    > U R                      U R                  R                  X25      nU H2  nUS   n[        R	                  SUS   U5        [
        TU ]  XU5        M4     S S S 5        g ! , (       d  f       g = f)Nr:   z%Queueing IO offset %s for fileobj: %sr;   )r   r   request_writesloggerdebugr\   r<   )r   r9   r:   r;   writesr   r`   s         r   r<   3DownloadNonSeekableOutputManager.queue_file_io_task   sk    !!&&55fCFV};(O
 *7&A   "!!s   AA++
A9c                 0    [        U R                  UUS.S9$ )N)r9   r:   r?   )IOStreamingWriteTaskr   r8   s       r   r7   2DownloadNonSeekableOutputManager.get_io_write_task   s#    #&&"
 	
r"   )r   r   r   )rQ   rR   rS   rT   r    rV   r)   r.   r3   rD   r<   r7   rW   rt   ru   s   @r   r   r      s>    EI0 1 1&6


B
 
r"   r   c                   J   ^  \ rS rSr SU 4S jjr\S 5       rS rS rSr	U =r
$ )$DownloadSpecialFilenameOutputManager   c                 4   > [         TU ]  XX45        S U l        g r   )r\   r    _fileobjr   s        r   r    -DownloadSpecialFilenameOutputManager.__init__   s      	+	
 r"   c                 R    [        U[        5      =(       a    UR                  U5      $ r   )rc   rd   is_special_filer&   s      r   r)   2DownloadSpecialFilenameOutputManager.is_compatible  s&    /3/ 
F4J4J5
 	
r"   c                     UR                   R                  R                  nU R                  U5      U l        U R                  $ r   )rg   rh   r9   rO   r   )r   r2   rM   s      r   r3   >DownloadSpecialFilenameOutputManager.get_fileobj_for_io_writes  s5    "''119977A}}r"   c                 D    [        U R                  SSU R                  0S9$ )NTr9   )r   rn   r@   )IOCloseTaskr   r   r-   s    r   rD   6DownloadSpecialFilenameOutputManager.get_final_io_task  s'    !%!;!;"DMM2
 	
r"   )r   r   )rQ   rR   rS   rT   r    rV   r)   r3   rD   rW   rt   ru   s   @r   r   r      s.    EI 
 



 
r"   r   c                   B    \ rS rSrSrS r SS jrS rS rS r	S	 r
S
rg)DownloadSubmissionTaski  z/Task for submitting tasks to execute a downloadc                     [         [        [        [        /nUR                  R
                  R                  nU H  nUR                  XB5      (       d  M  Us  $    [        SU S[        U5       S35      e)a  Retrieves a class for managing output for a download

:type transfer_future: s3transfer.futures.TransferFuture
:param transfer_future: The transfer future for the request

:type osutil: s3transfer.utils.OSUtils
:param osutil: The os utility associated to the transfer

:rtype: class of DownloadOutputManager
:returns: The appropriate class to use for managing a specific type of
    input for downloads.
zOutput z
 of type: z is not supported.)
r   rY   rw   r   rg   rh   r9   r)   RuntimeErrortype)r   r2   r   download_manager_resolver_chainr9   download_manager_clss         r    _get_download_output_manager_cls7DownloadSubmissionTask._get_download_output_manager_cls  sz     1)),	+
' "&&0088$C #11'BB++ %D gYjg7IJ
 	
r"   Nc           
      :   UR                   R                  c  UR                  " SUR                   R                  R                  UR                   R                  R
                  S.UR                   R                  R                  D6nUR                   R                  US   5        U R                  Xc5      " X0R                  U5      n	UR                   R                  UR                  :  a  U R                  UUUUUU	UU5        gU R                  UUUUUU	UU5        g)a  
:param client: The client associated with the transfer manager

:type config: s3transfer.manager.TransferConfig
:param config: The transfer config associated with the transfer
    manager

:type osutil: s3transfer.utils.OSUtil
:param osutil: The os utility associated to the transfer manager

:type request_executor: s3transfer.futures.BoundedExecutor
:param request_executor: The request executor associated with the
    transfer manager

:type io_executor: s3transfer.futures.BoundedExecutor
:param io_executor: The io executor associated with the
    transfer manager

:type transfer_future: s3transfer.futures.TransferFuture
:param transfer_future: The transfer future associated with the
    transfer request that tasks are being submitted for

:type bandwidth_limiter: s3transfer.bandwidth.BandwidthLimiter
:param bandwidth_limiter: The bandwidth limiter to use when
    downloading streams
NBucketKeyContentLengthr,   )rg   sizehead_objectrh   bucketkey
extra_argsprovide_transfer_sizer   r   multipart_threshold_submit_download_request_submit_ranged_download_request)
r   clientconfigr   request_executorr   r2   bandwidth_limiterresponsedownload_output_managers
             r   _submitDownloadSubmissionTask._submit9  s"   H $$, )) &++55<<#((2266 "&&00;;H
   66) #'"G"G#

,,k#; $$v'A'AA)) '!	 00 '!	r"   c	                    UR                   R                  n	UR                  U5      n
[        US5      nUR	                  5       nUR                  5       nU R                  R                  U[        U R                  UU	R                  U	R                  U
U	R                  UUR                  UUR                  US.
U/S9US9  g )Nprogress)
r   r   r   r9   r   	callbacksmax_attemptsr   io_chunksizer   r   r@   done_callbackstag)rg   rh   r3   r   r.   rD   r   r6   ImmediatelyWriteIOGetObjectTaskr   r   r   num_download_attemptsr   )r   r   r   r   r   r   r   r2   r   rh   r9   progress_callbacksget_object_tag
final_tasks                 r   r   /DownloadSubmissionTask._submit_download_request  s     $((22	 *CC

 +?JG 1FFH ->>@
 	""))+%)%?%?$'..$==&"+"6"6!3$*$@$@/F$*$7$7): !+|  % 	* 	
r"   c	                    UR                   R                  n	[        US5      n
UR                  U5      nUR                  n[        UR                   R                  U5      nUR                  5       n[        U R                  Xe5      5      n[        U5       H  n[        UUU5      nSU0nUR                  U	R                  5        UR                  5         U R                  R!                  U[#        U R                  UU	R$                  U	R&                  UUU
UR(                  UU-  UUR*                  US.UR,                  /S9US9  M     UR/                  5         g )Nr   Range)r   r   r   r9   r   r   r   start_indexr   r   r   r   r   )rg   rh   r   r3   multipart_chunksizer   r   r.   r   &_get_final_io_task_submission_callbackranger   updater   	incrementr   r6   GetObjectTaskr   r   r   r   	decrementfinalize)r   r   r   r   r   r   r   r2   r   rh   r   r9   	part_size	num_partsr   finalize_download_invokerirange_parameterr   s                      r   r   6DownloadSubmissionTask._submit_ranged_download_request  si    $((22	 +?JG *CC

 ..	'(<(<(A(A9M	 1FFH %977'%
!
 y!A71iO "?3Ji223%//1&&-- )-)C)C"("+"2"2(}}#*&0%7(.(D(D'(9}3J(.(;(;->! %>$G$G#H" #' .  "B 	"**,r"   c                 b    UR                  5       n[        U R                  R                  X#5      $ r   )rD   r   r   r6   )r   download_managerr   r   s       r   r   =DownloadSubmissionTask._get_final_io_task_submission_callback  s0     &779
 &&--{
 	
r"   c                 B    X!-  nX#S-
  :X  a  SnOXA-   S-
  nSU SU 3nU$ )N    zbytes=-r,   )r   r   
part_indexr   start_range	end_rangerange_params          r   _calculate_range_param-DownloadSubmissionTask._calculate_range_param  s>     ,Q&I#/!3I{m1YK8r"   r,   r   )rQ   rR   rS   rT   rU   r   r   r   r   r   r   rW   r,   r"   r   r   r     s.    9
H KZ0
dE-N
r"   r   c                   (    \ rS rSr  SS jrS rSrg)r   i  Nc           
         Sn[        U5       H  n U
nUR                  " SX#S.UD6n[        US   U5      nU(       a  UR                  UU R                  5      n[        UU	5      nU HG  nU R                  R                  5       (       d$  U R                  UUUU5        U[        U5      -  nMF      g     g   [        U5      e! [         a2  n[        R                  SUUUSS9  Un[        XjW-
  5         SnAM  SnAff = f)a^  Downloads an object and places content into io queue

:param client: The client to use when calling GetObject
:param bucket: The bucket to download from
:param key: The key to download from
:param fileobj: The file handle to write content to
:param exta_args: Any extra arguments to include in GetObject request
:param callbacks: List of progress callbacks to invoke on download
:param max_attempts: The number of retries to do when downloading
:param download_output_manager: The download output manager associated
    with the current download.
:param io_chunksize: The size of each io chunk to read from the
    download stream and queue in the io queue.
:param start_index: The location in the file to start writing the
    content of the key to.
:param bandwidth_limiter: The bandwidth limiter to use when throttling
    the downloading of data in streams.
Nr   BodyzCRetrying exception caught (%s), retrying request, (attempt %s / %s)T)exc_infor,   )r   
get_objectr   get_bandwith_limited_streamr   DownloadChunkIteratordone
_handle_iolenr
   r   r   r   r   )r   r   r   r   r9   r   r   r   r   r   r   r   last_exceptionr   current_indexr   streaming_bodychunkschunkes                       r   _mainGetObjectTask._main  s;   @ |$A/ +!,, !.8 "6V$i" %)EE*D,F,F # /~|L#E  55::<<3#!)	 &U3 $ ? %b #>22# 0 : !   "# *]: !s   BC1C
C>'C99C>c                 (    UR                  X#U5        g r   )r<   )r   r   r9   r
  indexs        r   r  GetObjectTask._handle_iog  s    2275Ir"   r,   )r   N)rQ   rR   rS   rT   r  r  rW   r,   r"   r   r   r     s     R3hJr"   r   c                       \ rS rSrSrS rSrg)r   ik  zGetObjectTask that immediately writes to the provided file object

This is useful for downloads where it is known only one thread is
downloading the object so there is no reason to go through the
overhead of using an IO queue and executor.
c                 6    UR                  X#U5      nU" 5         g r   )r7   )r   r   r9   r
  r  tasks         r   r  *ImmediatelyWriteIOGetObjectTask._handle_ios  s    &88Or"   r,   N)rQ   rR   rS   rT   rU   r  rW   r,   r"   r   r   r   k  s    r"   r   c                       \ rS rSrS rSrg)rA   ix  c                 H    UR                  U5        UR                  U5        g)zPulls off an io queue to write contents to a file

:param fileobj: The file handle to write content to
:param data: The data to write
:param offset: The offset to write the data to.
N)seekr   r8   s       r   r  IOWriteTask._mainy  s     	Vdr"   r,   N)rQ   rR   rS   rT   r  rW   r,   r"   r   rA   rA   x  s    r"   rA   c                       \ rS rSrSrS rSrg)r   i  z/Task for writing data to a non-seekable stream.c                 &    UR                  U5        g)zWrite data to a fileobj.

Data will be written directly to the fileobj without
any prior seeking.

:param fileobj: The fileobj to write content to
:param data: The data to write

N)r   )r   r9   r:   s      r   r  IOStreamingWriteTask._main  s     	dr"   r,   NrQ   rR   rS   rT   rU   r  rW   r,   r"   r   r   r     s
    9
r"   r   c                       \ rS rSrSrS rSrg)ro   i  zA task to rename a temporary file to its final filename

:param fileobj: The file handle that content was written to.
:param final_filename: The final name of the file to rename to
    upon completion of writing the contents.
:param osutil: OS utility
c                 \    UR                  5         UR                  UR                  U5        g r   )rL   rename_filename)r   r9   rm   r   s       r   r  IORenameFileTask._main  s    7<<8r"   r,   Nr  r,   r"   r   ro   ro     s    9r"   ro   c                       \ rS rSrSrS rSrg)r   i  zaA task to close out a file once the download is complete.

:param fileobj: The fileobj to close.
c                 $    UR                  5         g r   )rL   )r   r9   s     r   r  IOCloseTask._main  s    r"   r,   Nr  r,   r"   r   r   r     s    
r"   r   c                   >   ^  \ rS rSrSr    SU 4S jjrS rSrU =r$ )r   i  zA NOOP task to serve as an indicator that the download is complete

Note that the default for is_final is set to True because this should
always be the last task.
c                 (   > [         TU ]  UUUUUS9  g )N)r   r@   pending_main_kwargsr   rn   )r\   r    )r   r   r@   r'  r   rn   r`   s         r   r    !CompleteDownloadNOOPTask.__init__  s&     	!5# 3) 	 	
r"   c                     g r   r,   r-   s    r   r  CompleteDownloadNOOPTask._main  s    r"   r,   )NNNT)	rQ   rR   rS   rT   rU   r    r  rW   rt   ru   s   @r   r   r     s%      
  r"   r   c                   *    \ rS rSrS rS rS r\rSrg)r  i  c                 *    Xl         X l        SU l        g)zIterator to chunk out a downloaded S3 stream

:param body: A readable file-like object
:param chunksize: The amount to read each time
r   N_body
_chunksize
_num_reads)r   body	chunksizes      r   r    DownloadChunkIterator.__init__  s     
#r"   c                     U $ r   r,   r-   s    r   __iter__DownloadChunkIterator.__iter__  s    r"   c                     U R                   R                  U R                  5      nU =R                  S-  sl        U(       a  U$ U R                  S:X  a  U$ [	        5       e)Nr   )r.  readr/  r0  StopIteration)r   r
  s     r   __next__DownloadChunkIterator.__next__  sI    

01L__!
 Lor"   r-  N)	rQ   rR   rS   rT   r    r5  r:  nextrW   r,   r"   r   r  r    s     Dr"   r  c                   $    \ rS rSrSrS rS rSrg)r   i  a  IO queue that defers write requests until they are queued sequentially.

This class is used to track IO data for a *single* fileobj.

You can send data to this queue, and it will defer any IO write requests
until it has the next contiguous block available (starting at 0).

c                 >    / U l         [        5       U l        SU l        g )Nr   )_writesset_pending_offsets_next_offsetr-   s    r   r    DeferQueue.__init__  s     #r"   c                    XR                   :  a  / $ / nXR                  ;   a  / $ [        R                  " U R                  X45        U R                  R                  U5        U R                  (       a  U R                  S   S   U R                   :X  a  [        R                  " U R                  5      nUR                  US   US   S.5        U R                  R                  US   5        U =R                   [        US   5      -  sl         U R                  (       a"  U R                  S   S   U R                   :X  a  M  U$ )a  Request any available writes given new incoming data.

You call this method by providing new data along with the
offset associated with the data.  If that new data unlocks
any contiguous writes that can now be submitted, this
method will return all applicable writes.

This is done with 1 method call so you don't have to
make two method calls (put(), get()) which acquires a lock
each method call.

r   r   )r;   r:   )
rB  rA  heapqheappushr?  addheappopappendremover  )r   r;   r:   r   
next_writes        r   r   DeferQueue.request_writes  s     %%%
 I*** It||f^4!!&)llt||Aq1T5F5FFt||4JMMZ]JqMJK!!((A7Z]!33	 llt||Aq1T5F5FF
 r"   )rB  rA  r?  N)rQ   rR   rS   rT   rU   r    r   rW   r,   r"   r   r   r     s    
 r"   r   )(rE  loggingr   s3transfer.compatr   s3transfer.exceptionsr   s3transfer.futuresr   s3transfer.tasksr   r	   s3transfer.utilsr
   r   r   r   r   r   r   r   r   	getLoggerrQ   r   r   rY   rw   r   r   r   r   r   rA   r   ro   r   r   r  r   r,   r"   r   <module>rT     s       & 6 5 1
 
 
 
		8	$n nb%$9 %P
$9 
",
'< ,
^
+K 
:u^ upVJD VJr
m 
	$ 	4  9t 9$ t 6 </ /r"   