a
    Df                     @   sv   d dl Z d dlZd dlZd dlZd dlmZ ddlmZ G dd deZG dd deZ	G d	d
 d
eZ
dddZdS )    N)clear_output   )ProgressIndicatorc                       s   e Zd ZdZejdg dddZejdddZej	d	d
dZ
ej	dddZejdddZi Zg Z fddZdd ZdddZdddZ  ZS )ProgressBarz
    A simple text progress bar suitable for both the IPython notebook
    and the IPython interactive prompt.

    ProgressBars are automatically nested if a previous instantiated
    progress bars has not achieved 100% completion.
    stdout)r   disabled	broadcasta  
        Parameter to control display of the progress bar. By default,
        progress is shown on stdout but this may be disabled e.g. for
        jobs that log standard output to file.

        If the output mode is set to 'broadcast', a socket is opened on
        a stated port to broadcast the completion percentage. The
        RemoteProgress class may then be used to view the progress from
        a different process.)defaultZobjectsdocF   zB
        The width of the progress bar as the number of charactersr	   r
   #z5
        The character used to fill the progress bar. zA
        The character for the blank portion of the progress bar.Tz
        If enabled, the progress bar will disappear and display the
        total elapsed time once 100% completion is reached.c                    s4   d | _ | dd tj|  t jf i | d S )Nr   F)
start_time_stdout_displayr   current_progressappendsuper__init__selfparams	__class__ f/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/ipython/widgets.pyr   0   s    zProgressBar.__init__c                 C   s8  | j du rt | _ | jd | jd  }| jd |d |  }| jdkrLdS | jdkr|dkr| jrt | j  }tr|t  dd	| j |d
 |d |d f  | _d	dd | j
D }tj| n
| | |dkrtj
rtj
  dS d| jvr|  | jd< | jd dur4| jd | d| j  dS )z; Update the progress bar within the specified percent_rangeN   r         Y@r   r   d   z100%% %s %02d:%02d:%02di  <    c                 S   s   g | ]
}|j qS r   out.0Zpgr   r   r   
<listcomp>F       z(ProgressBar.__call__.<locals>.<listcomp>socket|)r   timeZpercent_rangedisplayelapsed_timer   labellowerr#   joinr   sysr   writer   r   popcache_get_socketsend)r   
percentagespanelapsedoutputr   r   r   __call__6   s0    


zProgressBar.__call__c                 C   s   t r
t   d| j }t|dk r,t|| n| j}| j| }t| jdkrPdnd}|d| jrj| jd nd| j	| dt| j	 | | | _
|rtjddd	 | jD  tj  td
 d S )Nr   r   
r!   z{}[{}{}] {:0.1f}%z:
r   c                 S   s   g | ]
}|j qS r   r"   r$   r   r   r   r&   b   r'   z/ProgressBar._stdout_display.<locals>.<listcomp>g-C6?)r   widthintmathfloorlenr   formatr-   	fill_charr#   r0   r   r1   r/   flushr*   sleep)r   r6   r+   Zpercent_per_charZ
char_countZblank_countprefixr   r   r   r   U   s&    


zProgressBar._stdout_display       c                 C   sj   dd l }| }||j}z(|jd|||d}| jd|  |W S  tyd   | jd Y d S 0 d S )Nr   ztcp://*)min_portmax_port	max_triesz#Progress broadcast bound to port %dz.No suitable port found for progress broadcast.)zmqContextr(   ZPUBZbind_to_random_portparammessage	Exception)r   rI   rJ   rK   rL   contextsockportr   r   r   r4   g   s    zProgressBar._get_socket)T)rF   rG   rH   )__name__
__module____qualname____doc__rN   ZObjectSelectorr+   Integerr<   StringrB   Z
blank_charBooleanr,   r3   r   r   r:   r   r4   __classcell__r   r   r   r   r      s   
r   c                       sH   e Zd ZdZejdddZejdddZ fddZ	 fd	d
Z
  ZS )RemoteProgressz
    Connect to a progress bar in a separate process with output_mode
    set to 'broadcast' in order to display the results (to stdout).
    	localhostz4
        Hostname where progress is being broadcast.r   rF   zTarget port on hostname.c                    s   t  jf d|i| d S )NrS   r   r   )r   rS   r   r   r   r   r      s    zRemoteProgress.__init__c                    s   dd l }| }||j}||jd |d| j d t| j	  d }z4|
 }|d\}}t|}|| _t | W qL ty   |d ur| jd|dd Y qY qL ty   | jd	| Y qL0 qLd S )
Nr   r!   ztcp://:r)   z
Exited at z.3fz% completionz"Could not process socket message: )rL   rM   r(   ZSUB
setsockoptZ	SUBSCRIBEconnecthostnamestrrS   recvsplitfloatr-   r   r:   KeyboardInterruptrN   rO   rP   )r   rL   rQ   rR   percentrO   Zpercent_strr-   r   r   r   r:      s$    zRemoteProgress.__call__)rT   rU   rV   rW   rN   rY   rb   rX   rS   r   r:   r[   r   r   r   r   r\   w   s
   r\   c                       sN   e Zd ZdZejdddZejejj	j
ddZ fddZ fdd	Z  ZS )
RunProgressa  
    RunProgress breaks up the execution of a slow running command so
    that the level of completion can be displayed during execution.

    This class is designed to run commands that take a single numeric
    argument that acts additively. Namely, it is expected that a slow
    running command 'run_hook(X+Y)' can be arbitrarily broken up into
    multiple, faster executing calls 'run_hook(X)' and 'run_hook(Y)'
    without affecting the overall result.

    For instance, this is suitable for simulations where the numeric
    argument is the simulated time - typically, advancing 10 simulated
    seconds takes about twice as long as advancing by 5 seconds.
    r   zG
        The run interval used to break up updates to the progress bar.r   z
        By default updates time in param which is very fast and does
        not need a progress bar. Should be set to some slower running
        callable where display of progress level is desired.c                    s   t  jf i | d S )Nr^   r   r   r   r   r      s    zRunProgress.__init__c                    sl   d}|| | j krB| | j  || j 7 }t d|t|   q|| }|dkrh| | t d dS )z
        Execute the run_hook to a total of value, breaking up progress
        updates by the value specified by interval.
        r   r   N)intervalrun_hookr   r:   rf   )r   valueZ	completed	remainingr   r   r   r:      s    

zRunProgress.__call__)rT   rU   rV   rW   rN   Numberrj   CallableZDynamicZtime_fnZadvancerk   r   r:   r[   r   r   r   r   ri      s
   ri   Fc                 c   s^   t  }|du rt| n|}t| }t|\}}||d | d  |rR||fV  q"|V  q"dS )z
    A helper utility to display a progress bar when iterating over a
    collection of a fixed length or a generator (with a declared
    length).

    If enum=True, then equivalent to enumerate with a progress bar.
    Ng      ?r   )r   r@   	enumeratenext)iteratorenumlengthprogressgenivalr   r   r   ru      s    ru   )FN)r>   r0   r*   rN   ZIPython.core.displayr   Z	core.utilr   r   r\   ri   ru   r   r   r   r   <module>   s   l%,