o
    ˷e5                     @   s  d 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	 ddl
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 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l0m1Z1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= zddl>Z>W n e?y   ddlm@Z@ e@Z>Y nw dgZAe(ZBeCeddZDde&fddZEedZFG dd dZGG dd de2ZHeddd ZIG d!d" d"eeI ZJdS )#z
Progress bar implementation on top of prompt_toolkit.

::

    with ProgressBar(...) as pb:
        for item in pb(data):
            ...
    N)new_event_loopset_event_loop)
GenericIterableIteratorListOptionalSequenceSizedTextIOTypeVarcast)Application)get_app_session)get_event_loop)	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   )	Formattercreate_default_formatters)dummy_contextvarsProgressBarSIGWINCHreturnc                  C   sB   t  } | ddtddfdd}| ddt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eventr-   Nc                 S   s   | j j  d S N)apprendererclearr.    r4   a/var/www/ideatree/venv/lib/python3.10/site-packages/prompt_toolkit/shortcuts/progress_bar/base.py_clearO   s   z#create_key_bindings.<locals>._clearzc-cc                 S   s   t t  tj d S r/   )oskillgetpidsignalSIGINTr3   r4   r4   r5   
_interruptS   s   z'create_key_bindings.<locals>._interrupt)r   addE)kbr6   r<   r4   r4   r5   create_key_bindingsH   s   r@   _Tc                   @   s   e Zd ZdZ									ddedeee  dedee dee	 dee
 d	ee d
ee dee ddfddZd ddZdeddfddZ				d!deee  dededee ddf
ddZd"ddZdS )#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 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title
formattersbottom_toolbarstylekey_bindingsfilecolor_depthoutputinputr-   c
           
      C   sv   || _ |pt | _|| _g | _|| _|| _|| _|pt j	| _	|	p$t j
| _
d | _t | _t | _d| _t | _d S )NF)rB   r)   rC   rD   countersrE   rF   rH   r   rI   rJ   _threadr   _loopr   	_app_loop_has_sigwinch	threadingEvent_app_started)
selfrB   rC   rD   rE   rF   rG   rH   rI   rJ   r4   r4   r5   __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tdtf 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 r/   rB   r4   rS   r4   r5   <lambda>       z'ProgressBar.__enter__.<locals>.<lambda>r'   zclass:progressbar,title)heightrE   c                      
    j d uS r/   rV   r4   rW   r4   r5   rX         
 )filterc                      rU   r/   rD   r4   rW   r4   r5   rX      rY   zclass:bottom-toolbar.text)rE   zclass:bottom-toolbar)rE   rZ   c                      r[   r/   r^   r4   rW   r4   r5   rX      r\   	formatterr-   c                    s   | j  dS )N)progress_bar)	get_width)r_   rW   r4   r5   width_for_formatter   s   z2ProgressBar.__enter__.<locals>.width_for_formatterc                    s&   g | ]}t t |t|d qS ))contentwidth)r   _ProgressControl	functoolspartial).0frS   rb   r4   r5   
<listcomp>   s    
z)ProgressBar.__enter__.<locals>.<listcomp>g?c                      s   t t jt jdS )N)	preferredmax)r#   lenrK   r4   rW   r4   r5   rX      s    )rZ   g333333?)min_redraw_intervallayoutrE   rF   refresh_intervalrH   rI   rJ   c               
      sZ   t  j 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)
r   rN   r0   runrR   setBaseException	traceback	print_excprint)erW   r4   r5   rs      s   
z"ProgressBar.__enter__.<locals>.run)targetargsr-   N)r   r   r   r   r   r   r(   r"   rC   r   r   r   r   rE   rF   rH   rI   rJ   r0   contextvarscopy_contextrP   Threadrs   rL   start)rS   title_toolbarrD   progress_controlsrs   ctxr4   rj   r5   	__enter__   sh   	

zProgressBar.__enter__ac                 G   sD   | j   | jjr| j| jj | jd ur| j  | j	  d S r/   )
rR   waitr0   
is_runningrN   call_soon_threadsafeexitrL   joinclose)rS   r   r4   r4   r5   __exit__   s   


zProgressBar.__exit__ Fdatalabelremove_when_donetotalz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   )ProgressBarCounterrK   append)rS   r   r   r   r   counterr4   r4   r5   __call__   s
   
zProgressBar.__call__c                 C   s   | j   d S r/   )r0   
invalidaterW   r4   r4   r5   r   	  s   zProgressBar.invalidate)	NNNNNNNNN)r-   r+   Nr   FNr|   )__name__
__module____qualname____doc__r   r   r	   r(   r&   r   r   r$   r%   r   rT   r   objectr   r   rA   boolintr   r   r4   r4   r4   r5   r+   ^   sd    
	


!N

c                   @   sX   e Zd ZdZdededdfddZded	edefd
dZ	de
fddZdefddZdS )re   z,
    User control for the progress bar.
    r`   r_   r-   Nc                 C   s   || _ || _t | _d S r/   )r`   r_   r@   _key_bindings)rS   r`   r_   r4   r4   r5   rT     s   z_ProgressControl.__init__rd   rZ   c              	      s~   g  | j jD ]$}z| j| j ||}W n ty"   t  d}Y nw  t| qdt	dt
f fdd}t|t ddS )NERRORir-   c                    s    |  S r/   r4   )r   itemsr4   r5   get_line#  s   z1_ProgressControl.create_content.<locals>.get_lineF)r   
line_countshow_cursor)r`   rK   r_   formatru   rv   rw   r   r   r   r   r    rn   )rS   rd   rZ   prtextr   r4   r   r5   create_content  s   z_ProgressControl.create_contentc                 C   s   dS )NTr4   rW   r4   r4   r5   is_focusable(  s   z_ProgressControl.is_focusablec                 C      | j S r/   )r   rW   r4   r4   r5   get_key_bindings+  s   z!_ProgressControl.get_key_bindings)r   r   r   r   r+   r(   rT   r   r    r   r   r   r   r   r4   r4   r4   r5   re     s    re   _CounterItemT)	covariantc                   @   s   e Zd ZdZ				ddedeee  dede	d	ee
 d
dfddZd
ee fddZdddZed
e	fddZejde	d
dfddZed
e	fddZejde	d
dfddZed
efddZed
ejfddZed
eej fddZdS ) r   zL
    An individual counter (A progress bar can have multiple counters).
    Nr   Fr`   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_timer`   r   items_completedr   r   _donern   r   r
   r   	TypeError)rS   r`   r   r   r   r   r4   r4   r5   rT   7  s    	
zProgressBarCounter.__init__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)rS   itemr4   r4   r5   __iter__R  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   r`   r   rW   r4   r4   r5   r   b  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   rW   r4   r4   r5   r   k  s   
zProgressBarCounter.donevaluec                 C   s0   || _ || _|r| jr| jj|  d S d S d S r/   )r   r   r   r`   rK   removerS   r   r4   r4   r5   r   w  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   rW   r4   r4   r5   r     s   
zProgressBarCounter.stoppedc                 C   s(   |r| j stj | _ d S d S d | _ d S r/   )r   r   r   r   r4   r4   r5   r     s
   
c                 C   s$   | j d u rdS | jd t| j d S )Nr   d   r'   )r   r   rm   rW   r4   r4   r5   
percentage  s   
zProgressBarCounter.percentagec                 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   rW   r4   r4   r5   time_elapsed  s   
zProgressBarCounter.time_elapsedc                 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   rW   r4   r4   r5   	time_left  s
   
zProgressBarCounter.time_leftr   r|   )r   r   r   r   r+   r   r   r   r   r   r   rT   r   r   r   propertyr   setterr   floatr   r   r   r   r   r4   r4   r4   r5   r   2  sF    


			r   )Kr   r   rf   r7   r:   rP   rv   asyncior   r   typingr   r   r   r   r   r	   r
   r   r   r   prompt_toolkit.applicationr   "prompt_toolkit.application.currentr   prompt_toolkit.eventloopr   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&   rC   r(   r)   r}   ImportErrorr*   __all__r>   getattr	_SIGWINCHr@   rA   r+   re   r   r   r4   r4   r4   r5   <module>   sN    	0  0"