U
    O8c??                     @   sh   d dl Z d dlZd dlmZ eeZG dd dZG dd deZG dd deZ	G d	d
 d
eZ
dS )    N)get_callbacksc                   @   sv   e Zd ZdZdddZdd Zedd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )TaskzA task associated to a TransferFuture request

    This is a base class for other classes to subclass from. All subclassed
    classes must implement the main() method.
    NFc                 C   sP   || _ || _| jdkri | _|| _|dkr0i | _|| _| jdkrFg | _|| _dS )a  
        :type transfer_coordinator: s3transfer.futures.TransferCoordinator
        :param transfer_coordinator: The context associated to the
            TransferFuture for which this Task is associated with.

        :type main_kwargs: dict
        :param main_kwargs: The keyword args that can be immediately supplied
            to the _main() method of the task

        :type pending_main_kwargs: dict
        :param pending_main_kwargs: The keyword args that are depended upon
            by the result from a dependent future(s). The result returned by
            the future(s) will be used as the value for the keyword argument
            when _main() is called. The values for each key can be:
                * a single future - Once completed, its value will be the
                  result of that single future
                * a list of futures - Once all of the futures complete, the
                  value used will be a list of each completed future result
                  value in order of when they were originally supplied.

        :type done_callbacks: list of callbacks
        :param done_callbacks: A list of callbacks to call once the task is
            done completing. Each callback will be called with no arguments
            and will be called no matter if the task succeeds or an exception
            is raised.

        :type is_final: boolean
        :param is_final: True, to indicate that this task is the final task
            for the TransferFuture request. By setting this value to True, it
            will set the result of the entire TransferFuture to the result
            returned by this task's main() method.
        N)_transfer_coordinator_main_kwargs_pending_main_kwargs_done_callbacks	_is_final)selfZtransfer_coordinatorZmain_kwargsZpending_main_kwargsZdone_callbacksis_final r   4/tmp/pip-unpacked-wheel-6hpttf6a/s3transfer/tasks.py__init__   s    (

zTask.__init__c                 C   s6   dddddddg}|  | j|}d| jj| jj|S )	NbucketkeyZpart_numberZfinal_filenametransfer_futureoffset
extra_argsz{}(transfer_id={}, {}))"_get_kwargs_with_params_to_includer   format	__class____name__r   transfer_id)r	   Zparams_to_displayZmain_kwargs_to_displayr   r   r   __repr__T   s"    	 zTask.__repr__c                 C   s   | j jS )z8The id for the transfer request that the task belongs to)r   r   )r	   r   r   r   r   i   s    zTask.transfer_idc                 C   s&   i }|D ]}||kr|| ||< q|S Nr   )r	   kwargsincludefiltered_kwargsparamr   r   r   r   n   s
    z'Task._get_kwargs_with_params_to_includec                 C   s,   i }|  D ]\}}||krq|||< q|S r   )items)r	   r   excluder   r   valuer   r   r   "_get_kwargs_with_params_to_excludeu   s    
z'Task._get_kwargs_with_params_to_excludec              
   C   s   z`z.|   |  }| j s.| |W W 6S W n, tk
r\ } z| 	| W 5 d}~X Y nX W 5 | j D ]
}|  qh| jr| j  X dS )z9The callable to use when submitting a Task to an executorN)
r   r   r   announce_done_wait_on_dependent_futures_get_all_main_kwargsdone_execute_main	Exception_log_and_set_exception)r	   Zdone_callbackr   er   r   r   __call__}   s    
 
zTask.__call__c                 C   sJ   dg}|  ||}td|  d|  | jf |}| jrF| j| |S )NdatazExecuting task z with kwargs )r!   loggerdebug_mainr   r   Z
set_result)r	   r   Zparams_to_excludeZkwargs_to_displayZreturn_valuer   r   r   r&      s     zTask._execute_mainc                 C   s   t jddd | j| d S )NzException raised.T)exc_info)r,   r-   r   Zset_exception)r	   	exceptionr   r   r   r(      s    zTask._log_and_set_exceptionc                 K   s   t ddS )zThe method that will be ran in the executor

        This method must be implemented by subclasses from Task. main() can
        be implemented with any arguments decided upon by the subclass.
        z_main() must be implementedNNotImplementedError)r	   r   r   r   r   r.      s    z
Task._mainc                 C   sF   g }| j  D ](\}}t|tr,|| q|| q| | d S r   )r   r   
isinstancelistextendappend_wait_until_all_complete)r	   Zfutures_to_wait_on_futurer   r   r   r#      s    
zTask._wait_on_dependent_futuresc              	   C   sX   t d| | |D ]4}zt d| | |  W q tk
rD   Y qX qt d|  d S )Nz-%s about to wait for the following futures %sz%s about to wait for %sz%%s done waiting for dependent futures)r,   r-   resultr'   )r	   Zfuturesr9   r   r   r   r7      s    	  zTask._wait_until_all_completec                 C   s\   t  | j}| j D ]@\}}t|trFg }|D ]}||  q0n| }|||< q|S r   )copyr   r   r   r3   r4   r6   r:   )r	   r   r   Zpending_valuer:   r9   r   r   r   r$      s    

zTask._get_all_main_kwargs)NNNF)r   
__module____qualname____doc__r   r   propertyr   r   r!   r*   r&   r(   r.   r#   r7   r$   r   r   r   r   r      s$   	    
8
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SubmissionTaskzA base class for any submission task

    Submission tasks are the top-level task used to submit a series of tasks
    to execute a particular transfer.
    c              
   K   s   zF| j   t|d}|D ]
}|  q| j   | jf d|i| W n> tk
r } z | | |   | j   W 5 d}~X Y nX dS )a>  
        :type transfer_future: s3transfer.futures.TransferFuture
        :param transfer_future: The transfer future associated with the
            transfer request that tasks are being submitted for

        :param kwargs: Any additional kwargs that you may want to pass
            to the _submit() method
        Zqueuedr   N)	r   Zset_status_to_queuedr   Zset_status_to_running_submitBaseExceptionr(   +_wait_for_all_submitted_futures_to_completer"   )r	   r   r   Zon_queued_callbacksZon_queued_callbackr)   r   r   r   r.      s    	



zSubmissionTask._mainc                 K   s   t ddS )aX  The submission method to be implemented

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

        :param kwargs: Any additional keyword arguments you want to be passed
            in
        z_submit() must be implementedNr1   )r	   r   r   r   r   r   rA   &  s    
zSubmissionTask._submitc                 C   s2   | j j}|r.| | | j j}||kr(q.|}qd S r   )r   Zassociated_futuresr7   )r	   Zsubmitted_futuresZpossibly_more_submitted_futuresr   r   r   rC   2  s    
	z:SubmissionTask._wait_for_all_submitted_futures_to_completeN)r   r<   r=   r>   r.   rA   rC   r   r   r   r   r@      s   /r@   c                   @   s   e Zd ZdZdd ZdS )CreateMultipartUploadTaskz#Task to initiate a multipart uploadc                 C   s8   |j f ||d|}|d }| jj|j|||d |S )aq  
        :param client: The client to use when calling CreateMultipartUpload
        :param bucket: The name of the bucket to upload to
        :param key: The name of the key to upload to
        :param extra_args: A dictionary of any extra arguments that may be
            used in the initialization.

        :returns: The upload id of the multipart upload
        )BucketKeyUploadId)rE   rF   rG   )Zcreate_multipart_uploadr   Zadd_failure_cleanupZabort_multipart_upload)r	   clientr   r   r   response	upload_idr   r   r   r.   Q  s     zCreateMultipartUploadTask._mainNr   r<   r=   r>   r.   r   r   r   r   rD   N  s   rD   c                   @   s   e Zd ZdZdd ZdS )CompleteMultipartUploadTaskz#Task to complete a multipart uploadc                 C   s"   |j f |||d|id| dS )az  
        :param client: The client to use when calling CompleteMultipartUpload
        :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 parts: A list of parts to use to complete the multipart upload::

            [{'Etag': etag_value, 'PartNumber': part_number}, ...]

            Each element in the list consists of a return value from
            ``UploadPartTask.main()``.
        :param extra_args:  A dictionary of any extra arguments that may be
            used in completing the multipart transfer.
        ZParts)rE   rF   rG   ZMultipartUploadN)Zcomplete_multipart_upload)r	   rH   r   r   rJ   partsr   r   r   r   r.   n  s    z!CompleteMultipartUploadTask._mainNrK   r   r   r   r   rL   k  s   rL   )r;   loggingZs3transfer.utilsr   	getLoggerr   r,   r   r@   rD   rL   r   r   r   r   <module>   s   
 \^