o
    Ëý°jVŒ  ã                    @   st  d dl mZmZ d dlmZ d dlmZ d dlmZ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mZmZmZmZmZmZmZmZm Z m!Z!m"Z" d
dl#m$Z$m%Z% d
dl&m'Z'm(Z(m)Z)m*Z* d
dl+m,Z, d
dl-m.Z. d
dl/m0Z0 d
dl1m2Z2 d
dl3m4Z4 d
dl5m6Z6 d
dl7m8Z8m9Z9 d
dl:m;Z;m<Z< ede=ƒZ>e!dƒZ?eg e@f ZAG dd„ deƒZB										 	!	"	dwd#e"ee? ee? f d$eCd%ee@ d&eDd'ee' d(eDd)eeg e@f  d*e@d+e6d,e6d-e6d.e6d/e@d0eDd1ee? fd2d3„ZEG d4d5„ d5eƒZFG d6d7„ d7eFƒZGG d8d9„ d9eFƒZHG d:d;„ d;eFƒZIG d<d=„ d=eFƒZJG d>d?„ d?eFƒZKG d@dA„ dAeFƒZLG dBdC„ dCeFƒZMG dDdE„ dEeFƒZNG dFdG„ dGeFƒZOG dHdI„ dIeFƒZPG dJdK„ dKeƒZQeG dLdM„ dMƒƒZRG dNdO„ dOe.ƒZSeTdPk�r8d dlUZUd dlVZVd
dQlWmXZX d
dRlYmZZZ d
dSl[m\Z\ d
dTl7m9Z9 e\dUdVddW�Z[e9dXdYdZƒZ7e7 ]d[d\d]¡ d^eXd_ƒd`e7dadbdcddœdee[eZdfƒg	Z^d dgl_m`Z` e`e^ƒZae'ddh�Z&eSeHƒ eIdiƒeJƒ eIdjƒeLƒ eKƒ e&ddk��RZbebjcdldmdn�Zdebjcdodmdn�Zeebjcdpdmddq�Zfebjg�s%ebjheddrds� ebjheedtds� eV idu¡ eU jd dv¡d
k �reb keleaƒ¡ ebjg�röW d  ƒ dS W d  ƒ dS 1 �s1w   Y  dS dS )xé    )ÚABCÚabstractmethod)Údeque)ÚSized)Ú	dataclassÚfield)Ú	timedelta)Úceil)ÚEventÚRLockÚThread)ÚTracebackType)ÚAnyÚCallableÚDequeÚDictÚIterableÚListÚ
NamedTupleÚNewTypeÚOptionalÚSequenceÚTupleÚTypeÚTypeVarÚUnioné   )ÚfilesizeÚget_console)ÚConsoleÚJustifyMethodÚRenderableTypeÚGroup)ÚHighlighter)ÚJupyterMixin)ÚLive)ÚProgressBar)ÚSpinner)Ú	StyleType)ÚColumnÚTable)ÚTextÚTextTypeÚTaskIDÚProgressTypec                       sl   e Zd ZdZdddddef‡ fdd„Zddd„Zddd„Zdee	e
  dee
 dee d	d
fdd„Z‡  ZS )Ú_TrackThreadz)A thread to periodically update progress.ÚprogressÚProgressÚtask_idr-   Úupdate_periodc                    s.   || _ || _|| _tƒ | _d| _tƒ  ¡  d S )Nr   )r0   r2   r3   r
   ÚdoneÚ	completedÚsuperÚ__init__)Úselfr0   r2   r3   ©Ú	__class__© úŒ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pip/_vendor/rich/progress.pyr7   /   s   z_TrackThread.__init__ÚreturnNc                 C   sj   | j }| jj}| j}d}| jj}||ƒs(| j}||kr$|||| ƒ |}||ƒr| jj| j | jdd� d S )Nr   T)r5   Úrefresh)r2   r0   Úadvancer3   r4   Úwaitr5   Úupdate)r8   r2   r?   r3   Úlast_completedr@   r5   r;   r;   r<   Úrun8   s   üz_TrackThread.runc                 C   ó   |   ¡  | S ©N©Ústart©r8   r;   r;   r<   Ú	__enter__F   ó   z_TrackThread.__enter__Úexc_typeÚexc_valÚexc_tbc                 C   s   | j  ¡  |  ¡  d S rE   )r4   ÚsetÚjoin©r8   rK   rL   rM   r;   r;   r<   Ú__exit__J   s   
z_TrackThread.__exit__©r=   N)r=   r/   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úfloatr7   rC   rI   r   r   ÚBaseExceptionr   rQ   Ú__classcell__r;   r;   r9   r<   r/   ,   s    
	

þýüûr/   ú
Working...NTFé
   úbar.backúbar.completeúbar.finishedú	bar.pulseçš™™™™™¹?ÚsequenceÚdescriptionÚtotalÚauto_refreshÚconsoleÚ	transientÚget_timeÚrefresh_per_secondÚstyleÚcomplete_styleÚfinished_styleÚpulse_styler3   Údisabler=   c              	   c   s”   � |rt dƒgng }| t||	|
|d�t dƒtƒ f¡ t||||||p$d|dœŽ}|� |j| |||d�E dH  W d  ƒ dS 1 sCw   Y  dS )a�  Track progress by iterating over a sequence.

    Args:
        sequence (Iterable[ProgressType]): A sequence (must support "len") you wish to iterate over.
        description (str, optional): Description of task show next to progress bar. Defaults to "Working".
        total: (float, optional): Total number of steps. Default is len(sequence).
        auto_refresh (bool, optional): Automatic refresh, disable to force a refresh after each iteration. Default is True.
        transient: (bool, optional): Clear the progress on exit. Defaults to False.
        console (Console, optional): Console to write to. Default creates internal Console instance.
        refresh_per_second (float): Number of times per second to refresh the progress information. Defaults to 10.
        style (StyleType, optional): Style for the bar background. Defaults to "bar.back".
        complete_style (StyleType, optional): Style for the completed bar. Defaults to "bar.complete".
        finished_style (StyleType, optional): Style for a finished bar. Defaults to "bar.done".
        pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
        update_period (float, optional): Minimum time (in seconds) between calls to update(). Defaults to 0.1.
        disable (bool, optional): Disable display of progress.
    Returns:
        Iterable[ProgressType]: An iterable of the values in the sequence.

    ú([progress.description]{task.description})ri   rj   rk   rl   ú-[progress.percentage]{task.percentage:>3.0f}%r[   )rd   re   rf   rg   rh   rm   )rc   rb   r3   N)Ú
TextColumnÚextendÚ	BarColumnÚTimeRemainingColumnr1   Útrack)ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   r3   rm   Úcolumnsr0   r;   r;   r<   rt   T   s8   €&ÿüøÿù
ÿ"ÿrt   c                   @   sp   e Zd ZU dZdZee ed< ddee ddfdd„Z	defdd	„Z
d
ddefdd„Zed
ddefdd„ƒZdS )ÚProgressColumnz3Base class for a widget to use in progress display.NÚmax_refreshÚtable_columnr=   c                 C   s   || _ i | _d | _d S rE   )Ú_table_columnÚ_renderable_cacheÚ_update_time)r8   rx   r;   r;   r<   r7   �   s   
zProgressColumn.__init__c                 C   s   | j ptƒ S )z.Get a table column, used to build tasks table.)ry   r)   rH   r;   r;   r<   Úget_table_column¢   s   zProgressColumn.get_table_columnÚtaskÚTaskc                 C   sp   |  ¡ }| jdur)|js)z
| j|j \}}W n	 ty   Y n
w || j |kr)|S |  |¡}||f| j|j< |S )a  Called by the Progress object to return a renderable for the given task.

        Args:
            task (Task): An object containing information regarding the task.

        Returns:
            RenderableType: Anything renderable (including str).
        N)rg   rw   r5   rz   ÚidÚKeyErrorÚrender)r8   r}   Úcurrent_timeÚ	timestampÚ
renderabler;   r;   r<   Ú__call__¦   s   	ÿ
zProgressColumn.__call__c                 C   s   dS )z"Should return a renderable object.Nr;   ©r8   r}   r;   r;   r<   r�   ½   s    zProgressColumn.renderrE   )rS   rT   rU   rV   rw   r   rW   Ú__annotations__r)   r7   r|   r!   r…   r   r�   r;   r;   r;   r<   rv   ˜   s   
 rv   c                       sJ   e Zd ZdZ	dddœdedee f‡ fdd„Zd	d
defdd„Z‡  Z	S )ÚRenderableColumnz�A column to insert an arbitrary column.

    Args:
        renderable (RenderableType, optional): Any renderable. Defaults to empty string.
    Ú N©rx   r„   rx   c                   ó   || _ tƒ j|d� d S ©NrŠ   )r„   r6   r7   )r8   r„   rx   r9   r;   r<   r7   É   ó   zRenderableColumn.__init__r}   r~   r=   c                 C   s   | j S rE   )r„   r†   r;   r;   r<   r�   Ï   s   zRenderableColumn.render)r‰   )
rS   rT   rU   rV   r!   r   r)   r7   r�   rY   r;   r;   r9   r<   rˆ   Â   s    ÿÿÿÿrˆ   c                       s€   e Zd ZdZ					ddedee d	ed
edee	 f
‡ fdd„Z
		ddedee d	eddfdd„Zdddefdd„Z‡  ZS )ÚSpinnerColumna‰  A column with a 'spinner' animation.

    Args:
        spinner_name (str, optional): Name of spinner animation. Defaults to "dots".
        style (StyleType, optional): Style of spinner. Defaults to "progress.spinner".
        speed (float, optional): Speed factor of spinner. Defaults to 1.0.
        finished_text (TextType, optional): Text used when task is finished. Defaults to " ".
    Údotsúprogress.spinnerç      ð?ú NÚspinner_nameri   ÚspeedÚfinished_textrx   c                    s<   t |||d�| _t|tƒrt |¡n|| _tƒ j|d� d S )N©ri   r”   rŠ   )	r'   ÚspinnerÚ
isinstanceÚstrr+   Úfrom_markupr•   r6   r7   )r8   r“   ri   r”   r•   rx   r9   r;   r<   r7   Ý   s   ÿýzSpinnerColumn.__init__Úspinner_styler=   c                 C   s   t |||d�| _dS )a-  Set a new spinner.

        Args:
            spinner_name (str): Spinner name, see python -m rich.spinner.
            spinner_style (Optional[StyleType], optional): Spinner style. Defaults to "progress.spinner".
            speed (float, optional): Speed factor of spinner. Defaults to 1.0.
        r–   N)r'   r—   )r8   r“   r›   r”   r;   r;   r<   Úset_spinnerí   s   zSpinnerColumn.set_spinnerr}   r~   c                 C   s$   |j r| j}|S | j | ¡ ¡}|S rE   )Úfinishedr•   r—   r�   rg   )r8   r}   Útextr;   r;   r<   r�   ü   s   ÿÿþýzSpinnerColumn.render)r�   r�   r‘   r’   N)r�   r‘   )rS   rT   rU   rV   r™   r   r(   rW   r,   r)   r7   rœ   r!   r�   rY   r;   r;   r9   r<   rŽ   Ó   s<    úþýüûúüþýü
ûrŽ   c                       sd   e Zd ZdZ					ddededed	ed
ee	 dee
 ddf‡ fdd„Zdddefdd„Z‡  ZS )rp   zA column containing text.ÚnoneÚleftTNÚtext_formatri   ÚjustifyÚmarkupÚhighlighterrx   r=   c                    s:   || _ || _|| _|| _|| _tƒ j|ptdd�d� d S )NT©Úno_wraprŠ   )r¡   r¢   ri   r£   r¤   r6   r7   r)   )r8   r¡   ri   r¢   r£   r¤   rx   r9   r;   r<   r7     s   	zTextColumn.__init__r}   r~   c                 C   sR   | j j|d�}| jrtj|| j| jd�}n	t|| j| jd�}| jr'| j |¡ |S )N©r}   )ri   r¢   )	r¡   Úformatr£   r+   rš   ri   r¢   r¤   Ú	highlight)r8   r}   Ú_textrž   r;   r;   r<   r�     s   zTextColumn.render)rŸ   r    TNN)rS   rT   rU   rV   r™   r(   r    Úboolr   r#   r)   r7   r+   r�   rY   r;   r;   r9   r<   rp     s.    ùþýüûúùørp   c                       sf   e Zd ZdZ						ddee d	ed
edededee ddf‡ fdd„Zddde	fdd„Z
‡  ZS )rr   a  Renders a visual progress bar.

    Args:
        bar_width (Optional[int], optional): Width of bar or None for full width. Defaults to 40.
        style (StyleType, optional): Style for the bar background. Defaults to "bar.back".
        complete_style (StyleType, optional): Style for the completed bar. Defaults to "bar.complete".
        finished_style (StyleType, optional): Style for a finished bar. Defaults to "bar.done".
        pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
    é(   r\   r]   r^   r_   NÚ	bar_widthri   rj   rk   rl   rx   r=   c                    s0   || _ || _|| _|| _|| _tƒ j|d� d S rŒ   )r­   ri   rj   rk   rl   r6   r7   )r8   r­   ri   rj   rk   rl   rx   r9   r;   r<   r7   .  s   	zBarColumn.__init__r}   r~   c                 C   sP   t td|jƒtd|jƒ| jdu rdntd| jƒ|j | ¡ | j| j| j	| j
d�	S )z&Gets a progress bar widget for a task.r   Nr   )	rc   r5   ÚwidthÚpulseÚanimation_timeri   rj   rk   rl   )r&   Úmaxrc   r5   r­   Ústartedrg   ri   rj   rk   rl   r†   r;   r;   r<   r�   >  s   

÷zBarColumn.render)r¬   r\   r]   r^   r_   N)rS   rT   rU   rV   r   Úintr(   r)   r7   r&   r�   rY   r;   r;   r9   r<   rr   #  s0    ùþýüûúùørr   c                   @   ó"   e Zd ZdZdddefdd„ZdS )ÚTimeElapsedColumnzRenders time elapsed.r}   r~   r=   c                 C   sD   |j r|jn|j}|du rtddd�S tt|ƒd�}tt|ƒdd�S )úShow time remaining.Nú-:--:--zprogress.elapsed©ri   ©Úseconds)r�   Úfinished_timeÚelapsedr+   r   r³   r™   )r8   r}   r¼   Údeltar;   r;   r<   r�   P  s
   zTimeElapsedColumn.renderN©rS   rT   rU   rV   r+   r�   r;   r;   r;   r<   rµ   M  ó    rµ   c                   @   s&   e Zd ZdZdZdddefdd„ZdS )	rs   z!Renders estimated time remaining.ç      à?r}   r~   r=   c                 C   s8   |j }|du rtddd�S tt|ƒd�}tt|ƒdd�S )r¶   Nr·   zprogress.remainingr¸   r¹   )Útime_remainingr+   r   r³   r™   )r8   r}   Ú	remainingÚremaining_deltar;   r;   r<   r�   _  s
   zTimeRemainingColumn.renderN)rS   rT   rU   rV   rw   r+   r�   r;   r;   r;   r<   rs   Y  s    rs   c                   @   r´   )ÚFileSizeColumnzRenders completed filesize.r}   r~   r=   c                 C   ó   t  t|jƒ¡}t|dd�S )úShow data completed.zprogress.filesizer¸   )r   Údecimalr³   r5   r+   ©r8   r}   Ú	data_sizer;   r;   r<   r�   k  ó   zFileSizeColumn.renderNr¾   r;   r;   r;   r<   rÄ   h  r¿   rÄ   c                   @   r´   )ÚTotalFileSizeColumnzRenders total filesize.r}   r~   r=   c                 C   rÅ   )rÆ   zprogress.filesize.totalr¸   )r   rÇ   r³   rc   r+   rÈ   r;   r;   r<   r�   t  rÊ   zTotalFileSizeColumn.renderNr¾   r;   r;   r;   r<   rË   q  r¿   rË   c                       sH   e Zd ZdZ	ddedee ddf‡ fdd„Zd	d
defdd„Z	‡  Z
S )ÚDownloadColumnz¤Renders file size downloaded and total, e.g. '0.5/2.3 GB'.

    Args:
        binary_units (bool, optional): Use binary units, KiB, MiB etc. Defaults to False.
    FNÚbinary_unitsrx   r=   c                    r‹   rŒ   )rÍ   r6   r7   )r8   rÍ   rx   r9   r;   r<   r7   �  r�   zDownloadColumn.__init__r}   r~   c                 C   s¬   t |jƒ}t |jƒ}| jrt |g d¢d¡\}}nt |g d¢d¡\}}|| }|| }|dkr2dnd}|d|› d�›}	|d|› d�›}
|	› d	|
› d
|› �}t|dd�}|S )z.Calculate common unit for completed and total.)	ÚbytesÚKiBÚMiBÚGiBÚTiBÚPiBÚEiBÚZiBÚYiBi   )	rÎ   ÚKBÚMBÚGBÚTBÚPBÚEBÚZBÚYBéè  r   r   z,.Úfú/r’   zprogress.downloadr¸   )r³   r5   rc   rÍ   r   Úpick_unit_and_suffixr+   )r8   r}   r5   rc   ÚunitÚsuffixÚcompleted_ratioÚtotal_ratioÚ	precisionÚcompleted_strÚ	total_strÚdownload_statusÚdownload_textr;   r;   r<   r�   ‡  s&   


ý
ÿzDownloadColumn.render)FN)rS   rT   rU   rV   r«   r   r)   r7   r+   r�   rY   r;   r;   r9   r<   rÌ   z  s    ÿÿÿþrÌ   c                   @   r´   )ÚTransferSpeedColumnz&Renders human readable transfer speed.r}   r~   r=   c                 C   s@   |j p|j}|du rtddd�S t t|ƒ¡}t|› d�dd�S )zShow data transfer speed.Nú?zprogress.data.speedr¸   z/s)Úfinished_speedr”   r+   r   rÇ   r³   )r8   r}   r”   Ú
data_speedr;   r;   r<   r�   ¢  s
   zTransferSpeedColumn.renderNr¾   r;   r;   r;   r<   rì   Ÿ  r¿   rì   c                   @   s$   e Zd ZU dZeed< 	 eed< dS )ÚProgressSamplez$Sample of progress for a given time.rƒ   r5   N)rS   rT   rU   rV   rW   r‡   r;   r;   r;   r<   rð   «  s   
 rð   c                   @   sœ  e Zd ZU dZeed< 	 eed< 	 eed< 	 eed< 	 eed< 	 dZ	e
e ed< 	 d	Zeed
< 	 eed�Zeeef ed< 	 edddd�Ze
e ed< 	 edddd�Ze
e ed< 	 dZe
e ed< 	 eeddd�Zee ed< eded�Zeed< 	 defdd„Zedefdd„ƒZedefdd„ƒZede
e fdd„ƒZedefdd „ƒZ edefd!d"„ƒZ!ede
e fd#d$„ƒZ"ede
e fd%d&„ƒZ#d)d'd(„Z$dS )*r~   z…Information regarding a progress task.

    This object should be considered read-only outside of the :class:`~Progress` class.

    r   rb   rc   r5   Ú	_get_timeNr»   TÚvisible)Údefault_factoryÚfieldsF)ÚdefaultÚinitÚreprÚ
start_timeÚ	stop_timerî   )ró   rö   r÷   Ú	_progress)r÷   ró   Ú_lockr=   c                 C   s   |   ¡ S )z(float: Get the current time, in seconds.)rñ   rH   r;   r;   r<   rg   ä  ó   zTask.get_timec                 C   ó
   | j duS )z#bool: Check if the task as started.N)rø   rH   r;   r;   r<   r²   è  ó   
zTask.startedc                 C   s   | j | j S )z)float: Get the number of steps remaining.)rc   r5   rH   r;   r;   r<   rÂ   í  s   zTask.remainingc                 C   s2   | j du rdS | jdur| j| j  S |  ¡ | j  S )z]Optional[float]: Time elapsed since task was started, or ``None`` if the task hasn't started.N)rø   rù   rg   rH   r;   r;   r<   r¼   ò  s
   

zTask.elapsedc                 C   rý   )zCheck if the task has finished.N)r»   rH   r;   r;   r<   r�   û  rþ   zTask.finishedc                 C   s.   | j sdS | j| j  d }tdtd|ƒƒ}|S )z,float: Get progress of task as a percentage.ç        ç      Y@)rc   r5   Úminr±   )r8   r5   r;   r;   r<   Ú
percentage   s
   zTask.percentagec                 C   s°   | j du rdS | j�D | j}|s	 W d  ƒ dS |d j|d j }|dkr0	 W d  ƒ dS t|ƒ}t|ƒ tdd„ |D ƒƒ}|| }|W  d  ƒ S 1 sQw   Y  dS )z=Optional[float]: Get the estimated speed in steps per second.Néÿÿÿÿr   c                 s   ó   � | ]}|j V  qd S rE   )r5   )Ú.0Úsampler;   r;   r<   Ú	<genexpr>  ó   € zTask.speed.<locals>.<genexpr>)rø   rû   rú   rƒ   ÚiterÚnextÚsum)r8   r0   Ú
total_timeÚiter_progressÚtotal_completedr”   r;   r;   r<   r”   	  s"   
ýú$õz
Task.speedc                 C   s*   | j rdS | j}|sdS t| j| ƒ}|S )zJOptional[float]: Get estimated time to completion, or ``None`` if no data.rÿ   N)r�   r”   r	   rÂ   )r8   r”   Úestimater;   r;   r<   rÁ     s   zTask.time_remainingc                 C   s   | j  ¡  d| _d| _dS )zReset progress.N)rú   Úclearr»   rî   rH   r;   r;   r<   Ú_reset&  s   

zTask._resetrR   )%rS   rT   rU   rV   r-   r‡   r™   rW   ÚGetTimeCallabler»   r   rò   r«   r   Údictrô   r   r   rø   rù   rî   r   rú   r   rð   r   rû   rg   Úpropertyr²   rÂ   r¼   r�   r  r”   rÁ   r  r;   r;   r;   r<   r~   ´  sZ   
 ÿ
r~   c                   @   s´  e Zd ZdZdddddddddddœ
deeef d	ee d
e	de
de
de	de	de	dee de	de	ddfdd„Zedefdd„ƒZedee fdd„ƒZedee fdd„ƒZede	fdd„ƒZdVdd„ZdVd d!„ZdWd"d#„Zd$eee  d%ee d&ee ddfd'd(„Z			)	*dXd+eee ee f d,ee
 d-ee d.ed/e
dee fd0d1„Zd-eddfd2d3„Z d-eddfd4d5„Z!ddddddd6œd-ed,ee
 d7ee
 d8ee
 d.ee d9ee	 d:e	d;e"ddfd<d=„Z#ddd>ddd?œd-ed@e	d,ee
 d7e$d9ee	 d.ee d;e"ddfdAdB„Z%dYd-ed8e
ddfdDdE„Z&dVdFdG„Z'de(fdHdI„Z)dee( fdJdK„Z*dLee de+fdMdN„Z,de(fdOdP„Z-		Q	>	dZd.ed@e	d,e
d7e$d9e	d;e"defdRdS„Z.d-eddfdTdU„Z/dS )[r1   a�  Renders an auto-updating progress bar(s).

    Args:
        console (Console, optional): Optional Console instance. Default will an internal Console instance writing to stdout.
        auto_refresh (bool, optional): Enable auto refresh. If disabled, you will need to call `refresh()`.
        refresh_per_second (Optional[float], optional): Number of times per second to refresh the progress information or None to use default (10). Defaults to None.
        speed_estimate_period: (float, optional): Period (in seconds) used to calculate the speed estimate. Defaults to 30.
        transient: (bool, optional): Clear the progress on exit. Defaults to False.
        redirect_stdout: (bool, optional): Enable redirection of stdout, so ``print`` may be used. Defaults to True.
        redirect_stderr: (bool, optional): Enable redirection of stderr. Defaults to True.
        get_time: (Callable, optional): A callable that gets the current time, or None to use Console.get_time. Defaults to None.
        disable (bool, optional): Disable progress display. Defaults to False
        expand (bool, optional): Expand tasks table to fit width. Defaults to False.
    NTr[   g      >@F)
re   rd   rh   Úspeed_estimate_periodrf   Úredirect_stdoutÚredirect_stderrrg   rm   Úexpandru   re   rd   rh   r  rf   r  r  rg   rm   r  r=   c       
      	   G   s¦   |d u s|dksJ dƒ‚t ƒ | _|ptdƒtƒ tdƒtƒ f| _|| _|	| _|
| _i | _	t
dƒ| _t|p5tƒ |||||| jd�| _|pE| jj| _| jj| _| jj| _d S )Nr   zrefresh_per_second must be > 0rn   ro   )re   rd   rh   rf   r  r  Úget_renderable)r   rû   rp   rr   rs   ru   r  rm   r  Ú_tasksr-   Ú_task_indexr%   r   r  Úlivere   rg   ÚprintÚlog)r8   re   rd   rh   r  rf   r  r  rg   rm   r  ru   r;   r;   r<   r7   =  s6   ÿü
ù	
zProgress.__init__c                 C   s   | j jS rE   )r  re   rH   r;   r;   r<   re   h  rü   zProgress.consolec                 C   ó8   | j � t| j ¡ ƒW  d  ƒ S 1 sw   Y  dS )zGet a list of Task instances.N)rû   Úlistr  ÚvaluesrH   r;   r;   r<   Útasksl  ó   $ÿzProgress.tasksc                 C   r  )zA list of task IDs.N)rû   r   r  ÚkeysrH   r;   r;   r<   Útask_idsr  r#  zProgress.task_idsc                 C   sZ   | j �  | js	 W d  ƒ dS tdd„ | j ¡ D ƒƒW  d  ƒ S 1 s&w   Y  dS )z'Check if all tasks have been completed.NTc                 s   r  rE   )r�   )r  r}   r;   r;   r<   r  ~  r  z$Progress.finished.<locals>.<genexpr>)rû   r  Úallr!  rH   r;   r;   r<   r�   x  s   þ$ýzProgress.finishedc                 C   s   | j s| jjdd� dS dS )zStart the progress display.T)r>   N)rm   r  rG   rH   r;   r;   r<   rG   €  s   ÿzProgress.startc                 C   s$   | j  ¡  | jjs| j ¡  dS dS )zStop the progress display.N)r  Ústopre   Úis_interactiver  rH   r;   r;   r<   r'  …  s   
ÿzProgress.stopc                 C   rD   rE   rF   rH   r;   r;   r<   rI   ‹  rJ   zProgress.__enter__rK   rL   rM   c                 C   s   |   ¡  d S rE   )r'  rP   r;   r;   r<   rQ   �  s   zProgress.__exit__rZ   r`   ra   rc   r2   rb   r3   c                 c   sä   � |du rt |tƒrtt|ƒƒ}n
td|›d�ƒ‚|}|du r'| j||d�}n| j||d� | jjrZt	| ||ƒ�}|D ]}|V  | j
d7  _
q;W d  ƒ dS 1 sSw   Y  dS | j}	| j}
|D ]}|V  |	|dƒ |
ƒ  qbdS )a‡  Track progress by iterating over a sequence.

        Args:
            sequence (Sequence[ProgressType]): A sequence of values you want to iterate over and track progress.
            total: (float, optional): Total number of steps. Default is len(sequence).
            task_id: (TaskID): Task to track. Default is new task.
            description: (str, optional): Description of task, if new task is created.
            update_period (float, optional): Minimum time (in seconds) between calls to update(). Defaults to 0.1.

        Returns:
            Iterable[ProgressType]: An iterable of values taken from the provided sequence.
        Nzunable to get size of z, please specify 'total'©rc   r   )r˜   r   rW   ÚlenÚ
ValueErrorÚadd_taskrA   r  rd   r/   r5   r?   r>   )r8   ra   rc   r2   rb   r3   Ú
task_totalÚtrack_threadÚvaluer?   r>   r;   r;   r<   rt   —  s2   €

ÿþ"ÿ
ýzProgress.trackc                 C   sZ   | j �  | j| }|jdu r|  ¡ |_W d  ƒ dS W d  ƒ dS 1 s&w   Y  dS )zéStart a task.

        Starts a task (used when calculating elapsed time). You may need to call this manually,
        if you called ``add_task`` with ``start=False``.

        Args:
            task_id (TaskID): ID of task.
        N)rû   r  rø   rg   )r8   r2   r}   r;   r;   r<   Ú
start_taskÈ  s   	

ý"þzProgress.start_taskc                 C   sT   | j � | j| }|  ¡ }|jdu r||_||_W d  ƒ dS 1 s#w   Y  dS )z†Stop a task.

        This will freeze the elapsed time on the task.

        Args:
            task_id (TaskID): ID of task.
        N)rû   r  rg   rø   rù   )r8   r2   r}   r‚   r;   r;   r<   Ú	stop_taskÖ  s   

"ûzProgress.stop_task)rc   r5   r?   rb   rò   r>   r5   r?   rò   r>   rô   c                K   s\  | j �™ | j| }	|	j}
|dur||	jkr||	_|	 ¡  |dur'|	 j|7  _|dur.||	_|dur5||	_|dur<||	_|	j |¡ |	j|
 }|  	¡ }|| j
 }|	j}|j}|rk|d j|k rk|ƒ  |rk|d j|k s_t|ƒdkrz|ƒ  t|ƒdksq|dkr†| t||ƒ¡ |	j|	jkr•|	jdu r•|	j|	_W d  ƒ n1 sŸw   Y  |r¬|  ¡  dS dS )a˜  Update information associated with a task.

        Args:
            task_id (TaskID): Task id (returned by add_task).
            total (float, optional): Updates task.total if not None.
            completed (float, optional): Updates task.completed if not None.
            advance (float, optional): Add a value to task.completed if not None.
            description (str, optional): Change task description if not None.
            visible (bool, optional): Set visible flag if not None.
            refresh (bool): Force a refresh of progress information. Default is False.
            **fields (Any): Additional data fields required for rendering.
        Nr   rß   )rû   r  r5   rc   r  rb   rò   rô   rA   rg   r  rú   Úpopleftrƒ   r*  Úappendrð   r»   r¼   r>   )r8   r2   rc   r5   r?   rb   rò   r>   rô   r}   Úcompleted_startÚupdate_completedr‚   Úold_sample_timerú   r2  r;   r;   r<   rA   å  sF   


ÿÿ€â ÿzProgress.updater   )rG   rc   r5   rò   rb   rG   c          
      K   sš   |   ¡ }| j�8 | j| }	|	 ¡  |r|nd|	_|dur||	_||	_|dur)||	_|r.||	_|dur5||	_	d|	_
W d  ƒ n1 sBw   Y  |  ¡  dS )aÔ  Reset a task so completed is 0 and the clock is reset.

        Args:
            task_id (TaskID): ID of task.
            start (bool, optional): Start the task after reset. Defaults to True.
            total (float, optional): New total steps in task, or None to use current total. Defaults to None.
            completed (int, optional): Number of steps completed. Defaults to 0.
            **fields (str): Additional data fields required for rendering.
        N)rg   rû   r  r  rø   rc   r5   rò   rô   rb   r»   r>   )
r8   r2   rG   rc   r5   rò   rb   rô   r‚   r}   r;   r;   r<   Úreset   s"   
ózProgress.resetr   c           
      C   s  |   ¡ }| j�w | j| }|j}| j|7  _|j| }|| j }|j}|j}	|r<|d j|k r<|	ƒ  |r<|d j|k s0t|ƒdkrK|	ƒ  t|ƒdksB| 	t
||ƒ¡ |j|jkrn|jdu rv|j|_|j|_W d  ƒ dS W d  ƒ dS W d  ƒ dS 1 s�w   Y  dS )z«Advance task by a number of steps.

        Args:
            task_id (TaskID): ID of task.
            advance (float): Number of steps to advance. Default is 1.
        r   rß   N)rg   rû   r  r5   r  rú   r2  rƒ   r*  r3  rð   rc   r»   r¼   r”   rî   )
r8   r2   r?   r‚   r}   r4  r5  r6  rú   r2  r;   r;   r<   r?   E  s0   


ÿÿ
ðò"òzProgress.advancec                 C   s$   | j s| jjr| j ¡  dS dS dS )z*Refresh (render) the progress information.N)rm   r  Ú
is_startedr>   rH   r;   r;   r<   r>   _  s   ÿzProgress.refreshc                 C   s   t |  ¡ Ž }|S )z*Get a renderable for the progress display.)r"   Úget_renderables)r8   r„   r;   r;   r<   r  d  s   zProgress.get_renderablec                 c   s   � |   | j¡}|V  dS )z5Get a number of renderables for the progress display.N)Úmake_tasks_tabler"  )r8   Útabler;   r;   r<   r9  i  s   €
zProgress.get_renderablesr"  c                    sR   dd„ | j D ƒ}tj|d| jdœŽ}|D ]‰ ˆ jr&|j‡ fdd„| j D ƒŽ  q|S )z×Get a table to render the Progress display.

        Args:
            tasks (Iterable[Task]): An iterable of Task instances, one per row of the table.

        Returns:
            Table: A table instance.
        c                 s   s0   � | ]}t |tƒrtd d�n| ¡  ¡ V  qdS )Tr¥   N)r˜   r™   r)   r|   Úcopy)r  Ú_columnr;   r;   r<   r  w  s   € ýÿ

üz,Progress.make_tasks_table.<locals>.<genexpr>)r   r   )Úpaddingr  c                 3   s.   � | ]}t |tƒr|jˆ d �n|ˆ ƒV  qdS )r§   N)r˜   r™   r¨   )r  Úcolumnr§   r;   r<   r  „  s   € ýÿ
ü)ru   r*   Úgridr  rò   Úadd_row)r8   r"  Útable_columnsr;  r;   r§   r<   r:  n  s   	ú
úÿ€
zProgress.make_tasks_tablec                 C   s2   | j � |  ¡ W  d  ƒ S 1 sw   Y  dS )z+Makes the Progress class itself renderable.N)rû   r  rH   r;   r;   r<   Ú__rich__�  s   $ÿzProgress.__rich__r   c           	      K   s†   | j �2 t| j|||||| j| j d�}|| j| j< |r!|  | j¡ | j}tt| jƒd ƒ| _W d  ƒ n1 s8w   Y  |  ¡  |S )aÞ  Add a new 'task' to the Progress display.

        Args:
            description (str): A description of the task.
            start (bool, optional): Start the task immediately (to calculate elapsed time). If set to False,
                you will need to call `start` manually. Defaults to True.
            total (float, optional): Number of total steps in the progress if know. Defaults to 100.
            completed (int, optional): Number of steps completed so far.. Defaults to 0.
            visible (bool, optional): Enable display of the task. Defaults to True.
            **fields (str): Additional data fields required for rendering.

        Returns:
            TaskID: An ID you can use when calling `update`.
        )rò   rô   rñ   rû   r   N)	rû   r~   r  rg   r  r0  r-   r³   r>   )	r8   rb   rG   rc   r5   rò   rô   r}   Únew_task_indexr;   r;   r<   r,  ”  s&   ø
ñzProgress.add_taskc                 C   s4   | j � | j|= W d  ƒ dS 1 sw   Y  dS )z]Delete a task if it exists.

        Args:
            task_id (TaskID): A task ID.

        N)rû   r  )r8   r2   r;   r;   r<   Úremove_task¾  s   
"ÿzProgress.remove_taskrR   )r=   r1   )NNrZ   r`   )r   )Tr   r   T)0rS   rT   rU   rV   r   r™   rv   r   r   r«   rW   r  r7   r  re   r   r~   r"  r-   r%  r�   rG   r'  rI   r   rX   r   rQ   r   r.   r   rt   r0  r1  r   rA   r³   r7  r?   r>   r!   r  r9  r*   r:  rC  r,  rE  r;   r;   r;   r<   r1   -  s*   ô
þýüûúùø	÷
öõô
ó+



þýü
ûúþýüûú
ù1÷þüûúùø	÷
ö
õ?øþüûúùø	÷

ö%
!úþýüûúù
ø*r1   Ú__main__)ÚPanel)ÚRule)ÚSyntax)r*   a~  def loop_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
    """Iterate and generate a tuple with a flag for last value."""
    iter_values = iter(values)
    try:
        previous_value = next(iter_values)
    except StopIteration:
        return
    for value in iter_values:
        yield False, previous_value
        previous_value = value
    yield True, previous_valueÚpython)Úline_numbersÚfooÚbarÚbazÚ1Ú2Ú3z:Text may be printed while the progress bars are rendering.z(In fact, [i]any[/i] renderable will workzSuch as [magenta]tables[/]...zPretty printed structures...ÚexamplezPretty printed)Útyperž   z	Syntax...zGive it a try!)Úcycle)Úrecordrn   ro   )re   rf   z[red]Downloadingrß   r)  z[green]Processingz[yellow]Thinking)rc   rG   rÀ   )r?   g333333Ó?g{®Gáz„?éd   )rZ   NTNFNr[   r\   r]   r^   r_   r`   F)mÚabcr   r   Úcollectionsr   Úcollections.abcr   Údataclassesr   r   Údatetimer   Úmathr	   Ú	threadingr
   r   r   Útypesr   Útypingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r‰   r   r   re   r   r    r!   r"   r¤   r#   Újupyterr$   r  r%   Úprogress_barr&   r—   r'   ri   r(   r;  r)   r*   rž   r+   r,   r³   r-   r.   rW   r  r/   r™   r«   rt   rv   rˆ   rŽ   rp   rr   rµ   rs   rÄ   rË   rÌ   rì   rð   r~   r1   rS   ÚrandomÚtimeÚpanelrG  ÚrulerH  ÚsyntaxrI  rA  Úprogress_renderablesÚ	itertoolsrT  Úexamplesr0   r,  Útask1Útask2Útask3r�   rA   ÚsleepÚrandintr  r
  r;   r;   r;   r<   Ú<module>   s   @
*òÿþýüûúùø	÷
öõôóò
ñD*2*		%	x   
ó÷
ø	

ûñ$ñÑ