o
    ra(                     @   s   d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZ ddlZddlmZ g dZdd ZG d	d
 d
eZG dd dejeZG dd dejeZG dd dejeZdS )z'Progress reporting for text interfaces.    )print_functionN)CallableOptionalUnion)base)AcquireProgressCdromProgress
OpProgressc                 C   s"   t | }|| krt | d}|S )z>Translate the message, also try apt if translation is missing.Zapt)apt_pkggettext)msgres r   3/usr/lib/python3/dist-packages/apt/progress/text.py_$   s   
r   c                   @   s$   e Zd ZdZd	ddZd
ddZdS )TextProgressz.Internal Base class for text progress classes.Nc                 C   s   |pt j| _d| _d S )Nr   )sysstdout_file_widthselfZoutfiler   r   r   __init__0   s   
zTextProgress.__init__TFc                 C   sz   | j d | j | | jt|kr!| j | jt| d  n|r,t| jt|| _|r6| j d dS | j   dS )z8Write the message on the terminal, fill remaining space. 
N)r   writer   lenmaxflush)r   r   newlineZmaximizer   r   r   _write5   s   zTextProgress._writeN)TF)__name__
__module____qualname____doc__r   r!   r   r   r   r   r   -   s    
r   c                   @   s,   e Zd ZdZd	ddZd	ddZdd ZdS )
r	   z\Operation progress reporting.

    This closely resembles OpTextProgress in libapt-pkg.
    Nc                 C   s"   t | | tj|  d| _d S )N )r   r   r   r	   old_opr   r   r   r   r   M   s   
zOpProgress.__init__c                 C   sL   t j| | | jr| jr| | j | d| j| jf dd | j| _dS )z1Called periodically to update the user interface.z%s... %i%%FTN)r   r	   updateZmajor_changer(   r!   oppercent)r   r+   r   r   r   r)   S   s
   zOpProgress.updatec                 C   s8   t j|  | jr| tdd| jf dd d| _dS )z,Called once an operation has been completed.z%c%s... Doner   Tr'   N)r   r	   doner(   r!   r   r   r   r   r   r,   \   s   
zOpProgress.doner"   )r#   r$   r%   r&   r   r)   r,   r   r   r   r   r	   G   s
    

	r	   c                   @   sZ   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d ZdS )r   z'AcquireProgress for the text interface.Nc                 C   s.   t | | tj|  d | _d| _d| _d S )NP      )r   r   r   r   _signalr   _idr   r   r   r   r   h   s
   
zAcquireProgress.__init__c                 C   s0   t j|  ttj| j| _|   d| _dS )zStart an Acquire progress.

        In this case, the function sets up a signal handler for SIGWINCH, i.e.
        window resize signals. And it also sets id to 1.
        r/   N)r   r   startsignalSIGWINCH_winchr0   r1   r-   r   r   r   r2   p   s   
zAcquireProgress.startc                 G   sp   t | jdr4t| j r6ddl}ddl}ddl}|| j|j	d}|
d|\}}}}|d | _dS dS dS )z)Signal handler for window resize signals.filenor   Ns           Zhhhhr/   )hasattrr   osisattyr6   fcntltermiosstructZioctlZ
TIOCGWINSZunpackr   )r   Zdummyr:   r;   r<   bufcolr   r   r   r5   }   s   zAcquireProgress._winchc                 C   sH   t j| | td|j }|jjr|dt|jj 7 }| 	| dS )z@Called when an item is update (e.g. not modified on the server).zHit  [%sB]N)
r   r   ims_hitr   descriptionownerfilesizer
   size_to_strr!   r   itemliner   r   r   rA      s
   zAcquireProgress.ims_hitc                 C   s`   t j| | |jj|jjkr| td|j  dS | td|j  | d|jj	  dS )zCalled when an item is failed.zIgn zErr z  %sN)
r   r   failrC   statusZ	STAT_DONEr!   r   rB   Z
error_text)r   rG   r   r   r   rI      s
   zAcquireProgress.failc                 C   sx   t j| | |jjrdS | j|j_|  jd7  _tdd|jj|jf  }|jj	r5|dt
|jj	 7 }| | dS )z/Called when some of the item's data is fetched.Nr/   zGet:z%s %sr@   )r   r   fetchrC   completer1   idr   rB   rD   r
   rE   r!   rF   r   r   r   rK      s   
zAcquireProgress.fetchc           	      C   s  t j| | t| jdrt| j sdS | j| j	 d t
| j| j  }d}d| }d}| jrLtt
| j| j | j }dt| jt|f }|jD ]}d}|jsv|jrud|j }t|t| t| | jkro np||7 }d}qOd}|jjjr|d	|jjj|jjf 7 }n|d
|jj 7 }|jjjr|d|jjj 7 }|dt|j 7 }|jr|jjjs|dt|j|jd |j f 7 }|d7 }t|t| t| | jkr n||7 }qO|s|td7 }| jr|| jt| t| d | 7 }|  |d dS )zPeriodically invoked while the Acquire process is underway.

        Return False if the user asked to cancel the whole Acquire process.r6   Tg      Y@Fz%i%%r'   z	 %sB/s %sz [%s]z [%i %sz [%sz %sz %sBz	/%sB %i%%]z
 [Working]r   )!r   r   pulser7   r   r8   r9   r6   Zcurrent_bytesZcurrent_itemsfloatZtotal_bytesZtotal_itemscurrent_cpsintr
   rE   time_to_strZworkersZcurrent_itemrJ   r   r   rC   rM   Z	shortdescrB   Zactive_subprocessZcurrent_sizeZ
total_sizerL   r   r!   )	r   rC   r+   ZshownZtvalendZetaZworkervalr   r   r   rO      sh   






"zAcquireProgress.pulsec                 C   s0   t j| || | td||f  t dvS )z7Prompt the user to change the inserted removable media.zUMedia change: please insert the disc labeled
 '%s'
in the drive '%s' and press enter
)cC)r   r   media_changer!   r   input)r   ZmediumZdriver   r   r   rX      s
   

zAcquireProgress.media_changec                 C   s^   t j|  | tdt| jt| j	t| j
f d ddl}||j| j dS )z/Invoked when the Acquire process stops running.zFetched %sB in %s (%sB/s)
r   r   N)r   r   stopr!   r   r
   rE   Zfetched_bytesrS   Zelapsed_timerQ   rstripr3   r4   r0   )r   r3   r   r   r   rZ      s   



zAcquireProgress.stopr"   )r#   r$   r%   r&   r   r2   r5   rA   rI   rK   rO   rX   rZ   r   r   r   r   r   e   s    
	
E	r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   zText CD-ROM progress.c                 C   s@   t j|  | tdd zttdW S  ty   Y dS w )z,Ask the user to provide a name for the disc.zDPlease provide a name for this medium, such as 'Debian 2.1r1 Disk 1'F:N)r   r   ask_cdrom_namer!   r   strrY   KeyboardInterruptr-   r   r   r   r]   
  s   
zCdromProgress.ask_cdrom_namec                 C   s(   t j| || |r| |d dS dS )zSet the current progress.FN)r   r   r)   r!   )r   textZcurrentr   r   r   r)     s   zCdromProgress.updatec                 C   sB   t j|  | tdd ztt dkW S  ty    Y dS w )z"Ask the user to change the CD-ROM.z4Please insert an installation medium and press enterFr'   )r   r   change_cdromr!   r   boolrY   r_   r-   r   r   r   ra     s   
zCdromProgress.change_cdromN)r#   r$   r%   r&   r]   r)   ra   r   r   r   r   r     s
    r   )r&   Z
__future__r   ior8   r3   r   typestypingr   r   r   r
   Zapt.progressr   __all__r   objectr   r	   r   r   r   r   r   r   <module>   s"   	 #