o
    fB8                     @  s  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZmZmZmZmZmZ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mZm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)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z;m<Z< dgZ=e%Z>e?eddZ@d%ddZAedZBG dd dZCG dd de/ZDed d!d"ZEG d#d$ d$eeE ZFdS )&z
Progress bar implementation on top of prompt_toolkit.

::

    with ProgressBar(...) as pb:
        for item in pb(data):
            ...
    )annotationsN)	CallableGenericIterableIteratorSequenceSizedTextIOTypeVarcast)Application)get_app_session)	Conditionis_donerenderer_height_is_known)AnyFormattedTextStyleAndTextTuplesto_formatted_text)Input)KeyBindings)KeyPressEvent)ConditionalContainerFormattedTextControlHSplitLayoutVSplitWindow)	UIContent	UIControl)AnyDimensionD)
ColorDepthOutput)	BaseStyle)in_main_thread   )	Formattercreate_default_formattersProgressBarSIGWINCHcancel_callbackCallable[[], None] | Nonereturnr   c                   s>   t  }|dddd} dur|d	d fd
d}|S )zu
    Key bindings handled by the progress bar.
    (The main thread is not supposed to handle any key bindings.)
    zc-leventEr,   Nonec                 S  s   | j j  d S N)apprendererclearr-    r5   ]/var/www/html/venv/lib/python3.10/site-packages/prompt_toolkit/shortcuts/progress_bar/base.py_clearI   s   z#create_key_bindings.<locals>._clearNzc-cc                   s    dusJ    dS )zMKill the 'body' of the progress bar, but only if we run from the main thread.Nr5   r4   r*   r5   r6   
_interruptO   s   
z'create_key_bindings.<locals>._interrupt)r-   r.   r,   r/   )r   add)r*   kbr7   r9   r5   r8   r6   create_key_bindingsB   s   r<   _Tc                   @  sb   e Zd ZdZ										d.d/ddZd0ddZd1ddZ		 	!	d2d3d*d+Zd4d,d-ZdS )5r(   a.  
    Progress bar context manager.

    Usage ::

        with ProgressBar(...) as pb:
            for item in pb(data):
                ...

    :param title: Text to be displayed above the progress bars. This can be a
        callable or formatted text as well.
    :param formatters: List of :class:`.Formatter` instances.
    :param bottom_toolbar: Text to be displayed in the bottom toolbar. This
        can be a callable or formatted text.
    :param style: :class:`prompt_toolkit.styles.BaseStyle` instance.
    :param key_bindings: :class:`.KeyBindings` instance.
    :param cancel_callback: Callback function that's called when control-c is
        pressed by the user. This can be used for instance to start "proper"
        cancellation if the wrapped code supports it.
    :param file: The file object used for rendering, by default `sys.stderr` is used.

    :param color_depth: `prompt_toolkit` `ColorDepth` instance.
    :param output: :class:`~prompt_toolkit.output.Output` instance.
    :param input: :class:`~prompt_toolkit.input.Input` instance.
    Ntitler   
formattersSequence[Formatter] | Nonebottom_toolbarstyleBaseStyle | Nonekey_bindingsKeyBindings | Noner*   r+   fileTextIO | Nonecolor_depthColorDepth | NoneoutputOutput | NoneinputInput | Noner,   r/   c                 C  s   || _ |pt | _|| _g | _|| _|| _|| _| jd u r(t r(ddd}|| _|| _	|	p0t
 j| _|
p7t
 j| _d | _d| _t | _d S )Nr,   r/   c                   S  s   t t  tj d S r0   )oskillgetpidsignalSIGINTr5   r5   r5   r6   !keyboard_interrupt_to_main_thread   s   z?ProgressBar.__init__.<locals>.keyboard_interrupt_to_main_threadFr,   r/   )r>   r'   r?   rA   countersrB   rD   r*   r$   rH   r   rJ   rL   _thread_has_sigwinch	threadingEvent_app_started)selfr>   r?   rA   rB   rD   r*   rF   rH   rJ   rL   rS   r5   r5   r6   __init__v   s    
zProgressBar.__init__c              
     s  t tt fdddddt fddd}t tt fddd	d
dddt t@ t fdd@ d}d fdd fdd jD }tdtt	|t
| fdddt |g j jd j j jd _d  fdd}t }tj|j|fd _ j   S )!Nc                         j S r0   r>   r5   r[   r5   r6   <lambda>       z'ProgressBar.__enter__.<locals>.<lambda>r%   zclass:progressbar,title)heightrB   c                     
    j d uS r0   r^   r5   r_   r5   r6   r`         
 )filterc                     r]   r0   rA   r5   r_   r5   r6   r`      ra   zclass:bottom-toolbar.text)rB   zclass:bottom-toolbar)rB   rb   c                     rc   r0   rf   r5   r_   r5   r6   r`      rd   	formatterr&   r,   r   c                   s   | j  dS )N)progress_bar)	get_width)rg   r_   r5   r6   width_for_formatter   s   z2ProgressBar.__enter__.<locals>.width_for_formatterc                   s*   g | ]}t t | jt|d qS ))contentwidth)r   _ProgressControlr*   	functoolspartial).0fr[   rj   r5   r6   
<listcomp>   s    
z)ProgressBar.__enter__.<locals>.<listcomp>g?c                     s   t t jt jdS )N)	preferredmax)r    lenrU   r5   r_   r5   r6   r`      s    )rb   g333333?)min_redraw_intervallayoutrB   rD   refresh_intervalrH   rJ   rL   r/   c               
     sP   z j j jjd W d S  ty' }  zt  t|  W Y d } ~ d S d } ~ ww )N)pre_run)r1   runrZ   setBaseException	traceback	print_excprint)er_   r5   r6   r{      s   z"ProgressBar.__enter__.<locals>.run)targetargs)rg   r&   r,   r   rT   )r   r   r   r   r   r   r?   r   r   r   r   rB   rD   rH   rJ   rL   r1   contextvarscopy_contextrX   Threadr{   rV   start)r[   title_toolbarrA   progress_controlsr{   ctxr5   rr   r6   	__enter__   sh   	

zProgressBar.__enter__aobjectc                 G  sL   | j   | jjr| jjd ur| jj| jj | jd ur$| j  d S d S r0   )	rZ   waitr1   
is_runningloopcall_soon_threadsafeexitrV   join)r[   r   r5   r5   r6   __exit__   s   

zProgressBar.__exit__ FdataIterable[_T] | Nonelabelremove_when_donebooltotal
int | NoneProgressBarCounter[_T]c                 C  s"   t | ||||d}| j| |S )aL  
        Start a new counter.

        :param label: Title text or description for this progress. (This can be
            formatted text as well).
        :param remove_when_done: When `True`, hide this progress bar.
        :param total: Specify the maximum value if it can't be calculated by
            calling ``len``.
        )r   r   r   )ProgressBarCounterrU   append)r[   r   r   r   r   counterr5   r5   r6   __call__   s
   
zProgressBar.__call__c                 C  s   | j   d S r0   )r1   
invalidater_   r5   r5   r6   r     s   zProgressBar.invalidate)
NNNNNNNNNN)r>   r   r?   r@   rA   r   rB   rC   rD   rE   r*   r+   rF   rG   rH   rI   rJ   rK   rL   rM   r,   r/   )r,   r(   )r   r   r,   r/   Nr   FN)
r   r   r   r   r   r   r   r   r,   r   rT   )	__name__
__module____qualname____doc__r\   r   r   r   r   r5   r5   r5   r6   r(   [   s*    
*
Mc                   @  s8   e Zd ZdZdd
dZdddZdddZdddZdS )rm   z,
    User control for the progress bar.
    rh   r(   rg   r&   r*   r+   r,   r/   c                 C  s   || _ || _t|| _d S r0   )rh   rg   r<   _key_bindings)r[   rh   rg   r*   r5   r5   r6   r\     s   z_ProgressControl.__init__rl   intrb   r   c              	     sv   g  | j jD ]$}z| j| j ||}W n ty"   t  d}Y nw  t| qd
 fdd}t	|t
 dd	S )NERRORir   r,   r   c                   s    |  S r0   r5   )r   itemsr5   r6   get_line/  s   z1_ProgressControl.create_content.<locals>.get_lineF)r   
line_countshow_cursor)r   r   r,   r   )rh   rU   rg   formatr}   r~   r   r   r   r   rv   )r[   rl   rb   prtextr   r5   r   r6   create_content#  s   z_ProgressControl.create_contentr   c                 C  s   dS )NTr5   r_   r5   r5   r6   is_focusable4  s   z_ProgressControl.is_focusabler   c                 C     | j S r0   )r   r_   r5   r5   r6   get_key_bindings7  s   z!_ProgressControl.get_key_bindingsN)rh   r(   rg   r&   r*   r+   r,   r/   )rl   r   rb   r   r,   r   r,   r   )r,   r   )r   r   r   r   r\   r   r   r   r5   r5   r5   r6   rm     s    



rm   _CounterItemT)	covariantc                   @  s   e Zd ZdZ				d(d)ddZd*ddZd+ddZed,ddZej	d-ddZed,ddZ
e
j	d-ddZ
ed.d d!Zed/d#d$Zed0d&d'ZdS )1r   zL
    An individual counter (A progress bar can have multiple counters).
    Nr   Frh   r(   r   Iterable[_CounterItem] | Noner   r   r   r   r   r   r,   r/   c                 C  s~   t j  | _d | _|| _|| _d| _|| _|| _d| _	|  |d u r:zt
tt|| _W d S  ty9   d | _Y d S w || _d S )Nr   F)datetimenow
start_time	stop_timerh   r   items_completedr   r   _donerv   r   r   r   	TypeError)r[   rh   r   r   r   r   r5   r5   r6   r\   C  s    
zProgressBarCounter.__init__Iterator[_CounterItem]c                 c  sJ    | j d ur!z| j D ]	}|V  |   q
d| _W d| _d S d| _w td)NTz No data defined to iterate over.)r   item_completeddonestoppedNotImplementedError)r[   itemr5   r5   r6   __iter__]  s   


zProgressBarCounter.__iter__c                 C  s   |  j d7  _ | j  dS )z
        Start handling the next item.

        (Can be called manually in case we don't have a collection to loop through.)
        r%   N)r   rh   r   r_   r5   r5   r6   r   m  s   z!ProgressBarCounter.item_completedc                 C  r   )a6  Whether a counter has been completed.

        Done counter have been stopped (see stopped) and removed depending on
        remove_when_done value.

        Contrast this with stopped. A stopped counter may be terminated before
        100% completion. A done counter has reached its 100% completion.
        )r   r_   r5   r5   r6   r   v  s   
zProgressBarCounter.donevaluec                 C  s0   || _ || _|r| jr| jj|  d S d S d S r0   )r   r   r   rh   rU   remover[   r   r5   r5   r6   r     s
   
c                 C  s
   | j duS )a  Whether a counter has been stopped.

        Stopped counters no longer have increasing time_elapsed. This distinction is
        also used to prevent the Bar formatter with unknown totals from continuing to run.

        A stopped counter (but not done) can be used to signal that a given counter has
        encountered an error but allows other counters to continue
        (e.g. download X of Y failed). Given how only done counters are removed
        (see remove_when_done) this can help aggregate failures from a large number of
        successes.

        Contrast this with done. A done counter has reached its 100% completion.
        A stopped counter may be terminated before 100% completion.
        N)r   r_   r5   r5   r6   r     s   
zProgressBarCounter.stoppedc                 C  s(   |r| j stj | _ d S d S d | _ d S r0   )r   r   r   r   r5   r5   r6   r     s
   
floatc                 C  s$   | j d u rdS | jd t| j d S )Nr   d   r%   )r   r   ru   r_   r5   r5   r6   
percentage  s   
zProgressBarCounter.percentagedatetime.timedeltac                 C  s&   | j du rtj | j S | j | j S )zH
        Return how much time has been elapsed since the start.
        N)r   r   r   r   r_   r5   r5   r6   time_elapsed  s   
zProgressBarCounter.time_elapseddatetime.timedelta | Nonec                 C  s@   | j du s| js
dS | js| jrtdS | jd| j  | j S )z7
        Timedelta representing the time left.
        Nr   r   )r   r   r   r   r   	timedeltar   r_   r5   r5   r6   	time_left  s
   
zProgressBarCounter.time_leftr   )rh   r(   r   r   r   r   r   r   r   r   r,   r/   )r,   r   rT   r   )r   r   r,   r/   )r,   r   )r,   r   )r,   r   )r   r   r   r   r\   r   r   propertyr   setterr   r   r   r   r5   r5   r5   r6   r   >  s.    

			r   )r*   r+   r,   r   )Gr   
__future__r   r   r   rn   rN   rQ   rX   r~   typingr   r   r   r   r   r   r	   r
   r   prompt_toolkit.applicationr   "prompt_toolkit.application.currentr   prompt_toolkit.filtersr   r   r   prompt_toolkit.formatted_textr   r   r   prompt_toolkit.inputr   prompt_toolkit.key_bindingr   (prompt_toolkit.key_binding.key_processorr   prompt_toolkit.layoutr   r   r   r   r   r   prompt_toolkit.layout.controlsr   r   prompt_toolkit.layout.dimensionr   r    prompt_toolkit.outputr!   r"   prompt_toolkit.stylesr#   prompt_toolkit.utilsr$   r?   r&   r'   __all__r.   getattr	_SIGWINCHr<   r=   r(   rm   r   r   r5   r5   r5   r6   <module>   sD    
, 
 :'