o
    !dh                     @   s8  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dl	mZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ eeZedg dZdefddZeddgZedg dZedZeddgZ edZ!eddgZ"ede"dZ#edddgZ$ed d!gZ%G d"d# d#e&Z'G d$d% d%eZ(G d&d' d'e(Z)G d(d) d)e)Z*G d*d+ d+e(Z+G d,d- d-e+Z,G d.d/ d/e(Z-G d0d1 d1e(Z.G d2d3 d3e&Z/G d4d5 d5e/Z0G d6d7 d7e/Z1G d8d9 d9e1Z2G d:d; d;e1Z3G d<d= d=ej4Z5G d>d? d?e&Z6dS )@    )divisionN)
namedtuple)defaultdict)CancelledError)
FatalError)BaseSubscriber)queueensure_text_typerelative_path)human_readable_size)	uni_print)WarningResult)OnDoneFilteredSubscriber
BaseResult)transfer_typesrcdestc                 C   s,   t |j}|r||7 }t| t| ||fi S N)list_fieldstyper   )nameZextra_fieldsbase_clsfields r   Dusr/lib/python3.10/site-packages/awscli/customizations/s3/results.py_create_new_result_cls'   s   
r   QueuedResulttotal_transfer_sizeProgressResult)bytes_transferredr   	timestampSuccessResultFailureResult	exceptionDryRunResultErrorResultCtrlCResult)r   CommandResultZnum_tasks_failedZnum_tasks_warnedFinalTotalSubmissionsResulttotal_submissionsc                   @   s   e Zd ZdS )ShutdownThreadRequestN)__name__
__module____qualname__r   r   r   r   r,   K   s    r,   c                   @   sR   e Zd ZdZd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 )BaseResultSubscriberNc                 C   s*   || _ i | _|| _|du r| j| _dS dS )zSubscriber to send result notifications during transfer process

        :param result_queue: The queue to place results to be processed later
            on.
        N)_result_queue_result_kwargs_cache_transfer_typeTRANSFER_TYPE)selfresult_queuer   r   r   r   __init__R   s   zBaseResultSubscriber.__init__c                 K   s6   |  | | j|jj }tdi |}| j| d S Nr   )_add_to_result_kwargs_cacher2   metatransfer_idr   r1   put)r5   futurekwargsresult_kwargsZqueued_resultr   r   r   	on_queued^   s   
zBaseResultSubscriber.on_queuedc                 K   s6   | j |jj }td|t d|}| j| d S )N)r!   r"   r   )r2   r:   r;   r    timer1   r<   )r5   r=   r!   r>   r?   Zprogress_resultr   r   r   on_progressd   s   z BaseResultSubscriber.on_progressc                 C   s$   |  |}| jtdi | d S r8   )%_on_done_pop_from_result_kwargs_cacher1   r<   r#   r5   r=   r?   r   r   r   _on_successk   s   
z BaseResultSubscriber._on_successc                 C   sZ   |  |}t|trt}t|trt}| j||d d S | jtdd|i| d S )Nr%   r%   r   )	rC   
isinstancer   r(   r   r'   r1   r<   r$   )r5   r=   er?   Zerror_result_clsr   r   r   _on_failureo   s   


z BaseResultSubscriber._on_failurec                 C   s4   |  |\}}| j|||jjd}|| j|jj< d S )N)r   r   r   r   )_get_src_destr3   r:   sizer2   r;   )r5   r=   r   r   r?   r   r   r   r9   y   s   z0BaseResultSubscriber._add_to_result_kwargs_cachec                 C   s   | j |jj}|d |S )Nr   )r2   popr:   r;   rD   r   r   r   rC      s   
z:BaseResultSubscriber._on_done_pop_from_result_kwargs_cachec                 C      t d)Nz_get_src_dest()NotImplementedError)r5   r=   r   r   r   rJ         z"BaseResultSubscriber._get_src_destr   )r-   r.   r/   r4   r7   r@   rB   rE   rI   r9   rC   rJ   r   r   r   r   r0   O   s    


r0   c                   @       e Zd ZdZdd Zdd ZdS )UploadResultSubscriberuploadc                 C   s0   |j j}| |j}d|j d |j }||fS Ns3:///)r:   	call_args_get_srcfileobjbucketkeyr5   r=   rW   r   r   r   r   r   rJ      s   z$UploadResultSubscriber._get_src_destc                 C      t |S r   r
   r5   rY   r   r   r   rX      rP   zUploadResultSubscriber._get_srcN)r-   r.   r/   r4   rJ   rX   r   r   r   r   rR          rR   c                   @      e Zd Zdd ZdS )UploadStreamResultSubscriberc                 C      dS N-r   r^   r   r   r   rX         z%UploadStreamResultSubscriber._get_srcN)r-   r.   r/   rX   r   r   r   r   ra          ra   c                   @   rQ   )DownloadResultSubscriberZdownloadc                 C   s0   |j j}d|j d |j }| |j}||fS rT   )r:   rW   rZ   r[   	_get_destrY   r\   r   r   r   rJ      s   z&DownloadResultSubscriber._get_src_destc                 C   r]   r   r
   r^   r   r   r   rh      rP   z"DownloadResultSubscriber._get_destN)r-   r.   r/   r4   rJ   rh   r   r   r   r   rg      r_   rg   c                   @   r`   )DownloadStreamResultSubscriberc                 C   rb   rc   r   r^   r   r   r   rh      re   z(DownloadStreamResultSubscriber._get_destN)r-   r.   r/   rh   r   r   r   r   ri      rf   ri   c                   @      e Zd ZdZdd ZdS )CopyResultSubscribercopyc                 C   sB   |j j}|j}d|d  d |d  }d|j d |j }||fS )NrU   ZBucketrV   Key)r:   rW   copy_sourcerZ   r[   )r5   r=   rW   rn   r   r   r   r   r   rJ      s
   z"CopyResultSubscriber._get_src_destNr-   r.   r/   r4   rJ   r   r   r   r   rk          rk   c                   @   rj   )DeleteResultSubscriberdeletec                 C   s$   |j j}d|j d |j }|d fS rT   )r:   rW   rZ   r[   )r5   r=   rW   r   r   r   r   rJ      s   z$DeleteResultSubscriber._get_src_destNro   r   r   r   r   rq      rp   rq   c                   @   rj   )BaseResultHandlerz6Base handler class to be called in the ResultProcessorc                 C   rM   )Nz
__call__()rN   r5   resultr   r   r   __call__   rP   zBaseResultHandler.__call__N)r-   r.   r/   __doc__rv   r   r   r   r   rs          rs   c                   @   s   e Zd Z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d Zdd Zdd Zdd Zdd ZdS )ResultRecorderz?Records and track transfer statistics based on results receivedc                 C   s   d| _ d| _d| _d| _d| _d| _d| _d| _d | _d | _	d| _
tt| _i | _t| jt| jt| jt| jt| jt| jt| jt| ji| _d S Nr   )r!   bytes_failed_to_transferfiles_transferredfiles_failedfiles_warnederrorsexpected_bytes_transferredexpected_files_transferred final_expected_files_transferred
start_timebytes_transfer_speedr   int_ongoing_progress_ongoing_total_sizesr   _record_queued_resultr    _record_progress_resultr#   _record_success_resultr$   _record_failure_resultr   _record_warning_resultr'   _record_error_resultr(   r*   _record_final_expected_files_result_handler_mapr5   r   r   r   r7      s,   

zResultRecorder.__init__c                 C   s   | j | jkS r   )r   r   r   r   r   r   expected_totals_are_final   s   z(ResultRecorder.expected_totals_are_finalc                 C      | j t|| j|d dS )z0Record the result of an individual Result objectru   N)r   getr   _record_nooprt   r   r   r   rv         
zResultRecorder.__call__c                 C   sT   t |tstdt| g }|j|j|jfD ]}|d ur$|t| qd	|S )NzdAny result using _get_ongoing_dict_key must subclass from BaseResult. Provided result is of type: %s:)
rG   r   
ValueErrorr   r   r   r   appendr	   join)r5   ru   Z	key_partsZresult_propertyr   r   r   _get_ongoing_dict_key   s   

z$ResultRecorder._get_ongoing_dict_keyc                 C   s.   |  |}| j|d}| j|d }||fS rz   )r   r   rL   r   )r5   ru   ongoing_keytotal_progresstotal_file_sizer   r   r   _pop_result_from_ongoing_dicts   s   
z-ResultRecorder._pop_result_from_ongoing_dictsc                 K      d S r   r   r5   r>   r   r   r   r        zResultRecorder._record_noopc                 K   sN   | j d u r
t | _ |j}|| j| |< |r|  j|7  _|  jd7  _d S N   )r   rA   r   r   r   r   r   )r5   ru   r>   r   r   r   r   r   	  s   

z$ResultRecorder._record_queued_resultc                 K   s^   |j }| | | j| |  |7  < |  j |7  _ |j| jkr-| j |j| j  | _d S d S r   )r!   (_update_ongoing_transfer_size_if_unknownr   r   r"   r   r   )r5   ru   r>   r!   r   r   r   r     s   


z&ResultRecorder._record_progress_resultc                 C   sn   |  |}| j| d u r5|j}|jd ur+|| j|< | j| }|| }|  j|7  _d S |  j|j7  _d S d S r   )r   r   r   r   r   r!   )r5   ru   r   r   Zongoing_progressZunaccounted_bytesr   r   r   r   &  s   



z7ResultRecorder._update_ongoing_transfer_size_if_unknownc                 K   s   |  | |  jd7  _d S r   )r   r|   r5   ru   r>   r   r   r   r   @  s   
z%ResultRecorder._record_success_resultc                 K   sL   |  |\}}|d ur|| }|  j|7  _|  jd7  _|  jd7  _d S r   )r   r{   r}   r|   )r5   ru   r>   r   r   Zprogress_leftr   r   r   r   D  s   z%ResultRecorder._record_failure_resultc                 K      |  j d7  _ d S r   )r~   r   r   r   r   r   Q     z%ResultRecorder._record_warning_resultc                 K   r   r   )r   r   r   r   r   r   T  r   z#ResultRecorder._record_error_resultc                 K   s   |j | _d S r   )r+   r   r   r   r   r   r   W  s   z+ResultRecorder._record_final_expected_filesN)r-   r.   r/   rw   r7   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ry      s     ry   c                   @   s   e Zd ZdZdZdZde Zde ZdZde Z	dZ
d	Zd
ZdZdZdZd8d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#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd9d.d/Zd0d1 Z d2d3 Z!d4d5 Z"d6d7 Z#dS ):ResultPrinterz#{remaining_files} file(s) remainingz~{expected_total}z (calculating...)zOCompleted {bytes_completed}/{expected_bytes_completed} ({transfer_speed}) with z)Completed {files_completed} file(s) with z${transfer_type}: {transfer_location}z	(dryrun) z7{transfer_type} failed: {transfer_location} {exception}z	{message}zfatal error: {exception}zcancelled: ctrl-c receivedz{src} to {dest}z{src}Nc                 C   sv   || _ || _| jdu rtj| _|| _| jdu rtj| _d| _t| jt	| j
t| jt| jt| jt| jt| jt| ji| _dS )a  Prints status of ongoing transfer

        :type result_recorder: ResultRecorder
        :param result_recorder: The associated result recorder

        :type out_file: file-like obj
        :param out_file: Location to write progress and success statements.
            By default, the location is sys.stdout.

        :type error_file: file-like obj
        :param error_file: Location to write warnings and errors.
            By default, the location is sys.stderr.
        Nr   )_result_recorder	_out_filesysstdout_error_filestderr_progress_lengthr    _print_progressr#   _print_successr$   _print_failurer   _print_warningr'   _print_errorr(   _print_ctrl_cr&   _print_dry_runr*   -_clear_progress_if_no_more_expected_transfersr   )r5   result_recorderout_fileZ
error_filer   r   r   r7   z  s$   


zResultPrinter.__init__c                 C   r   )z<Print the progress of the ongoing transfer based on a resultr   N)r   r   r   _print_nooprt   r   r   r   rv     r   zResultPrinter.__call__c                 K   r   r   r   r   r   r   r   r     r   zResultPrinter._print_noopc                 K   s0   | j j|j| |d}| |}| | d S N)r   transfer_location)DRY_RUN_FORMATformatr   _get_transfer_location_adjust_statement_padding_print_to_out_file)r5   ru   r>   	statementr   r   r   r     s   
zResultPrinter._print_dry_runc                 K   s8   | j j|j| |d}| |}| | |   d S r   )SUCCESS_FORMATr   r   r   r   r   _redisplay_progress)r5   ru   r>   Zsuccess_statementr   r   r   r     s   

zResultPrinter._print_successc                 K   s<   | j j|j| ||jd}| |}| | |   d S )N)r   r   r%   )FAILURE_FORMATr   r   r   r%   r   _print_to_error_filer   )r5   ru   r>   Zfailure_statementr   r   r   r     s   

zResultPrinter._print_failurec                 K   s0   | j j|jd}| |}| | |   d S )N)message)WARNING_FORMATr   r   r   r   r   )r5   ru   r>   Zwarning_statementr   r   r   r     s   

zResultPrinter._print_warningc                 K   s   |  | jj|jd d S )NrF   )_flush_error_statementERROR_FORMATr   r%   r   r   r   r   r     s   zResultPrinter._print_errorc                 K   s   |  | j d S r   )r   
CTRL_C_MSGr   r   r   r   r        zResultPrinter._print_ctrl_cc                 C   s   |  |}| | d S r   )r   r   )r5   Zerror_statementr   r   r   r     s   
z$ResultPrinter._flush_error_statementc                 C   s.   |j d u r| jj|jdS | jj|j|j dS )N)r   )r   r   )r   SRC_TRANSFER_LOCATION_FORMATr   r   !SRC_DEST_TRANSFER_LOCATION_FORMATrt   r   r   r   r     s
   
z$ResultPrinter._get_transfer_locationc                 C   s   d| _ |   d S rz   )r   _add_progress_if_neededr   r   r   r   r     s   z!ResultPrinter._redisplay_progressc                 C   s   |   r
|   d S d S r   )_has_remaining_progressr   r   r   r   r   r     s   z%ResultPrinter._add_progress_if_neededc                 K   s   |  t| jj| jj }| jjdkr9t| jj| jj }|  t| jj}t| jj	d }| j
j||||d}n
| jj| jj|d}| j sM|| j7 }| j|dd}t|d | _| | d S )Nr   z/s)bytes_completedexpected_bytes_completedtransfer_speedremaining_files)Zfiles_completedr   )ending_charr   )_get_expected_totalstrr   r   r|   r   r   r!   r{   r   BYTE_PROGRESS_FORMATr   FILE_PROGRESS_FORMATr   _STILL_CALCULATING_TOTALSr   lenr   r   )r5   r>   r   r   r   r   Zprogress_statementr   r   r   r     sL   	

zResultPrinter._print_progressc                 C   s   | j  s| jj|dS |S )N)expected_total)r   r   _ESTIMATED_EXPECTED_TOTALr   )r5   r   r   r   r   r     s
   
z!ResultPrinter._get_expected_total
c                 C   s   | | jd}|| S )N )ljustr   )r5   Zprint_statementr   r   r   r   r     s   z'ResultPrinter._adjust_statement_paddingc                 C   s&   | j  sdS | j j}| j j}||kS )NT)r   r   r|   r   )r5   Zactualexpectedr   r   r   r     s
   
z%ResultPrinter._has_remaining_progressc                 C      t || j d S r   )r   r   r5   r   r   r   r   r   "  r   z ResultPrinter._print_to_out_filec                 C   r   r   )r   r   r   r   r   r   r   %  r   z"ResultPrinter._print_to_error_filec                 K   s,   | j r|  st| d| j d S d S d S )N )r   r   r   r   r   r   r   r   r   r   (  s   z;ResultPrinter._clear_progress_if_no_more_expected_transfers)NN)r   )$r-   r.   r/   Z_FILES_REMAININGr   r   r   r   r   r   r   r   r   r   r   r   r7   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   [  sT    
"	
/
r   c                   @   rj   )NoProgressResultPrinterz,A result printer that doesn't print progressc                 K   r   r   r   r   r   r   r   r   /  re   z'NoProgressResultPrinter._print_progressN)r-   r.   r/   rw   r   r   r   r   r   r   -  rx   r   c                   @   rQ   )OnlyShowErrorsResultPrinterz,A result printer that only prints out errorsc                 K   r   r   r   r   r   r   r   r   5  re   z+OnlyShowErrorsResultPrinter._print_progressc                 K   r   r   r   r   r   r   r   r   8  re   z*OnlyShowErrorsResultPrinter._print_successN)r-   r.   r/   rw   r   r   r   r   r   r   r   3  s    r   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	ResultProcessorNc                 C   s2   t j|  || _|| _| jdu rg | _d| _dS )aQ  Thread to process results from result queue

        This includes recording statistics and printing transfer status

        :param result_queue: The result queue to process results from
        :param result_handlers: A list of callables that take a result in as
            a parameter to process the result for that handler.
        NT)	threadingThreadr7   r1   _result_handlers_result_handlers_enabled)r5   r6   Zresult_handlersr   r   r   r7   =  s   	

zResultProcessor.__init__c                 C   sd   	 z%| j d}t|trtd W d S | jr| | t|tr%d| _W n
 t	j
y0   Y nw q)NTzSShutdown request received in result processing thread, shutting down result thread.F)r1   r   rG   r,   LOGGERdebugr   _process_resultr'   r   Emptyrt   r   r   r   runM  s"   


zResultProcessor.runc                 C   sT   | j D ]$}z|| W q ty' } ztjd|||dd W Y d }~qd }~ww d S )Nz.Error processing result %s with handler %s: %sTexc_info)r   	Exceptionr   r   )r5   ru   Zresult_handlerrH   r   r   r   r   b  s   
zResultProcessor._process_resultr   )r-   r.   r/   r7   r   r   r   r   r   r   r   <  s    
r   c                   @   sD   e Z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 )CommandResultRecorderc                 C   s   || _ || _|| _dS )a  Records the result for an entire command

        It will fully process all results in a result queue and determine
        a CommandResult representing the entire command.

        :type result_queue: queue.Queue
        :param result_queue: The result queue in which results are placed on
            and processed from

        :type result_recorder: ResultRecorder
        :param result_recorder: The result recorder to track the various
            results sent through the result queue

        :type result_processor: ResultProcessor
        :param result_processor: The result processor to process results
            placed on the queue
        N)r6   r   _result_processor)r5   r6   r   Zresult_processorr   r   r   r7   m  s   
zCommandResultRecorder.__init__c                 C   s   | j   d S r   )r   startr   r   r   r   r     s   zCommandResultRecorder.startc                 C   s   | j t  | j  d S r   )r6   r<   r,   r   r   r   r   r   r   shutdown  s   zCommandResultRecorder.shutdownc                 C   s   t | jj| jj | jjS )zGet the CommandResult representing the result of a command

        :rtype: CommandResult
        :returns: The CommandResult representing the total result from running
            a particular command
        )r)   r   r}   r   r~   r   r   r   r   get_command_result  s   z(CommandResultRecorder.get_command_resultc                 C   s   | j t| d S r   )r6   r<   r*   )r5   totalr   r   r   notify_total_submissions  s   z.CommandResultRecorder.notify_total_submissionsc                 C   s   |    | S r   )r   r   r   r   r   	__enter__  s   zCommandResultRecorder.__enter__c                 G   s>   |rt jd|dd | jt|d |   dS |   d S )Nz-Exception caught during command execution: %sTr   rF   )r   r   r6   r<   r'   r   )r5   exc_type	exc_valueargsr   r   r   __exit__  s   zCommandResultRecorder.__exit__N)
r-   r.   r/   r7   r   r   r   r   r   r  r   r   r   r   r   l  s    r   )7
__future__r   loggingr   r   rA   collectionsr   r   Zs3transfer.exceptionsr   r   Zs3transfer.subscribersr   Zawscli.compatr   r	   Zawscli.customizations.s3.utilsr   r   Zawscli.customizations.utilsr   r   r   	getLoggerr-   r   r   r   r   r    r#   r$   r&   r'   r(   r)   r*   objectr,   r0   rR   ra   rg   ri   rk   rq   rs   ry   r   r   r   r   r   r   r   r   r   r   <module>   sf   
=	  S	0