B
    G"¹]”  ã               @   s2   d dl Z d dlZddlmZ G dd„ de jƒZdS )é    Né   )Úutilsc               @   sb   e Zd Z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dd„Zdd„ ZdS )ÚControlThreadz>A class that shows information about a running SmartDL object.c             C   sl   t j | ¡ || _|j| _|j| _|j| _d| _d| _g | _	d| _
g | _d| _d| _d| _d| _|  ¡  d S )Nr   g      ð¿T)Ú	threadingÚThreadÚ__init__ÚobjÚprogress_barÚloggerÚ
shared_varÚdl_speedÚetaÚlastBytesSamplesÚlast_calculated_totalBytesÚcalcETA_queueÚ	calcETA_iÚcalcETA_valÚdl_timeÚdaemonÚstart)Úselfr   © r   úFC:\Users\Ibrahim Fayad\Desktop\SentinelTUI\pySmartDL\control_thread.pyr      s    zControlThread.__init__c          
   C   s   t   ¡ }| j d¡ x| jj ¡ s"|  | jj¡| _	| j	dkr^|  
| jj| jj | j	 ¡| _| jr| jjrÒdt | jj¡t | jj¡t | j	¡t d| jj | jj ¡| jjd | jj tj| jddf }ndt | jj¡t | j	¡f }|td	ƒt|ƒd
   }t|ddd t  d¡ qW | jjr>| j d¡ d S | jr¶| jjrˆtdt | jj¡t | jj¡t | j	¡t d¡f ƒ n.tdt | jj¡t | jj¡t | j	¡f ƒ t   ¡ }t|| ƒ| _x| jj ¡ rêt  d¡ qÎW | jj ¡  d| j_| jjs| j d| j ¡ d S )Nz Control thread has been started.r   z+[*] %s / %s @ %s/s %s [%3.1f%%, %s left]   g      ð?g      Y@T)Ú	fmt_shortz[*] %s / ??? MB @ %s/s   é   r   ú )ÚendÚflushgš™™™™™¹?z'File download process has been stopped.z*[*] %s / %s @ %s/s %s [100%%, 0s left]    z[*] %s / %s @ %s/s    Úfinishedz$File downloaded within %.2f seconds.)Útimer
   Úinfor   ÚpoolÚdoneÚcalcDownloadSpeedr   Úvaluer   ÚcalcETAÚfilesizer   r	   r   Úsizeof_humanÚ
time_humanÚchrÚlenÚprintÚsleepÚ_killedÚfloatr   Úpost_threadpool_threadÚis_aliveÚshutdownÚstatusÚerrors)r   Út1r2   Zt2r   r   r   Úrun   s8    
d

8.
zControlThread.runc             C   s    | j dks| jjdkrdS | j S )Nr   Úpaused)r   r   r2   )r   r   r   r   Úget_etaB   s    zControlThread.get_etac             C   s   | j jdkrdS | jS )Nr6   r   )r   r2   r   )r   r   r   r   Ú	get_speedF   s    zControlThread.get_speedc             C   s    | j j| jjkr| jjS | j jS )N)r   r$   r   r&   )r   r   r   r   Úget_dl_sizeJ   s    zControlThread.get_dl_sizec             C   s   | j jS )N)r   r&   )r   r   r   r   Úget_final_filesizeN   s    z ControlThread.get_final_filesizec             C   s    | j jsdS d| jj | j j S )Nr   g      ð?)r   r&   r   r$   )r   r   r   r   Úget_progressP   s    zControlThread.get_progressc             C   s   | j S )N)r   )r   r   r   r   Úget_dl_timeT   s    zControlThread.get_dl_timeé   çš™™™™™¹?c             C   sV   | j }|| j }|| _|dkr>t|ƒ|kr4| d¡ | |¡ t|ƒt|ƒ | }|S )a  
        Function calculates the download rate.
        @param totalBytes: The total amount of bytes.
        @param sampleCount: How much samples should the function take into consideration.
        @param sampleDuration: Duration of a sample in seconds.
        r   )r   r   r*   ÚpopÚappendÚsum)r   Z
totalBytesZsampleCountZsampleDurationÚlZnewBytesZdlRater   r   r   r#   W   s    


zControlThread.calcDownloadSpeedc             C   sh   |  j d7  _ | j}| |¡ | j d dkr>t|ƒt|ƒ | _t|ƒdkrT| d¡ | j dk rbdS | jS )Nr   é
   r   r=   é2   )r   r   r@   rA   r*   r   r?   )r   r   rB   r   r   r   r%   k   s    


zControlThread.calcETAN)r=   r>   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r5   r7   r8   r9   r:   r;   r<   r#   r%   r   r   r   r   r      s   '
r   )r   r   Ú r   r   r   r   r   r   r   Ú<module>   s   