o
    (d(                     @   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.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self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   newline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	   update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   r0   \   s   
zOpProgress.doner%   )r&   r'   r(   r)   r   r,   r0   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.
        r3   N)r   r   startsignalSIGWINCH_winchr4   r5   r1   r   r   r   r6   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           hhhhr3   )hasattrr   osisattyr:   fcntltermiosstructioctl
TIOCGWINSZunpackr   )r   dummyr?   r@   rA   bufcolr   r   r   r9   }   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   rI      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   failrK   status	STAT_DONEr$   r   rJ   
error_text)r   rO   r   r   r   rQ      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.Nr3   zGet:z%s %srH   )r   r   fetchrK   completer5   idr   rJ   rL   r   rM   r$   rN   r   r   r   rU      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.r:   Tg      Y@Fz%i%%r*   z	 %sB/s %sz [%s]z [%i %sz [%sz %sz %sBz	/%sB %i%%]z
 [Working]r   )!r   r   pulser<   r   r=   r>   r:   current_bytescurrent_itemsfloattotal_bytestotal_itemscurrent_cpsintr   rM   time_to_strworkerscurrent_itemrR   r   r   rK   rW   	shortdescrJ   active_subprocesscurrent_size
total_sizerV   r   r$   )	r   rK   r/   showntvalendetaworkervalr   r   r   rY      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   mediumdriver   r   r   rp      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   rM   fetched_bytesra   elapsed_timer_   rstripr7   r8   r4   )r   r7   r   r   r   rt      s   



zAcquireProgress.stopr%   )r&   r'   r(   r)   r   r6   r9   rI   rQ   rU   rY   rp   rt   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   strrq   KeyboardInterruptr1   r   r   r   ry   
  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   text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   boolrq   r{   r1   r   r   r   r~     s   
zCdromProgress.change_cdromN)r&   r'   r(   r)   ry   r,   r~   r   r   r   r   r     s
    r   )r)   
__future__r   ior=   r7   r   typestypingr   r   r   r   apt.progressr   __all__r   objectr   r	   r   r   r   r   r   r   <module>   s"   	 #