U
    ž`)                     @   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 re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 )NP      )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 )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   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rtt
| j| j | j }dt| jt|f }|jD ].}d}|js|jrd|j }t|t| t| | jkr q||7 }d}qd}|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 qq||7 }q|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.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   
	 #