
    6                     &   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Zddlm	Z
 ddlmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)zAThread message classes.

Messages are added to the status queue.
    )absolute_import)print_function)division)unicode_literalsN)Error)errorc                       e Zd ZdZddZd Zy)StatusMessagezXGeneral StatusMessage class.

  All Message classes inherit this StatusMessage class.
  Nc                     || _         |xs t        j                         | _        |xs t	        j
                         j                  | _        y)a  Creates a Message.

    Args:
      message_time: Time that this message was created (since Epoch).
      process_id: Process ID that produced this message (overridable for
          testing).
      thread_id: Thread ID that produced this message (overridable for testing).
    N)timeosgetpid
process_id	threadingcurrent_threadident	thread_id)selfmessage_timer   r   s       'platform/gsutil/gslib/thread_message.py__init__zStatusMessage.__init__&   s8     DI /BIIKDOB)":":"<"B"BDN    c                     | j                   j                  d| j                  d| j                  d| j                  dS );Returns a string with a valid constructor for this message.(, process_id=, thread_id=))	__class____name__r   r   r   r   s    r   __str__zStatusMessage.__str__3   s/     
	 	 $))T__dnn	NOr   )NN)r    
__module____qualname____doc__r   r"    r   r   r
   r
       s    
COr   r
   c                   2     e Zd ZdZ	 	 	 	 d fd	Zd Z xZS )RetryableErrorMessagezMessage class for retryable errors encountered by the JSON API.

  This class contains information about the retryable error encountered to
  report to analytics collection and to display in the UI.
  c                 j   t         t        |   |||       |j                  j                  | _        |j                  j                  dv r,d|j                  j                  j                         z   | _        t        |t              st        |t              rd| _        nd| _        || _        || _        y)a  Creates a RetryableErrorMessage.

    Args:
      exception: The retryable error that was thrown.
      message_time: Float representing when message was created (seconds since
          Epoch).
      num_retries: The number of retries consumed so far.
      total_wait_sec: The total amount of time waited so far in retrying.
      process_id: Process ID that produced this message (overridable for
          testing).
      thread_id: Thread ID that produced this message (overridable for testing).
    r   r   )socket_socketSocketTFN)superr(   r   r   r    
error_typer#   
capitalize
isinstanceapitools_service_errorsix_service_erroris_service_errornum_retriestotal_wait_sec)r   	exceptionr   r5   r6   r   r   r   s          r   r   zRetryableErrorMessage.__init__A   s    & 

/;E:C 0 E  ))22DO %%)>> 9#6#6#?#?#J#J#LLdo9459/0"d#d #D )Dr   c                     | j                   j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d| j                  dS )r   r   z(), num_retries=z, total_wait_sec=z, time=r   r   r   )r   r    r/   r5   r6   r   r   r   r!   s    r   r"   zRetryableErrorMessage.__str__k   sF     ^^$$doot7G7G  $))T__dnnN Or   )r   r   NNr    r#   r$   r%   r   r"   __classcell__r   s   @r   r(   r(   :   s"     ()TOr   r(   c                   "     e Zd ZdZ fdZ xZS )FinalMessagezbCreates a FinalMessage.

  A FinalMessage simply indicates that we have finished our operation.
  c                 ,    t         t        |   |       y)zCreates a FinalMessage.

    Args:
      message_time: Float representing when message was created (seconds since
          Epoch).
    N)r.   r=   r   r   r   r   s     r   r   zFinalMessage.__init__y   s     
,&|4r   r    r#   r$   r%   r   r:   r;   s   @r   r=   r=   s   s    
5 5r   r=   c                   "     e Zd ZdZ fdZ xZS )MetadataMessagezCreates a MetadataMessage.

  A MetadataMessage simply indicates that a metadata operation on a given object
  has been successfully done. The only passed argument is the time when such
  operation has finished.
  c                 ,    t         t        |   |       y)zCreates a MetadataMessage.

    Args:
      message_time: Float representing when message was created (seconds since
          Epoch).
    N)r.   rB   r   r?   s     r   r   zMetadataMessage.__init__   s     
/4),7r   r@   r;   s   @r   rB   rB      s    8 8r   rB   c                   d     e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZ	 	 	 	 	 	 	 d fd	Zd Z xZS )FileMessagezMarks start or end of an operation for a file, cloud object or component.

  This class contains general information about each file/component. With that,
  information such as total size and estimated time remaining may be calculated
  beforehand.
                          	   
      c                     t         t        |   ||	|
       || _        || _        || _        || _        || _        || _        || _	        y)a  Creates a FileMessage.

    Args:
      src_url: FileUrl/CloudUrl representing the source file.
      dst_url: FileUrl/CloudUrl representing the destination file.
      message_time: Float representing when message was created (seconds since
          Epoch).
      size: Total size of this file/component, in bytes.
      finished: Boolean to indicate whether this is starting or finishing
          a file/component transfer.
      component_num: Component number, if dealing with a component.
      message_type: Type of the file/component.
      bytes_already_downloaded: Specific field for resuming downloads. When
          starting a component download, it tells how many bytes were already
          downloaded.
      process_id: Process ID that produced this message (overridable for
          testing).
      thread_id: Thread ID that produced this message (overridable for testing).
    r*   N)
r.   rE   r   src_urldst_urlsizecomponent_numfinishedmessage_typebytes_already_downloaded)r   rR   rS   r   rT   rV   rU   rW   rX   r   r   r   s              r   r   zFileMessage.__init__   sW    > 
+t%l1;09 & ; DLDLDI&DDM$D$<D!r   c                 8   | j                   j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d| j                  d| j                  d| j                  d	| j                  d
| j                  dS )r   z('z', '', z, size=, finished=, component_num=z, message_type=z, bytes_already_downloaded=r   r   r   )r   r    rR   rS   r   rT   rV   rU   rW   rX   r   r   r!   s    r   r"   zFileMessage.__str__   sa    
 ^^$$dllDLL$))YYt'9'94;L;L**DOOT^^M Nr   )NFNNNNN)r    r#   r$   r%   FILE_DOWNLOADFILE_UPLOADFILE_CLOUD_COPYFILE_LOCAL_COPYFILE_DAISY_COPYFILE_REWRITE	FILE_HASHCOMPONENT_TO_UPLOADEXISTING_COMPONENTCOMPONENT_TO_DOWNLOADEXISTING_OBJECT_TO_DELETEr   r"   r:   r;   s   @r   rE   rE      sj     -+///,)   ! (,(=TNr   rE   c                   4     e Zd ZdZ	 	 	 	 	 d fd	Zd Z xZS )ProgressMessagezMessage class for a file/object/component progress.

  This class contains specific information about the current progress of a file,
  cloud object or single component.
  c
                     t         t        |   |       || _        || _        || _        || _        || _        ||k(  | _        || _	        y)aa  Creates a ProgressMessage.

    Args:
      size: Integer for total size of this file/component, in bytes.
      processed_bytes: Integer for number of bytes already processed from that
          specific component, which means processed_bytes <= size.
      src_url: FileUrl/CloudUrl representing the source file.
      message_time: Float representing when message was created (seconds since
          Epoch).
      dst_url: FileUrl/CloudUrl representing the destination file, or None
          for unary operations like hashing.
      component_num: Indicates the component number, if any.
      operation_name: Name of the operation that is being made over that
          component.
      process_id: Process ID that produced this message (overridable for
          testing).
      thread_id: Thread ID that produced this message (overridable for testing).
    N)
r.   ri   r   rT   processed_bytesrU   rR   rS   rV   operation_name)r   rT   rk   rR   r   rS   rU   rl   r   r   r   s             r   r   zProgressMessage.__init__   sM    8 
/4),7DI*D&DDLDL_,DM(Dr   c                 j   | j                   rd| j                   z  nd}| j                  rd| j                  z  nd}| j                  j                  d| j                  d| j
                  d| j                  d| j                  d|d| j                  d	|d
| j                  d| j                  dS )r   z'%s'Nr   , z, 'rZ   z
, dst_url=r\   z, operation_name=r   r   r   )rS   rl   r   r    rT   rk   rR   r   rU   r   r   )r   dst_url_stringoperation_name_strings      r   r"   zProgressMessage.__str__  s     37,,h-DN+/+>+>4&&&D  ^^$$dii1E1E\\499nd6H6H"DOOT^^	E Fr   )NNNNNr9   r;   s   @r   ri   ri      s%     !"#)J
Fr   ri   c                   (     e Zd ZdZ fdZd Z xZS )SeekAheadMessagea  Message class for results obtained by SeekAheadThread().

  It estimates the number of objects and total size in case the task_queue
  cannot hold all tasks at once (only used in large operations).
  This class contains information about all the objects yet to be processed.
  c                 H    t         t        |   |       || _        || _        y)aI  Creates a SeekAheadMessage.

    Args:
      num_objects: Number of total objects that the SeekAheadThread estimates.
      size: Total size corresponding to the sum of the size of objects iterated
          by SeekAheadThread.
      message_time: Float representing when message was created (seconds since
          Epoch).
    N)r.   rr   r   num_objectsrT   )r   rt   rT   r   r   s       r   r   zSeekAheadMessage.__init__$  s$     

D*<8"DDIr   c                     | j                   j                  d| j                  d| j                  d| j                  d| j
                  d| j                  dS )r   r   rn   r   r   r   )r   r    rt   rT   r   r   r   r!   s    r   r"   zSeekAheadMessage.__str__2  s<     ^^$$d&6&6		499__dnn. /r   r9   r;   s   @r   rr   rr     s    /r   rr   c                   *     e Zd ZdZd fd	Zd Z xZS )ProducerThreadMessagea%  Message class for results obtained by calculations made on ProducerThread.

  It estimates the number of objects and total size currently dealty by
  task_queue. If the task_queue cannot support all objects at once, the
  SeekAheadThread will be responsible for sending an accurate message.
  c                 V    t         t        |   |       || _        || _        || _        y)a^  Creates a SeekAheadMessage.

    Args:
      num_objects: Number of total objects that the task_queue has.
      size: Total size corresponding to the sum of the size of objects iterated
          by the task_queue
      message_time: Float representing when message was created (seconds since
          Epoch).
      finished: Boolean to indicate whether this is the final message from the
          ProducerThread. The difference is that this message displays
          the correct total size and number of objects, whereas the
          previous ones were periodic (on the number of files) updates.
    N)r.   rw   r   rt   rT   rV   )r   rt   rT   r   rV   r   s        r   r   zProducerThreadMessage.__init__A  s+     

/="DDIDMr   c           
          | j                   j                  d| j                  d| j                  d| j                  d| j
                  d
S )r   r   rn   r[   r   )r   r    rt   rT   r   rV   r!   s    r   r"   zProducerThreadMessage.__str__T  s6     ^^$$d&6&6		499]] r   )Fr9   r;   s   @r   rw   rw   9  s    &r   rw   c                   (     e Zd ZdZ fdZd Z xZS )PerformanceSummaryMessagezMessage class to log PerformanceSummary parameters.

  This class acts as a relay between a multiprocess/multithread situation and
  the global status queue, from which the PerformanceSummary info gets consumed.
  c                 @    t         t        |   |dd       || _        y)zCreates a PerformanceSummaryMessage.

    Args:
      message_time: Float representing when message was created (seconds since
          Epoch).
      uses_slice: True if the command uses slice parallelism.
    Nr*   )r.   r{   r   
uses_slice)r   r   r}   r   s      r   r   z"PerformanceSummaryMessage.__init__b  s,     

#T3L?C>B 4 D !DOr   c                 h    | j                   j                  d| j                  d| j                  dS )r   r   rn   r   )r   r    r   r}   r!   s    r   r"   z!PerformanceSummaryMessage.__str__o  s)     ^^$$diiB Cr   r9   r;   s   @r   r{   r{   [  s    !Cr   r{   )r%   
__future__r   r   r   r   r   r   apitools.base.py.exceptionsr   r2   six.moves.http_clientr   r3   objectr
   r(   r=   rB   rE   ri   rr   rw   r{   r&   r   r   <module>r      s   
 ' %  ' 	  G <OF O46OM 6Or5= 5 8m 8$KN- KN\6Fm 6Fr/} /:M DC Cr   