3
F"]                 @   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| _|j| _d| _d| _g | _	d| _
g | _d| _d| _d| _d| _| j  d S )Nr   g      ?Tg      )	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   I/home/ghaith/Applications/sentSatProg_v.0.1.3/pySmartDL/control_thread.pyr      s    zControlThread.__init__c          
   C   s   t j  }| jjd x| jjj s"| j| jj| _	| j	dkr^| j
| jj| jj | j	 | _| jr| jjrdtj| jjtj| jjtj| j	tjd| jj | jj | jjd | jj tj| jddf }ndtj| jjtj| j	f }|td	t|d
   }t|ddd t jd qW | jjr>| jjd d S | jr| jjrtdtj| jjtj| jjtj| j	tjdf  n.tdtj| jjtj| jjtj| j	f  t j  }t|| | _x| jjj rt jd qW | jjj  d| j_| jjs| j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   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 )Nr7   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|jd |j| 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| | j d dkr>t|t| | _t|dkrT|jd | j dk rbdS | jS )Nr   
   r   r>   2   )r   r   rA   rB   r*   r   r@   )r   r   rC   r   r   r   r%   k   s    


zControlThread.calcETAN)r>   r?   )__name__
__module____qualname____doc__r   r6   r8   r9   r:   r;   r<   r=   r#   r%   r   r   r   r   r      s   '
r   )r   r    r   r   r   r   r   r   r   <module>   s   