o
    4ñÕfÓ(  ã                   @   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 )NéP   é   )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 )z¬Start 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 )zˆPeriodically 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"   	 #