U
    &ŸˆcÞ)  ã                   @   s  d Z ddlmZ ddlZddlZddlZddlZz0ddlZddlm	Z	m
Z
mZ e e	 e
 e W n ek
rt   Y nX ddlZddlmZ e dddgZejjd	k rªeZne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Ú
OpProgressé   c                 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Ú_2   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__>   s    zTextProgress.__init__TFc                 C   sx   | j  d¡ | j  |¡ | jt|ƒkrB| j  | jt|ƒ d ¡ n|rXt| jt|ƒƒ| _|rj| j  d¡ n
| j  ¡  dS )z8Write the message on the terminal, fill remaining space.úú Ú
N)r   Úwriter   ÚlenÚmaxÚflush)r   r   ÚnewlineZmaximizer   r   r   Ú_writeC   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   [   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)   a   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,   j   s    zOpProgress.done)N)N)r#   r$   r%   r&   r   r)   r,   r   r   r   r   r	   U   s   

	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   s2   t  | |¡ tj | ¡ d | _d| _tdƒ| _d S )NéP   é   )r   r   r   r   Ú_signalr   ÚlongÚ_idr   r   r   r   r   v   s
    zAcquireProgress.__init__c                 C   s4   t j | ¡ t tj| j¡| _|  ¡  t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.
        r/   N)	r   r   ÚstartÚsignalÚSIGWINCHÚ_winchr0   r1   r2   r-   r   r   r   r3   ~   s    zAcquireProgress.startc                 G   sh   t | jdƒrdt | j ¡ ¡rdddl}ddl}ddl}| | j|j	d¡}| 
d|¡\}}}}|d | _dS )z)Signal handler for window resize signals.Úfilenor   Ns           Zhhhhr/   )Úhasattrr   ÚosÚisattyr7   ÚfcntlÚtermiosÚstructZioctlZ
TIOCGWINSZZunpackr   )r   Zdummyr;   r<   r=   ZbufÚcolr   r   r   r6   ‹   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   r@   –   s
    zAcquireProgress.ims_hitc                 C   s^   t j | |¡ |jj|jjkr4|  tdƒ|j ¡ n&|  tdƒ|j ¡ |  d|jj	 ¡ dS )zCalled when an item is failed.zIgn zErr z  %sN)
r   r   ÚfailrB   ÚstatusZ	STAT_DONEr"   r   rA   Z
error_text)r   rF   r   r   r   rH   Ÿ   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	rj|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   ÚfetchrB   Úcompleter2   Úidr   rA   rC   r   rD   r"   rE   r   r   r   rJ   ©   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r˜tt
| j| j ƒ| j ƒ}dt | j¡t |¡f }|jD ].}d}|jsð|jržd|j }t|ƒt|ƒ t|ƒ | jkrâ qÐ||7 }d}qžd}|jjjr|d	|jjj|jjf 7 }n|d
|jj 7 }|jjjrJ|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Æ qÐqž||7 }qž|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.r7   Tg      Y@Fz%i%%r'   z	 %sB/s %sz [%s]z [%i %sz [%sz %sz %sBz	/%sB %i%%ú]z
 [Working]r   )!r   r   Úpulser8   r   r9   r:   r7   Zcurrent_bytesZcurrent_itemsÚfloatZtotal_bytesZtotal_itemsÚcurrent_cpsr1   r   rD   Útime_to_strZworkersZcurrent_itemrI   r   r   rB   rL   Z	shortdescrA   Zactive_subprocessZcurrent_sizeZ
total_sizerK   r   r"   )	r   rB   r+   ZshownZtvalÚendZetaZworkerÚvalr   r   r   rN   ¸   sh    ÿÿÿÿ
ÿ

þ 
"zAcquireProgress.pulsec                 C   s0   t j | ||¡ |  tdƒ||f ¡ tƒ dk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   rV   ý   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   rD   Zfetched_bytesrQ   Zelapsed_timerP   Úrstripr4   r5   r0   )r   r4   r   r   r   rX     s    



ýýzAcquireProgress.stop)N)r#   r$   r%   r&   r   r3   r6   r@   rH   rJ   rN   rV   rX   r   r   r   r   r   s   s   
	
E	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   sF   t j | ¡ |  tdƒd¡ zttdƒƒW S  tk
r@   Y dS X dS )z,Ask the user to provide a name for the disc.zBPlease provide a name for this Disc, such as 'Debian 2.1r1 Disk 1'Fú:N)r   r   Úask_cdrom_namer"   r   ÚstrrW   ÚKeyboardInterruptr-   r   r   r   r[     s    
ÿzCdromProgress.ask_cdrom_namec                 C   s$   t j | ||¡ |r |  |d¡ dS )zSet the current progress.FN)r   r   r)   r"   )r   ÚtextZcurrentr   r   r   r)   #  s    zCdromProgress.updatec                 C   sH   t j | ¡ |  tdƒd¡ zttƒ dkƒW S  tk
rB   Y dS X dS )z"Ask the user to change the CD-ROM.z1Please insert a Disc in the drive and press enterFr'   N)r   r   Úchange_cdromr"   r   ÚboolrW   r]   r-   r   r   r   r_   *  s    
ÿzCdromProgress.change_cdromN)r#   r$   r%   r&   r[   r)   r_   r   r   r   r   r     s   )r&   Z
__future__r   Úior9   r4   r   ÚtypesÚtypingr   r   r   ÚImportErrorr   Zapt.progressr   Ú__all__Úversion_infoÚmajorZ	raw_inputrW   Úintr1   r   Úobjectr   r	   r   r   r   r   r   r   Ú<module>   s6   
	 #