o
    iY8                     @  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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/m0Z0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z> dgZ?e'Z@eAeddZBd%ddZCedZDG dd dZEG dd de1ZFed d!d"ZGG d#d$ d$eeG ZHdS )&z
Progress bar implementation on top of prompt_toolkit.

::

    with ProgressBar(...) as pb:
        for item in pb(data):
            ...
    )annotationsN)CallableGenericIterableIteratorListOptional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/    r7   e/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/prompt_toolkit/shortcuts/progress_bar/base.py_clearJ   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.Nr7   r6   r,   r7   r8   
_interruptP   s   
z'create_key_bindings.<locals>._interrupt)r/   r0   r.   r1   )r   add)r,   kbr9   r;   r7   r:   r8   create_key_bindingsC   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.   r1   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.   r1   c                   S  s   t t  tj d S r2   )oskillgetpidsignalSIGINTr7   r7   r7   r8   !keyboard_interrupt_to_main_thread   s   z?ProgressBar.__init__.<locals>.keyboard_interrupt_to_main_threadFr.   r1   )r@   r)   rA   rC   countersrD   rF   r,   r&   rJ   r   rL   rN   _thread_has_sigwinch	threadingEvent_app_started)selfr@   rA   rC   rD   rF   r,   rH   rJ   rL   rN   rU   r7   r7   r8   __init__w   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 r2   r@   r7   r]   r7   r8   <lambda>       z'ProgressBar.__enter__.<locals>.<lambda>r'   zclass:progressbar,title)heightrD   c                     
    j d uS r2   r`   r7   ra   r7   r8   rb         
 )filterc                     r_   r2   rC   r7   ra   r7   r8   rb      rc   zclass:bottom-toolbar.text)rD   zclass:bottom-toolbar)rD   rd   c                     re   r2   rh   r7   ra   r7   r8   rb      rf   	formatterr(   r.   r!   c                   s   | j  dS )N)progress_bar)	get_width)ri   ra   r7   r8   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]   rl   r7   r8   
<listcomp>   s    
z)ProgressBar.__enter__.<locals>.<listcomp>g?c                     s   t t jt jdS )N)	preferredmax)r"   lenrW   r7   ra   r7   r8   rb      s    )rd   g333333?)min_redraw_intervallayoutrD   rF   refresh_intervalrJ   rL   rN   r1   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)r3   runr\   setBaseException	traceback	print_excprint)era   r7   r8   r}      s   z"ProgressBar.__enter__.<locals>.run)targetargs)ri   r(   r.   r!   rV   )r   r   r   r   r   r   rA   r   r   r   r   rD   rF   rJ   rL   rN   r3   contextvarscopy_contextrZ   Threadr}   rX   start)r]   title_toolbarrC   progress_controlsr}   ctxr7   rt   r8   	__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 r2   )	r\   waitr3   
is_runningloopcall_soon_threadsafeexitrX   join)r]   r   r7   r7   r8   __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   )ProgressBarCounterrW   append)r]   r   r   r   r   counterr7   r7   r8   __call__   s
   
zProgressBar.__call__c                 C  s   | j   d S r2   )r3   
invalidatera   r7   r7   r8   r     s   zProgressBar.invalidate)
NNNNNNNNNN)r@   r   rA   rB   rC   r   rD   rE   rF   rG   r,   r-   rH   rI   rJ   rK   rL   rM   rN   rO   r.   r1   )r.   r*   )r   r   r.   r1   Nr   FN)
r   r   r   r   r   r   r   r   r.   r   rV   )	__name__
__module____qualname____doc__r^   r   r   r   r   r7   r7   r7   r8   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 )ro   z,
    User control for the progress bar.
    rj   r*   ri   r(   r,   r-   r.   r1   c                 C  s   || _ || _t|| _d S r2   )rj   ri   r>   _key_bindings)r]   rj   ri   r,   r7   r7   r8   r^     s   z_ProgressControl.__init__rn   intrd   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 r2   r7   )r   itemsr7   r8   get_line0  s   z1_ProgressControl.create_content.<locals>.get_lineF)r   
line_countshow_cursor)r   r   r.   r   )rj   rW   ri   formatr   r   r   r   r   r   rx   )r]   rn   rd   prtextr   r7   r   r8   create_content$  s   z_ProgressControl.create_contentr   c                 C  s   dS )NTr7   ra   r7   r7   r8   is_focusable5  s   z_ProgressControl.is_focusabler   c                 C     | j S r2   )r   ra   r7   r7   r8   get_key_bindings8  s   z!_ProgressControl.get_key_bindingsN)rj   r*   ri   r(   r,   r-   r.   r1   )rn   r   rd   r   r.   r   r.   r   )r.   r   )r   r   r   r   r^   r   r   r   r7   r7   r7   r8   ro     s    



ro   _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   Frj   r*   r   Iterable[_CounterItem] | Noner   r   r   r   r   r   r.   r1   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_timerj   r   items_completedr   r   _donerx   r   r
   r   	TypeError)r]   rj   r   r   r   r   r7   r7   r8   r^   D  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]   itemr7   r7   r8   __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   rj   r   ra   r7   r7   r8   r   n  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   ra   r7   r7   r8   r   w  s   
zProgressBarCounter.donevaluec                 C  s0   || _ || _|r| jr| jj|  d S d S d S r2   )r   r   r   rj   rW   remover]   r   r7   r7   r8   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   ra   r7   r7   r8   r     s   
zProgressBarCounter.stoppedc                 C  s(   |r| j stj | _ d S d S d | _ d S r2   )r   r   r   r   r7   r7   r8   r     s
   
floatc                 C  s$   | j d u rdS | jd t| j d S )Nr   d   r'   )r   r   rw   ra   r7   r7   r8   
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   ra   r7   r7   r8   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   ra   r7   r7   r8   	time_left  s
   
zProgressBarCounter.time_leftr   )rj   r*   r   r   r   r   r   r   r   r   r.   r1   )r.   r   rV   r   )r   r   r.   r1   )r.   r   )r.   r   )r.   r   )r   r   r   r   r^   r   r   propertyr   setterr   r   r   r   r7   r7   r7   r8   r   ?  s.    

			r   )r,   r-   r.   r   )Ir   
__future__r   r   r   rp   rP   rS   rZ   r   typingr   r   r   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&   rA   r(   r)   __all__r0   getattr	_SIGWINCHr>   r?   r*   ro   r   r   r7   r7   r7   r8   <module>   sD    	4 
 :'