U
    O8c!5                     @   st   d dl Z d dlZd dlmZmZmZmZ d dlmZm	Z	m
Z
mZ G dd deZG dd deZG dd	 d	eZdS )
    N)CompleteMultipartUploadTaskCreateMultipartUploadTaskSubmissionTaskTask)ChunksizeAdjustercalculate_range_parameterget_callbacksget_filtered_dictc                   @   s   e Zd ZdZdddddddd	d
d	Zddddddddddd	d
gZdddddddddg	Zd	d
gZdd Zdd Z	dd Z
dd Zdd Zdd  Zd!d" Zd#S )$CopySubmissionTaskz+Task for submitting tasks to execute a copyZIfMatchZIfModifiedSinceZIfNoneMatchZIfUnmodifiedSinceZSSECustomerKeyZSSECustomerAlgorithmZSSECustomerKeyMD5RequestPayerExpectedBucketOwner)	CopySourceIfMatchCopySourceIfModifiedSinceCopySourceIfNoneMatchCopySourceIfUnmodifiedSinceCopySourceSSECustomerKeyCopySourceSSECustomerAlgorithmCopySourceSSECustomerKeyMD5r   r   r   r   r   r   r   r   r   ZMetadataDirectiveZTaggingDirectivec                 C   s   |j jdkrn|j j}| |j}|j}| D ] \}	}
|	| jkr.|
|| j|	 < q.|jj	f |}|j 
|d  |j j|jk r| ||||| n| ||||| dS )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 transfer_future: s3transfer.futures.TransferFuture
        :param transfer_future: The transfer future associated with the
            transfer request that tasks are being submitted for
        NZContentLength)metasize	call_args)_get_head_object_request_from_copy_sourcecopy_source
extra_argsitemsEXTRA_ARGS_TO_HEAD_ARGS_MAPPINGZsource_clientZhead_objectZprovide_transfer_sizeZmultipart_threshold_submit_copy_request_submit_multipart_request)selfclientconfigosutilrequest_executortransfer_futurer   Zhead_object_requestr   paramvalueresponse r'   5/tmp/pip-unpacked-wheel-6hpttf6a/s3transfer/copies.py_submitJ   sD    
        zCopySubmissionTask._submitc                 C   sL   |j j}t|d}| j|t| j||j|j|j|j	||j j
ddd d S )Nprogress)r   r   bucketkeyr   	callbacksr   T)transfer_coordinatormain_kwargsis_final)r   r   r   _transfer_coordinatorsubmitCopyObjectTaskr   r+   r,   r   r   )r   r   r    r!   r"   r#   r   progress_callbacksr'   r'   r(   r      s"    
	z'CopySubmissionTask._submit_copy_requestc                 C   sj  |j j}i }|j D ]\}}	|| jkr|	||< q| j|t| j||j|j	|dd}
|j
}t }|||j j}tt|j jt| }g }t|d}td|d D ]~}| |j}t||d ||j j|d< | ||d ||j j}|| j|t| j||j|j|j	||||dd|
id q| |j}| j|t| j||j|j	|d|
|d	d
d d S )N)r   r+   r,   r   )r.   r/   r*      ZCopySourceRange)r   r   r+   r,   part_numberr   r-   r   	upload_id)r.   r/   pending_main_kwargs)r7   partsT)r.   r/   r8   r0   )r   r   r   r   CREATE_MULTIPART_ARGS_BLACKLISTr1   r2   r   r+   r,   Zmultipart_chunksizer   Zadjust_chunksizer   intmathceilfloatr   range_extra_upload_part_argsr   _get_transfer_sizeappendCopyPartTaskr   _extra_complete_multipart_argsr   )r   r   r    r!   r"   r#   r   Zcreate_multipart_extra_argsr$   valZcreate_multipart_future	part_sizeZadjuster	num_partsZpart_futuresr4   r6   Zextra_part_argsr   Zcomplete_multipart_extra_argsr'   r'   r(   r      s    

 
 z,CopySubmissionTask._submit_multipart_requestc                 C   s,   t |trt|S td|t|f d S )NzZExpecting dictionary formatted: {"Bucket": bucket_name, "Key": key} but got %s or type %s.)
isinstancedictcopy	TypeErrortype)r   r   r'   r'   r(   r     s    


z<CopySubmissionTask._get_head_object_request_from_copy_sourcec                 C   s   t || jS N)r	   UPLOAD_PART_COPY_ARGSr   r   r'   r'   r(   r@     s    z*CopySubmissionTask._extra_upload_part_argsc                 C   s   t || jS rM   )r	   COMPLETE_MULTIPART_ARGSrO   r'   r'   r(   rD     s    z1CopySubmissionTask._extra_complete_multipart_argsc                 C   s   ||d kr|||  S |S )Nr5   r'   )r   rF   Z
part_indexrG   Ztotal_transfer_sizer'   r'   r(   rA     s    z%CopySubmissionTask._get_transfer_sizeN)__name__
__module____qualname____doc__r   rN   r:   rP   r)   r   r   r   r@   rD   rA   r'   r'   r'   r(   r
      sT   >i
r
   c                   @   s   e Zd ZdZdd ZdS )r3   zTask to do a nonmultipart copyc           	      C   s0   |j f |||d| |D ]}||d qdS )a  
        :param client: The client to use when calling PutObject
        :param copy_source: The CopySource parameter to use
        :param bucket: The name of the bucket to copy to
        :param key: The name of the key to copy to
        :param extra_args: A dictionary of any extra arguments that may be
            used in the upload.
        :param callbacks: List of callbacks to call after copy
        :param size: The size of the transfer. This value is passed into
            the callbacks

        )
CopySourceBucketKeyZbytes_transferredN)Zcopy_object)	r   r   r   r+   r,   r   r-   r   callbackr'   r'   r(   _main*  s      zCopyObjectTask._mainNrQ   rR   rS   rT   rZ   r'   r'   r'   r(   r3   '  s   r3   c                   @   s   e Zd ZdZdd ZdS )rC   z)Task to upload a part in a multipart copyc
                 C   sF   |j f |||||d|}
|D ]}||	d q |
d d }||dS )a  
        :param client: The client to use when calling PutObject
        :param copy_source: The CopySource parameter to use
        :param bucket: The name of the bucket to upload to
        :param key: The name of the key to upload to
        :param upload_id: The id of the upload
        :param part_number: The number representing the part of the multipart
            upload
        :param extra_args: A dictionary of any extra arguments that may be
            used in the upload.
        :param callbacks: List of callbacks to call after copy part
        :param size: The size of the transfer. This value is passed into
            the callbacks

        :rtype: dict
        :returns: A dictionary representing a part::

            {'Etag': etag_value, 'PartNumber': part_number}

            This value can be appended to a list to be used to complete
            the multipart upload.
        )rU   rV   rW   ZUploadId
PartNumberrX   ZCopyPartResultETag)r]   r\   )Zupload_part_copy)r   r   r   r+   r,   r7   r6   r   r-   r   r&   rY   etagr'   r'   r(   rZ   C  s    "zCopyPartTask._mainNr[   r'   r'   r'   r(   rC   @  s   rC   )rJ   r<   Zs3transfer.tasksr   r   r   r   Zs3transfer.utilsr   r   r   r	   r
   r3   rC   r'   r'   r'   r(   <module>   s     