
    fB8                       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ZA ed      ZB G d d      ZC G d de/      ZD edd      ZE G d deeE         ZFy)!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SIGWINCHc                     t               }|j                  d      dd       } |j                  d      d fd       }|S )zu
    Key bindings handled by the progress bar.
    (The main thread is not supposed to handle any key bindings.)
    zc-lc                L    | j                   j                  j                          y N)apprendererclear)events    e/var/www/cvtools/html/venv/lib/python3.12/site-packages/prompt_toolkit/shortcuts/progress_bar/base.py_clearz#create_key_bindings.<locals>._clearI   s    		  "    zc-cc                    J          y)zMKill the 'body' of the progress bar, but only if we run from the main thread.N )r1   cancel_callbacks    r2   
_interruptz'create_key_bindings.<locals>._interruptO   s     #...r4   )r1   EreturnNone)r   add)r7   kbr3   r8   s   `   r2   create_key_bindingsr>   B   sO    
 
BVVE]# # "		 
	
 Ir4   _Tc                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	dZd
dZddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZddZy)r)   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.
    Nc                   || _         |xs
 t               | _        || _        g | _        || _        || _        || _        | j                  t               rdd}|| _        || _	        |	xs t               j                  | _        |
xs t               j                  | _        d | _        d| _        t        j                          | _        y )Nc                 p    t        j                  t        j                         t        j                         y r-   )oskillgetpidsignalSIGINTr6   r4   r2   !keyboard_interrupt_to_main_threadz?ProgressBar.__init__.<locals>.keyboard_interrupt_to_main_thread   s    		V]]3r4   Fr:   r;   )titler(   
formattersbottom_toolbarcountersstylekey_bindingsr7   r%   color_depthr   outputinput_thread_has_sigwinch	threadingEvent_app_started)selfrJ   rK   rL   rN   rO   r7   filerP   rQ   rR   rH   s               r2   __init__zProgressBar.__init__v   s     
$C(A(C,:<
(.
 'N,<4 $ED  '8 1 8 85o/55
04"%OO-r4   c                @    t        t        t         fd      dd      t         fd            }t        t        t         fdd	      d
d      t         t
        z  t         fd      z        }d fd} j                  D cg c]8  }t        t         | j                        t        j                  ||            : }}t        dt        t        |t        | fd      t               |g             j                   j                   d j"                   j$                   j&                         _        d fd}t+        j,                         }t/        j0                  |j2                  |f       _         j4                  j7                           S c c}w )Nc                      j                   S r-   rJ   rX   s   r2   <lambda>z'ProgressBar.__enter__.<locals>.<lambda>   s    TZZ r4   r&   zclass:progressbar,title)heightrN   c                       j                   d uS r-   r]   r^   s   r2   r_   z'ProgressBar.__enter__.<locals>.<lambda>   s    TZZt%; r4   )filterc                      j                   S r-   rL   r^   s   r2   r_   z'ProgressBar.__enter__.<locals>.<lambda>   s    D// r4   zclass:bottom-toolbar.text)rN   zclass:bottom-toolbar)rN   r`   c                       j                   d uS r-   rd   r^   s   r2   r_   z'ProgressBar.__enter__.<locals>.<lambda>   s     3 34 ? r4   c                (    | j                        S )N)progress_bar)	get_width)	formatterrX   s    r2   width_for_formatterz2ProgressBar.__enter__.<locals>.width_for_formatter   s     &&D&99r4   )contentwidthg?c                 j    t        t         j                        t         j                              S )N)	preferredmax)r!   lenrM   r^   s   r2   r_   z'ProgressBar.__enter__.<locals>.<lambda>   s#    1*-dmm*<#dmmBT, r4   )r`   g333333?)min_redraw_intervallayoutrN   rO   refresh_intervalrP   rQ   rR   c                     	 j                   j                  j                  j                         y # t        $ r)} t        j                          t        |        Y d } ~ y d } ~ ww xY w)N)pre_run)r.   runrW   setBaseException	traceback	print_excprint)erX   s    r2   rv   z"ProgressBar.__enter__.<locals>.run   sK    T%6%6%:%:;  ##%as   04 	A&A!!A&)targetargs)ri   r'   r:   r    rI   )r   r   r   r   r   r   rK   _ProgressControlr7   	functoolspartialr   r   r   r   rN   rO   rP   rQ   rR   r.   contextvarscopy_contextrU   Threadrv   rS   start)rX   title_toolbarrL   rj   fprogress_controlsrv   ctxs   `       r2   	__enter__zProgressBar.__enter__   s   ,$%78/
 ;<
 .$/7R - 8&'?@A
	: __

 	 (q$2F2FG''(;Q?
 
 '2 $%-$ &
 **** ((;;**-'
4	 $/#;#;#= ''swwcVD]
s   	=Fc                P   | j                   j                          | j                  j                  rO| j                  j                  9| j                  j                  j                  | j                  j                         | j                  | j                  j                          y y r-   )	rW   waitr.   
is_runningloopcall_soon_threadsafeexitrS   join)rX   as     r2   __exit__zProgressBar.__exit__   so     	  88488==#<HHMM..txx}}=<<#LL $r4   c                \    t        | ||||      }| j                  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``.
        )labelremove_when_donetotal)ProgressBarCounterrM   append)rX   datar   r   r   counters         r2   __call__zProgressBar.__call__   s3      %$e6Fe
 	W%r4   c                8    | j                   j                          y r-   )r.   
invalidater^   s    r2   r   zProgressBar.invalidate  s    r4   )
NNNNNNNNNN)rJ   r   rK   zSequence[Formatter] | NonerL   r   rN   zBaseStyle | NonerO   zKeyBindings | Noner7   Callable[[], None] | NonerY   zTextIO | NonerP   zColorDepth | NonerQ   zOutput | NonerR   zInput | Noner:   r;   )r:   r)   )r   objectr:   r;   N FN)
r   zIterable[_T] | Noner   r   r   boolr   
int | Noner:   zProgressBarCounter[_T]rI   )	__name__
__module____qualname____doc__rZ   r   r   r   r   r6   r4   r2   r)   r)   [   s    8 #'15+/"&+/59")- $"(.(. /(. )	(.
  (. )(. 3(. (. '(. (. (. 
(.TKZ  %)"$!& !   	
  
 ,r4   c                  @    e Zd ZdZ	 	 	 	 	 	 	 	 ddZddZd	dZd
dZy)r   z,
    User control for the progress bar.
    c                @    || _         || _        t        |      | _        y r-   )rg   ri   r>   _key_bindings)rX   rg   ri   r7   s       r2   rZ   z_ProgressControl.__init__  s      )"0Ar4   c                F   g | j                   j                  D ]D  }	 | j                  j                  | j                   ||      }j                  t        |             F dfd}t        |t              d      S # t        $ r t        j                          d}Y Zw xY w)NERRORc                    |    S r-   r6   )iitemss    r2   get_linez1_ProgressControl.create_content.<locals>.get_line/  s    8Or4   F)r   
line_countshow_cursor)r   intr:   r   )rg   rM   ri   formatrx   ry   rz   r   r   r   rp   )rX   rl   r`   prtextr   r   s         @r2   create_contentz_ProgressControl.create_content#  s    *,##,, 	2B~~,,T->->EJ
 LL*401	2	 (s5zuUU ! ##%s   'A>>B B c                     y)NTr6   r^   s    r2   is_focusablez_ProgressControl.is_focusable4  s    r4   c                    | j                   S r-   )r   r^   s    r2   get_key_bindingsz!_ProgressControl.get_key_bindings7  s    !!!r4   N)rg   r)   ri   r'   r7   r   r:   r;   )rl   r   r`   r   r:   r   r:   r   )r:   r   )r   r   r   r   rZ   r   r   r   r6   r4   r2   r   r     sG    B!B B 3	B
 
BV""r4   r   _CounterItemT)	covariantc                      e Zd ZdZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZddZedd       Zej                  dd       Zedd       Z
e
j                  dd	       Z
edd
       Zedd       Zedd       Zy)r   zL
    An individual counter (A progress bar can have multiple counters).
    Nc                .   t         j                   j                         | _        d | _        || _        || _        d| _        || _        || _        d| _	        |  | 	 t        t        t        |            | _        y || _        y # t        $ r
 d | _        Y y w xY w)Nr   F)datetimenow
start_time	stop_timerg   r   items_completedr   r   _donerp   r   r	   r   	TypeError)rX   rg   r   r   r   r   s         r2   rZ   zProgressBarCounter.__init__C  s     #++//137(	 
 0
=" eT!23
 DJ  "!
"s   B BBc              #     K   | j                   5	 | j                   D ]  }| | j                           d| _        d| _        y t	        d      # d| _        w xY ww)NTz No data defined to iterate over.)r   item_completeddonestoppedNotImplementedError)rX   items     r2   __iter__zProgressBarCounter.__iter__]  sa     99 
$ II *DJ'')*
 !	  $%&HII  $s   A,A A	AAc                b    | xj                   dz  c_         | j                  j                          y)z
        Start handling the next item.

        (Can be called manually in case we don't have a collection to loop through.)
        r&   N)r   rg   r   r^   s    r2   r   z!ProgressBarCounter.item_completedm  s'     	!$$&r4   c                    | j                   S )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^   s    r2   r   zProgressBarCounter.donev  s     zzr4   c                    || _         || _        |r3| j                  r&| j                  j                  j                  |        y y y r-   )r   r   r   rg   rM   removerX   values     r2   r   zProgressBarCounter.done  s<    
T**&&--d3 +5r4   c                    | 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^   s    r2   r   zProgressBarCounter.stopped  s      ~~T))r4   c                x    |r1| j                   s$t        j                  j                         | _         y y d | _         y r-   )r   r   r   r   s     r2   r   zProgressBarCounter.stopped  s0    >>!)!2!2!6!6!8 " "DNr4   c                h    | j                   y| j                  dz  t        | j                   d      z  S )Nr   d   r&   )r   r   ro   r^   s    r2   
percentagezProgressBarCounter.percentage  s0    ::''#-DJJ0BBBr4   c                    | j                   +t        j                  j                         | j                  z
  S | j                   | j                  z
  S )zH
        Return how much time has been elapsed since the start.
        )r   r   r   r   r^   s    r2   time_elapsedzProgressBarCounter.time_elapsed  s?    
 >>!$$((*T__<<>>DOO33r4   c                    | j                   | j                  sy| j                  s| j                  rt	        j
                  d      S | j                  d| j                  z
  z  | j                  z  S )z7
        Timedelta representing the time left.
        Nr   r   )r   r   r   r   r   	timedeltar   r^   s    r2   	time_leftzProgressBarCounter.time_left  sX    
 ::T__YY$,,%%a(($$doo(=>PPr4   r   )rg   r)   r   zIterable[_CounterItem] | Noner   r   r   r   r   r   r:   r;   )r:   zIterator[_CounterItem]rI   r   )r   r   r:   r;   )r:   float)r:   zdatetime.timedelta)r:   zdatetime.timedelta | None)r   r   r   r   rZ   r   r   propertyr   setterr   r   r   r   r6   r4   r2   r   r   >  s     /3"$!& ! ,  	
   
4J ' 	 	 
[[4 4 * *" ^^" " C C 4 4 	Q 	Qr4   r   )r7   r   r:   r   )Gr   
__future__r   r   r   r   rC   rF   rU   ry   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%   rK   r'   r(   __all__r9   getattr	_SIGWINCHr>   r?   r)   r   r   r   r6   r4   r2   <module>r      s    #    	   
 
 
 3 > O O 
 ' 2 B  @ ; 4 + / </FJ-	, T]v vr$"y $"N ~6CQ. CQr4   