
    vKg<                         S SK r S SK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)    Nc                   (   ^  \ rS rSrU 4S jrSrU =r$ )RequestExceededException   c                 L   > Xl         X l        SU SU 3n[        TU ]  U5        g)a`  Error when requested amount exceeds what is allowed

The request that raised this error should be retried after waiting
the time specified by ``retry_time``.

:type requested_amt: int
:param requested_amt: The originally requested byte amount

:type retry_time: float
:param retry_time: The length in time to wait to retry for the
    requested amount
zRequest amount z) exceeded the amount available. Retry in N)requested_amt
retry_timesuper__init__)selfr   r   msg	__class__s       T/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/s3transfer/bandwidth.pyr
   !RequestExceededException.__init__   s2     +$.WXbWcd    r   r   )__name__
__module____qualname____firstlineno__r
   __static_attributes____classcell__)r   s   @r   r   r      s     r   r   c                       \ rS rSrSrSrg)RequestToken%   zDA token to pass as an identifier when consuming from the LeakyBucket N)r   r   r   r   __doc__r   r   r   r   r   r   %   s    Nr   r   c                        \ rS rSrS rS rSrg)	TimeUtils+   c                 ,    [         R                   " 5       $ )zOGet the current time back

:rtype: float
:returns: The current time in seconds
)timer   s    r   r!   TimeUtils.time,   s     yy{r   c                 .    [         R                  " U5      $ )z_Sleep for a designated time

:type value: float
:param value: The time to sleep for in seconds
)r!   sleep)r   values     r   r%   TimeUtils.sleep4   s     zz%  r   r   N)r   r   r   r   r!   r%   r   r   r   r   r   r   +   s    !r   r   c                   *    \ rS rSrSS jr SS jrSrg)BandwidthLimiter=   Nc                 B    Xl         X l        Uc  [        5       U l        gg)zLimits bandwidth for shared S3 transfers

:type leaky_bucket: LeakyBucket
:param leaky_bucket: The leaky bucket to use limit bandwidth

:type time_utils: TimeUtils
:param time_utils: Time utility to use for interacting with time.
N)_leaky_bucket_time_utilsr   )r   leaky_bucket
time_utilss      r   r
   BandwidthLimiter.__init__>   s%     *%({D r   c                 t    [        XR                  X R                  5      nU(       d  UR                  5         U$ )a  Wraps a fileobj in a bandwidth limited stream wrapper

:type fileobj: file-like obj
:param fileobj: The file-like obj to wrap

:type transfer_coordinator: s3transfer.futures.TransferCoordinator
param transfer_coordinator: The coordinator for the general transfer
    that the wrapped stream is a part of

:type enabled: boolean
:param enabled: Whether bandwidth limiting should be enabled to start
)BandwidthLimitedStreamr,   r-   disable_bandwidth_limiting)r   fileobjtransfer_coordinatorenabledstreams        r   get_bandwith_limited_stream,BandwidthLimiter.get_bandwith_limited_streamL   s6     ('')=?O?O
 --/r   )r,   r-   N)T)r   r   r   r   r
   r8   r   r   r   r   r)   r)   =   s    + 6:r   r)   c                   h    \ rS rSr  SS jrS rS rS rS rS r	S	 r
SS
 jrS rS rS rS rSrg)r2   c   Nc                     Xl         X l        X0l        X@l        Uc  [	        5       U l        SU l        [        5       U l        SU l        XPl	        g)a  Limits bandwidth for reads on a wrapped stream

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

:type leaky_bucket: LeakyBucket
:param leaky_bucket: The leaky bucket to use to throttle reads on
    the stream

:type transfer_coordinator: s3transfer.futures.TransferCoordinator
param transfer_coordinator: The coordinator for the general transfer
    that the wrapped stream is a part of

:type time_utils: TimeUtils
:param time_utils: The time utility to use for interacting with time
NTr   )
_fileobjr,   _transfer_coordinatorr-   r   _bandwidth_limiting_enabledr   _request_token_bytes_seen_bytes_threshold)r   r4   r.   r5   r/   bytes_thresholds         r   r
   BandwidthLimitedStream.__init__d   sK    0  )%9"%({D+/(*n /r   c                     SU l         g)z0Enable bandwidth limiting on reads to the streamTNr@   r"   s    r   enable_bandwidth_limiting0BandwidthLimitedStream.enable_bandwidth_limiting   s
    +/(r   c                     SU l         g)z1Disable bandwidth limiting on reads to the streamFNrG   r"   s    r   r3   1BandwidthLimitedStream.disable_bandwidth_limiting   s
    +0(r   c                 D   U R                   (       d  U R                  R                  U5      $ U =R                  U-  sl        U R                  U R                  :  a  U R                  R                  U5      $ U R                  5         U R                  R                  U5      $ )zXRead a specified amount

Reads will only be throttled if bandwidth limiting is enabled.
)r@   r>   readrB   rC   _consume_through_leaky_bucket)r   amounts     r   rM   BandwidthLimitedStream.read   s    
 //==%%f-- 	F"d333==%%f--**,}}!!&))r   c                    U R                   R                  (       d9   U R                  R                  U R                  U R
                  5        SU l        g U R                   R                  e! [         a/  nU R                  R                  UR                  5         S nAOS nAff = fU R                   R                  (       d  M  Np)Nr   )
r?   	exceptionr,   consumerB   rA   r   r-   r%   r   )r   es     r   rN   4BandwidthLimitedStream._consume_through_leaky_bucket   s     ,,665""**$$d&9&9 $%  ,,666 , 5  &&q||445 ,,666s   7A+ +
B$5%BB$c                 $    U R                  5         g)z6Signal that data being read is being transferred to S3N)rH   r"   s    r   signal_transferring*BandwidthLimitedStream.signal_transferring   s    &&(r   c                 $    U R                  5         g)z:Signal that data being read is not being transferred to S3N)r3   r"   s    r   signal_not_transferring.BandwidthLimitedStream.signal_not_transferring   s    '')r   c                 :    U R                   R                  X5        g r:   )r>   seek)r   wherewhences      r   r]   BandwidthLimitedStream.seek   s    5)r   c                 6    U R                   R                  5       $ r:   )r>   tellr"   s    r   rb   BandwidthLimitedStream.tell   s    }}!!##r   c                     U R                   (       a!  U R                  (       a  U R                  5         U R                  R	                  5         g r:   )r@   rB   rN   r>   closer"   s    r   re   BandwidthLimitedStream.close   s1    ++0@0@ ..0r   c                     U $ r:   r   r"   s    r   	__enter__ BandwidthLimitedStream.__enter__   s    r   c                 $    U R                  5         g r:   )re   )r   argskwargss      r   __exit__BandwidthLimitedStream.__exit__   s    

r   )r@   rB   rC   r>   r,   rA   r-   r?   )Ni   )r   )r   r   r   r   r
   rH   r3   rM   rN   rW   rZ   r]   rb   re   rh   rm   r   r   r   r   r2   r2   c   sG     "!0F01*(7$)**$r   r2   c                   B    \ rS rSr   S
S jrS rS rS rS rS r	S	r
g)LeakyBucket   Nc                     [        U5      U l        X l        Uc  [        5       U l        [        R
                  " 5       U l        X0l        Uc  [        5       U l        X@l	        Uc  [        5       U l	        gg)a  A leaky bucket abstraction to limit bandwidth consumption

:type rate: int
:type rate: The maximum rate to allow. This rate is in terms of
    bytes per second.

:type time_utils: TimeUtils
:param time_utils: The time utility to use for interacting with time

:type rate_tracker: BandwidthRateTracker
:param rate_tracker: Tracks bandwidth consumption

:type consumption_scheduler: ConsumptionScheduler
:param consumption_scheduler: Schedules consumption retries when
    necessary
N)float	_max_rater-   r   	threadingLock_lock_rate_trackerBandwidthRateTracker_consumption_schedulerConsumptionScheduler)r   max_rater/   rate_trackerconsumption_schedulers        r   r
   LeakyBucket.__init__   sf    . x%({D^^%
)!5!7D&;# (*>*@D' )r   c                    U R                      U R                  R                  5       nU R                  R	                  U5      (       a  U R                  XU5      sSSS5        $ U R                  X5      (       a  U R                  XU5        OU R                  X5      sSSS5        $  SSS5        g! , (       d  f       g= f)a  Consume an a requested amount

:type amt: int
:param amt: The amount of bytes to request to consume

:type request_token: RequestToken
:param request_token: The token associated to the consumption
    request that is used to identify the request. So if a
    RequestExceededException is raised the token should be used
    in subsequent retry consume() request.

:raises RequestExceededException: If the consumption amount would
    exceed the maximum allocated bandwidth

:rtype: int
:returns: The amount consumed
N)	rw   r-   r!   rz   is_scheduled,_release_requested_amt_for_scheduled_request_projected_to_exceed_max_rate!_raise_request_exceeded_exception_release_requested_amtr   amtrequest_tokentime_nows       r   rS   LeakyBucket.consume   s    $ ZZ'',,.H**77FFHH Z 33CBB66 223A Z ZZs   AB0#9B00
B>c                 T    U R                   R                  X5      nX0R                  :  $ r:   )rx   get_projected_ratert   )r   r   r   projected_rates       r   r   )LeakyBucket._projected_to_exceed_max_rate  s%    ++>>sM..r   c                 Z    U R                   R                  U5        U R                  X5      $ r:   )rz   process_scheduled_consumptionr   r   s       r   r   8LeakyBucket._release_requested_amt_for_scheduled_request  s.     	##AA	
 **399r   c                 |    U[        U R                  5      -  nU R                  R                  XU5      n[	        XS9e)Nr   )rs   rt   rz   schedule_consumptionr   )r   r   r   r   allocated_timer   s         r   r   -LeakyBucket._raise_request_exceeded_exception#  sB    uT^^4400EE

 '
 	
r   c                 <    U R                   R                  X5        U$ r:   )rx   record_consumption_rate)r   r   r   s      r   r   "LeakyBucket._release_requested_amt,  s    223A
r   )rz   rw   rt   rx   r-   )NNN)r   r   r   r   r
   rS   r   r   r   r   r   r   r   r   rp   rp      s.     "!AFB>/:
r   rp   c                   ,    \ rS rSrS rS rS rS rSrg)r{   i1  c                      0 U l         SU l        g)z*Schedules when to consume a desired amountr   N _tokens_to_scheduled_consumption_total_waitr"   s    r   r
   ConsumptionScheduler.__init__2  s    02-r   c                     XR                   ;   $ )zIndicates if a consumption request has been scheduled

:type token: RequestToken
:param token: The token associated to the consumption
    request that is used to identify the request.
)r   )r   tokens     r   r   !ConsumptionScheduler.is_scheduled7  s     ====r   c                 |    U =R                   U-  sl         U R                   US.U R                  U'   U R                   $ )aL  Schedules a wait time to be able to consume an amount

:type amt: int
:param amt: The amount of bytes scheduled to be consumed

:type token: RequestToken
:param token: The token associated to the consumption
    request that is used to identify the request.

:type time_to_consume: float
:param time_to_consume: The desired time it should take for that
    specific request amount to be consumed in regardless of previously
    scheduled consumption requests

:rtype: float
:returns: The amount of time to wait for the specific request before
    actually consuming the specified amount.
)wait_durationtime_to_consume)r   r   )r   r   r   r   s       r   r   )ConsumptionScheduler.schedule_consumption@  sA    & 	O+!--.8
--e4 r   c                 |    U R                   R                  U5      n[        U R                  US   -
  S5      U l        g)zProcesses a scheduled consumption request that has completed

:type token: RequestToken
:param token: The token associated to the consumption
    request that is used to identify the request.
r   r   N)r   popmaxr   )r   r   scheduled_retrys      r   r   2ConsumptionScheduler.process_scheduled_consumptionZ  s=     ??CCEJ/@AA1
r   r   N)	r   r   r   r   r
   r   r   r   r   r   r   r   r{   r{   1  s    
> 4

r   r{   c                   F    \ rS rSrS
S jr\S 5       rS rS rS r	S r
Srg	)ry   ig  c                 ,    Xl         SU l        SU l        g)aZ  Tracks the rate of bandwidth consumption

:type a: float
:param a: The constant to use in calculating the exponentional moving
    average of the bandwidth rate. Specifically it is used in the
    following calculation:

    current_rate = alpha * new_rate + (1 - alpha) * current_rate

    This value of this constant should be between 0 and 1.
N)_alpha
_last_time_current_rate)r   alphas     r   r
   BandwidthRateTracker.__init__h  s     !r   c                 6    U R                   c  gU R                  $ )zUThe current transfer rate

:rtype: float
:returns: The current tracked transfer rate
        )r   r   r"   s    r   current_rate!BandwidthRateTracker.current_ratex  s     ??"!!!r   c                 @    U R                   c  gU R                  X5      $ )a"  Get the projected rate using a provided amount and time

:type amt: int
:param amt: The proposed amount to consume

:type time_at_consumption: float
:param time_at_consumption: The proposed time to consume at

:rtype: float
:returns: The consumption rate if that amt and time were consumed
r   )r   *_calculate_exponential_moving_average_rater   r   time_at_consumptions      r   r   'BandwidthRateTracker.get_projected_rate  s'     ??">>
 	
r   c                 r    U R                   c  X l         SU l        gU R                  X5      U l        X l         g)zRecord the consumption rate based off amount and time point

:type amt: int
:param amt: The amount that got consumed

:type time_at_consumption: float
:param time_at_consumption: The time at which the amount was consumed
Nr   )r   r   r   r   s      r   r   ,BandwidthRateTracker.record_consumption_rate  s;     ??"1O!$D!LL
 .r   c                 H    X R                   -
  nUS::  a  [        S5      $ X-  $ )Nr   inf)r   rs   )r   r   r   
time_deltas       r   _calculate_rate$BandwidthRateTracker._calculate_rate  s*    (??:
?
 <!!r   c                 |    U R                  X5      nU R                  U-  SU R                  -
  U R                  -  -   $ )N   )r   r   r   )r   r   r   new_rates       r   r   ?BandwidthRateTracker._calculate_exponential_moving_average_rate  s;     ''A{{X%T[[D<N<N(NNNr   )r   r   r   N)g?)r   r   r   r   r
   propertyr   r   r   r   r   r   r   r   r   ry   ry   g  s0    "  " "
$.$"Or   ry   )ru   r!   	Exceptionr   r   r   r)   r2   rp   r{   ry   r   r   r   <module>r      sk     y (	 	! !$# #Ln nbZ Zz3
 3
lNO NOr   