
    Q                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
 d	Zd
ZdZdZ G d de      Z G d de      Zy)z(Helper functions for progress callbacks.    )absolute_import)print_function)division)unicode_literalsN)ProgressMessage)parallelism_framework_utili   i  @   A   c                   "    e Zd ZdZeefdZd Zy)ProgressCallbackWithTimeoutzMakes progress callbacks at least once every _TIMEOUT_SECONDS.

  This prevents wrong throughput calculation while not being excessively
  overwhelming.
  c                     || _         || _        || _        t        j                         | _        || _        d| _        d| _        d| _        y)a  Initializes the callback with timeout.

    Args:
      total_size: Total bytes to process. If this is None, size is not known
          at the outset.
      callback_func: Func of (int: processed_so_far, int: total_bytes)
          used to make callbacks.
      start_bytes_per_callback: Lower bound of bytes per callback.
      timeout: Number maximum of seconds without a callback.

    r   N)	_bytes_per_callback_callback_func_total_sizetime
_last_time_timeout_bytes_processed_since_callback_callbacks_made_total_bytes_processed)self
total_sizecallback_funcstart_bytes_per_callbacktimeouts        *platform/gsutil/gslib/progress_callback.py__init__z$ProgressCallbackWithTimeout.__init__,   sH       8D'D!DiikDODM+,D(D"#D    c                 T   | xj                   |z  c_         t        j                         }| j                   | j                  kD  sN| j                  &| j                  | j                   z   | j                  k\  s| j
                  |z
  | j                  kD  r| xj                  | j                   z  c_        | j                  !t        | j                  | j                        }n| j                  }| j                  || j                         d| _         | xj                  dz  c_	        || _        yy)z@Tracks byte processing progress, making a callback if necessary.Nr   r	   )
r   r   r   r   r   r   r   minr   r   )r   bytes_processedcur_time
bytes_sents       r   Progressz$ProgressCallbackWithTimeout.ProgressE   s    ((O;(yy{H,,t/G/GG				%$*E*E		-	-+.151A1A+B	8	#t}}4
!!T%I%II!
 
			%44d6F6FG
00

*d&6&67-.d*
a do 	5r   N)__name__
__module____qualname____doc___START_BYTES_PER_CALLBACK_TIMEOUT_SECONDSr   r$    r   r   r   r   %   s     )B'	$2!r   r   c                   *    e Zd ZdZ	 	 	 	 	 	 ddZd Zy)FileProgressCallbackHandlerzTracks progress info for large operations like file copy or hash.

      Information is sent to the status_queue, which will print it in the
      UI Thread.
  Nc                 t    || _         || _        || _        || _        || _        || _        || _        d| _        y)aC  Initializes the callback handler.

    Args:
      status_queue: Queue for posting status messages for UI display.
      start_byte: The beginning of the file component, if one is being used.
      override_total_size: The size of the file component, if one is being used.
      src_url: FileUrl/CloudUrl representing the source file.
      component_num: Indicates the component number, if any.
      dst_url: FileUrl/CloudUrl representing the destination file, or None
        for unary operations like hashing.
      operation_name: String representing the operation name
    FN)_status_queue_start_byte_override_total_size_component_num_src_url_dst_url_operation_name_last_byte_written)r   status_queue
start_byteoverride_total_sizesrc_urlcomponent_numdst_urloperation_names           r   r   z$FileProgressCallbackHandler.__init__c   sB    ( &D!D 3D'DDMDM)D#Dr   c                 v   | j                   ry| j                  r| j                  }t        j                  | j                  t        ||| j                  z
  | j                  t        j                         | j                  | j                  | j                               |r|| j                  z
  |k(  rd| _         yyy)a  Gathers information describing the operation progress.

    Actual message is printed to stderr by UIThread.

    Args:
      last_byte_processed: The last byte processed in the file. For file
                           components, this number should be in the range
                           [start_byte:start_byte + override_total_size].
      total_size: Total size of the ongoing operation.
    N)r;   r=   r<   T)r6   r1   r   PutToQueueWithTimeoutr/   r   r0   r3   r   r2   r5   r4   )r   last_byte_processedr   s      r   callz FileProgressCallbackHandler.call   s       ,,j44
+d.>.>>		&*&9&9'+';'; $	/0 )D,<,<<
J $d Kzr   )r   NNNNN)r%   r&   r'   r(   r   rA   r+   r   r   r-   r-   \   s%     #'!"$>%r   r-   )r(   
__future__r   r   r   r   r   gslib.thread_messager   gslib.utilsr   r)   _MAX_BYTES_PER_CALLBACKr*   MAX_PROGRESS_INDICATOR_COLUMNSobjectr   r-   r+   r   r   <module>rH      sV    / & %  '  0 2 ' +   "$ 4!& 4!nD%& D%r   