B
    F.\6                 @   s6  d dl mZmZmZ d dlZd dlmZ d dlZd dlZd dl	m
Z
 d dlmZ d dlmZ ddlmZmZmZ d	d
lmZ d	dlmZmZmZ d	dlmZmZ d	dlmZ d	dlmZm Z m!Z!m"Z"m#Z# e$e%Z&dd Z'G dd de(Z)G dd de)Z*G dd de)Z+G dd de(Z,G dd de,Z-dd ZdS )    )print_functiondivisionabsolute_importN)default_timer)valmap)gen)IOLoop   )format_timeProgressMultiProgress   )html_escape)connectcoerce_to_addressCommClosedError)default_client
futures_of)dumps)ignoring	key_split	is_kernel
LoopRunnerparse_timedeltac             C   s   | d krt  jjS t| S )N)r   	schedulerZaddressr   )r    r   Blib/python3.7/site-packages/distributed/diagnostics/progressbar.pyget_scheduler   s    
r   c               @   s@   e Zd ZdddZedd Zejdd	 Zd
d Z	dd Z
dS )ProgressBarN100msTc             C   sh   t || _d | _x&|D ]}t|drt|j| _P qW dd |D | _t|dd| _|| _	t
 | _d S )Nclientc             S   s    h | ]}t |d r|jn|qS )key)hasattrr!   ).0kr   r   r   	<setcomp>)   s    z'ProgressBar.__init__.<locals>.<setcomp>s)default)r   r   r    r"   weakrefrefkeysr   intervalcompleter   _start_time)selfr*   r   r+   r,   r!   r   r   r   __init__    s    


zProgressBar.__init__c             C   s   t  | j S )N)r   r-   )r.   r   r   r   elapsed.   s    zProgressBar.elapsedc             #   s  | j  | jtj fdd}dd }t| j| jr>|  jnd dV | _t	
d | jjdt|t|| jd| jr|  jnd d	V  xy$| jj| jr|  jnd d
V }W n tk
r   P Y nX || _|d | _| jf | |d dkr| j V  | jf | P qW t	
d d S )Nc             3   s&   t |  d}| V  t|d S )N)r,   )r   setupr   Return)r   p)r,   r*   r   r   r1   7   s    
z!ProgressBar.listen.<locals>.setupc             S   s4   t |jt |j|jd}|jdkr0||j |S )N)all	remainingstatuserror)lenall_keysr*   r6   updateextra)r   r3   resultr   r   r   function=   s    

z$ProgressBar.listen.<locals>.function)connection_argsz"Progressbar Connected to schedulerfeed)opr1   r=   r+   )Zserializers)deserializersr6   )r7   finishedz'Progressbar disconnected from scheduler)r,   r*   r   	coroutiner   r   r    r>   commloggerdebugwriter   r+   Z_serializersread_deserializersr   _last_responser6   	_draw_barclose
_draw_stop)r.   r1   r=   responser   )r,   r*   r   listen2   s4    

zProgressBar.listenc             K   s   d S )Nr   )r.   kwargsr   r   r   rM   `   s    zProgressBar._draw_stopc          	   C   s"   t t | j  W d Q R X d S )N)r   AttributeErrorrD   abort)r.   r   r   r   __del__c   s    
zProgressBar.__del__)Nr   T)__name__
__module____qualname__r/   propertyr0   r   rC   rO   rM   rS   r   r   r   r   r      s
   
.r   c                   s.   e Zd Zd fdd	Zdd Zd	d
 Z  ZS )TextProgressBarNr   (   Tc       	         sF   t t| |||| || _|p$t | _|rBt| j}|| j d S )N)	superrX   r/   widthr   loopr   Zrun_syncrO   )	r.   r*   r   r+   r[   r\   r,   startZloop_runner)	__class__r   r   r/   i   s    
zTextProgressBar.__init__c       	   	   K   s|   |rd||  nd}dt | j|  }t d| }t| j}d|| j||}tt tj	| tj
  W d Q R X d S )Nr	   g      ?#d   z"[{0:<{1}}] | {2}% Completed | {3})intr[   r
   r0   formatr   
ValueErrorsysstdoutrG   flush)	r.   r5   r4   rP   ZfracbarZpercentr0   msgr   r   r   rK   t   s    


zTextProgressBar._draw_barc             K   s   t jd t j  d S )N)rd   re   rG   rf   )r.   rP   r   r   r   rM      s    zTextProgressBar._draw_stop)Nr   rY   NTT)rT   rU   rV   r/   rK   rM   __classcell__r   r   )r^   r   rX   h   s    
rX   c                   s<   e Zd ZdZd fdd	Zdd Zdd	d
Zdd Z  ZS )ProgressWidgetz ProgressBar that uses an IPython ProgressBar widget for the notebook

    See Also
    --------
    progress: User function
    TextProgressBar: Text version suitable for the console
    Nr   Fc       
         sz   t t| |||| ddlm}m}m}m}	 |	d| _|dddd| _	|	d| _
|| j
| j	g| _|| j| jg| _d S )Nr   )FloatProgressHBoxVBoxHTML r	   )minmaxdescription)rZ   rk   r/   
ipywidgetsrl   rm   rn   ro   elapsed_timerg   bar_textZ
bar_widgetwidget)
r.   r*   r   r+   r,   r\   rl   rm   rn   ro   )r^   r   r   r/      s    

zProgressWidget.__init__c             K   s   t  | j | jjf |S )N)r   currentadd_callbackrO   rw   _ipython_display_)r.   rP   r   r   r   rz      s    z ProgressWidget._ipython_display_c             K   s^   |dkr8d| j _dt| d t| j d d | j_n"|sZd| j _dt| j d | j_d S )	Nr7   dangerz=<div style="padding: 0px 10px 5px 10px"><b>Exception</b> <tt>z</tt>: z</div>successz9<div style="padding: 0px 10px 5px 10px"><b>Finished:</b> )rg   	bar_stylereprr
   r0   ru   value)r.   r5   r6   	exceptionrP   r   r   r   rM      s    zProgressWidget._draw_stopc             K   sF   || }dt | j d | j_|r*|| nd| j_d||f | j_d S )Nz:<div style="padding: 0px 10px 5px 10px"><b>Computing:</b> z</div>g      ?zH<div style="padding: 0px 10px 0px 10px; text-align:right;">%d / %d</div>)r
   r0   ru   r   rg   rv   )r.   r5   r4   rP   ndoner   r   r   rK      s    zProgressWidget._draw_bar)Nr   FN)N)	rT   rU   rV   __doc__r/   rz   rM   rK   rj   r   r   )r^   r   rk      s    
rk   c               @   sH   e Zd ZdeddfddZedd Zejdd	 Z	d
d Z
dd ZdS )MultiProgressBarNr   Fc             C   sf   t || _d | _x&|D ]}t|drt|j| _P qW dd |D | _|| _|| _|| _	t
 | _d S )Nr    c             S   s    h | ]}t |d r|jn|qS )r!   )r"   r!   )r#   r$   r   r   r   r%      s    z,MultiProgressBar.__init__.<locals>.<setcomp>)r   r   r    r"   r(   r)   r*   funcr+   r,   r   r-   )r.   r*   r   r   r+   r,   r!   r   r   r   r/      s    


zMultiProgressBar.__init__c             C   s   t  | j S )N)r   r-   )r.   r   r   r   r0      s    zMultiProgressBar.elapsedc             #   s   | j  | j| jtj fdd}dd }t| j| jrF|  jnd dV | _	t
d | j	dt|t|| jdV  xf| j	j| jr|  jnd d	V }|| _|d
 | _| jf | |d
 dkr| j	 V  | jf | P qW t
d d S )Nc             3   s(   t |  d}| V  t|d S )N)r,   r   )r   r1   r   r2   )r   r3   )r,   r   r*   r   r   r1      s    
z&MultiProgressBar.listen.<locals>.setupc             S   s8   t t|jt t|j|jd}|jdkr4||j |S )N)r4   r5   r6   r7   )r   r8   r9   r*   r6   r:   r;   )r   r3   r<   r   r   r   r=      s    



z)MultiProgressBar.listen.<locals>.function)r>   z"Progressbar Connected to schedulerr?   )r@   r1   r=   r+   )rA   r6   )r7   rB   z'Progressbar disconnected from scheduler)r,   r*   r   r   rC   r   r   r    r>   rD   rE   rF   rG   r   r+   rH   rI   rJ   r6   rK   rL   rM   )r.   r1   r=   rN   r   )r,   r   r*   r   rO      s.    

zMultiProgressBar.listenc             K   s   d S )Nr   )r.   rP   r   r   r   rM      s    zMultiProgressBar._draw_stopc          	   C   s"   t t | j  W d Q R X d S )N)r   rQ   rD   rR   )r.   r   r   r   rS      s    
zMultiProgressBar.__del__)rT   rU   rV   r   r/   rW   r0   r   rC   rO   rM   rS   r   r   r   r   r      s
   *r   c                   sN   e Zd ZdZdddedf fdd	Zdd	 Zd
d ZdddZdd Z	  Z
S )MultiProgressWidgetaU   Multiple progress bar Widget suitable for the notebook

    Displays multiple progress bars for a computation, split on computation
    type.

    See Also
    --------
    progress: User-level function <--- use this
    MultiProgress: Non-visualization component that contains most logic
    ProgressWidget: Single progress bar widget
    Nr   g?Fc                s2   t t| ||||| ddlm} |g | _d S )Nr   )rn   )rZ   r   r/   rt   rn   rw   )r.   r*   r   Zminimumr+   r   r,   rn   )r^   r   r   r/     s    zMultiProgressWidget.__init__c                s   ddl m mm}m d_ fdd|D _fdd|D _fdd|D _dd	 }d
d t	|
 |ddD }|fdd|D _jjfj_d S )Nr   )rl   rm   rn   ro   rp   c                s   i | ]} d ddd|qS )r   r	   rp   )rq   rr   rs   r   )r#   r!   )rl   r   r   
<dictcomp>  s   z3MultiProgressWidget.make_widget.<locals>.<dictcomp>c                s   i | ]} d |qS )rp   r   )r#   r!   )ro   r   r   r     s    c                s4   i | ],} d t t|tr | n| d |qS )zQ<div style="padding: 0px 10px 0px 10px; text-align:left; word-wrap: break-word;">z</div>)r   
isinstancebytesdecode)r#   r!   )ro   r   r   r     s   c             S   s   | ddd S )z2 Order keys by most numerous, then by string name Nr   )Zkvr   r   r   keyfunc  s    z0MultiProgressWidget.make_widget.<locals>.keyfuncc             S   s   g | ]\}}|qS r   r   )r#   r$   vr   r   r   
<listcomp>   s    z3MultiProgressWidget.make_widget.<locals>.<listcomp>T)r!   reversec                s,   g | ]$} j | j| j| gqS r   )	bar_textsbars
bar_labels)r#   r!   )rm   r.   r   r   r   "  s   )rt   rl   rm   rn   ro   ru   r   r   r   sorteditemsZbar_widgetsrw   children)r.   r4   rn   r   Z	key_orderr   )rl   rm   ro   r.   r   make_widget  s    




zMultiProgressWidget.make_widgetc             K   s   t  | j | jjf |S )N)r   rx   ry   rO   rw   rz   )r.   rP   r   r   r   rz   (  s    z%MultiProgressWidget._ipython_display_c             K   s~   x2|  D ]&\}}|s$d| j| _q
d| j| _q
W |dkrddt| d t| j d d | j_ndt| j d | j_d S )	Nr}   r{   r7   z=<div style="padding: 0px 10px 5px 10px"><b>Exception</b> <tt>z</tt>:r|   z</div>z9<div style="padding: 0px 10px 5px 10px"><b>Finished:</b> )r   r   r~   r   r
   r0   ru   r   )r.   r5   r6   r   r!   rP   r$   r   r   r   r   rM   ,  s    zMultiProgressWidget._draw_stopc             K   s   | j r| jjs| | xb| D ]V\}}|||  }dt| j d | j_|rX|| nd| j	| _d||f | j
| _q"W d S )Nz:<div style="padding: 0px 10px 5px 10px"><b>Computing:</b> z</div>g      ?zH<div style="padding: 0px 10px 0px 10px; text-align: right">%d / %d</div>)r*   rw   r   r   r   r
   r0   ru   r   r   r   )r.   r5   r4   r6   rP   r$   Zntasksr   r   r   r   rK   ?  s    
zMultiProgressWidget._draw_bar)NN)rT   rU   rV   r   r   r/   r   rz   rM   rK   rj   r   r   )r^   r   r      s   
r   c              O   s   | dd}| dd}| dd}|r,tt| } t| ttfsH| g} |dkrVt }|r||rlt| |d}nt| |d}|S t	| |d dS )a   Track progress of futures

    This operates differently in the notebook and the console

    *  Notebook:  This returns immediately, leaving an IPython widget on screen
    *  Console:  This blocks until the computation completes

    Parameters
    ----------
    futures: Futures
        A list of futures or keys to track
    notebook: bool (optional)
        Running in the notebook or not (defaults to guess)
    multi: bool (optional)
        Track different functions independently (defaults to True)
    complete: bool (optional)
        Track all keys (True) or only keys that have not yet run (False)
        (defaults to True)

    Notes
    -----
    In the notebook, the output of `progress` must be the last statement
    in the cell. Typically, this means calling `progress` at the end of a
    cell.

    Examples
    --------
    >>> progress(futures)  # doctest: +SKIP
    [########################################] | 100% Completed |  1.7s
    notebookNmultiTr,   )r,   )
popAssertionErrorr   r   setlistr   r   rk   rX   )ZfuturesrP   r   r   r,   rg   r   r   r   progressK  s    r   ).Z
__future__r   r   r   ZloggingZtimeitr   rd   r(   Ztoolzr   Ztornador   Ztornado.ioloopr   r   r
   r   r   Zcompatibilityr   Zcorer   r   r   r    r   r   Zprotocol.pickler   Zutilsr   r   r   r   r   Z	getLoggerrT   rE   r   objectr   rX   rk   r   r   r   r   r   r   <module>   s*   
I0FQ