o
    3g                      @  s   d Z ddlm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
 G dd deZG dd	 d	eZG d
d deZG dd deZee ee dd ZedkrYe  dS dS )z
Progress bar
------------
    )annotationsN)ABCabstractmethod)get_terminal_sizec                   @  s   e Zd ZdZ						d"ddZdd Zd	d
 Zedd Zdd Z	dd Z
edd Zejdd Zedd Zdd Zdd Zdd Zedd Zedd Zed#d d!ZdS )$ProgressBasea   Base class for progress bars. Customize for types of progress bars.

    :param iterator: The iterator to wrap with a progress bar
    :param length: The length of the iterator (will use ``__len__`` if None)
    :param timer: Try to time the completion status of the iterator
    :param body: True if the slow portion occurs outside the iterator (in a loop, for example)
    :param has_output: True if the iteration body produces output to the screen (forces rewrite off)
    :param clear: Clear the progress bar afterwards, if applicable.
    NTFc                 C  sd   |d u r	t |}n|d u rt|}n|d u r|d u rtd|| _|| _|| _|| _|| _|| _d S )Nz'Expected either an iterator or a length)	lenrange	TypeErrorlengthiteratortimerbody
has_outputclear)selfr   r
   r   r   r   r    r   L/home/garg/my-data/venv/lib/python3.10/site-packages/plumbum/cli/progress.py__init__   s   	


zProgressBase.__init__c                 C     | j S N)r
   r   r   r   r   __len__2   s   zProgressBase.__len__c                 C  s   |    | S r   )startr   r   r   r   __iter__5   s   zProgressBase.__iter__c                 C  s,   t | j| _ | jrdnd| _tj | _dS )z8This should initialize the progress bar and the iteratorr   N)iterr   r   valuedatetimenow_start_timer   r   r   r   r   9   s   zProgressBase.startc                 C  s2   zt | j}|   W |S  ty   |    w r   )nextr   	incrementStopIterationdone)r   rvalr   r   r   __next__@   s   

zProgressBase.__next__c                 C  s   t | S r   )r    r   r   r   r   r    I   s   zProgressBase.nextc                 C  r   )zHThis is the current value, as a property so setting it can be customized_valuer   r   r   r   r   L      zProgressBase.valuec                 C  s
   || _ d S r   r&   r   valr   r   r   r   Q   s   
c                 C     dS )z!Called to update the progress barNr   r   r   r   r   displayU       zProgressBase.displayc                 C  s   |  j d7  _ |   dS )z$Sets next value and displays the bar   N)r   r,   r   r   r   r   r!   Y   s   zProgressBase.incrementc                 C  sh   | j dk rdS tj | j }|jd d d |j |jd  | j  }|| j| j   }|td|dfS )z4Get the time remaining for the progress bar, guessesr.   )NN   <   g    .Ar   )	r   r   r   r   dayssecondsmicrosecondsr
   	timedelta)r   elapsed_time	time_eachtime_remainingr   r   r   r7   ^   s   
zProgressBase.time_remainingc                 C  sP   | j dk rdS ttt|  \}}|dd }|dd }| d| dS )z*Returns a string version of time remainingr.   z$Starting...                         .r   z completed, z
 remaining)r   listmapstrr7   split)r   r5   r7   	completed	remainingr   r   r   str_time_remainingk   s   
zProgressBase.str_time_remainingc                 C  r+   )z$Is called when the iterator is done.Nr   r   r   r   r   r#   u   r-   zProgressBase.donec                 O  s   | t | fddi|S )zMFast shortcut to create a range based progress bar, assumes work done in bodyr   T)r   )clsr   kargsr   r   r   r   y   s   zProgressBase.rangec                 K  s   | ||fddi|S )zEShortcut to wrap an iterator that does not do all the work internallyr   Tr   )r@   r   r
   rA   r   r   r   wrap~   s   zProgressBase.wrap)NNTFFTr   )__name__
__module____qualname____doc__r   r   r   r   r   r%   r    propertyr   setterr,   r!   r7   r?   r#   classmethodr   rB   r   r   r   r   r      s<    

	





r   c                      s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	Progressc                   s   t    |   d S r   )superr   r,   r   	__class__r   r   r      s   
zProgress.startc                 C  sX   | j | _|   | jr| jstjdtt	| d  d  ntjd tj
  d S )N 
)r
   r   r,   r   r   sysstdoutwriter   r;   flushr   r   r   r   r#      s   "zProgress.donec                 C  s(  t ddd }| jdkrd| _dS t| jd| j }d| jr"|  n	| j d| j d }|t| dk s8| jrNd| _| jrH|d	d
|   S |d	dS |t| d d | _t	|| j }dd|  d| j|   d | }d|d	d}|d | jd d  | || jd t| d d   S )N)r   r   )defaultr   z0/0 completerO   z of z	 complete
   z.0%z complete:    r.   [*])
r   r
   widthmaxr   r   r?   r   r   int)r   r[   percentendingnstarspbarstr_percentr   r   r   __str__   s2   

"zProgress.__str__c                 C  sP   t | }| jdks| jrtj|d  ntjd tj| tj  d S )Nr   rP   rN   )r;   r[   r   rQ   rR   rS   rT   )r   disptxtr   r   r   r,      s   zProgress.display)rC   rD   rE   r   r#   rc   r,   __classcell__r   r   rL   r   rJ      s
    	 rJ   c                      sV   e Zd ZdZ fddZ fddZedd Zejdd Zd	d
 Z	dd Z
  ZS )ProgressIPyzi<div class="widget-hbox widget-progress"><div class="widget-label" style="display:block;">{0}</div></div>c              	     s   t  - t d zddlm}m}m} W n ty)   ddlm}m}m} Y nw W d    n1 s4w   Y  t	 j
|i | || jd| _| | _|| j| jf| _d S )Nignorer   )HTMLHBoxIntProgress)r\   )warningscatch_warningssimplefilter
ipywidgetsrh   ri   rj   ImportErrorIPython.html.widgetsrK   r   r
   prog_label_box)r   argsrA   rh   ri   rj   rL   r   r   r      s   

zProgressIPy.__init__c                   s$   ddl m} || j t   d S )Nr   )r,   )IPython.displayr,   rs   rK   r   )r   r,   rL   r   r   r      s   
zProgressIPy.startc                 C  r   )zGThis is the current value, -1 allowed (automatically fixed for display)r&   r   r   r   r   r      r(   zProgressIPy.valuec                 C  sV   || _ t|d| j_| j| j d| j_| jr'|dkr)| j| 	 | j
_d S d S d S )Nr   z.2%)r'   r\   rq   r   r
   descriptionr   HTMLBOXformatr?   rr   r)   r   r   r   r      s   c                 C  s   d S r   r   r   r   r   r   r,      s   zProgressIPy.displayc                 C  s   | j r
| j  d S d S r   )r   rs   closer   r   r   r   r#      s   zProgressIPy.done)rC   rD   rE   rw   r   r   rG   r   rH   r,   r#   re   r   r   rL   r   rf      s    

rf   c                   @  s   e Zd ZdZdd ZdS )ProgressAutoa<  Automatically selects the best progress bar (IPython HTML or text). Does not work with qtconsole
    (as that is correctly identified as identical to notebook, since the kernel is the same); it will still
    iterate, but no graphical indication will be displayed.

    :param iterator: The iterator to wrap with a progress bar
    :param length: The length of the iterator (will use ``__len__`` if None)
    :param timer: Try to time the completion status of the iterator
    :param body: True if the slow portion occurs outside the iterator (in a loop, for example)
    c              	   O  s   z/t  zddlm} W n ty   ddlm} Y nw z	t|i |W W S  |y/   t dw  ttfyA   t|i | Y S w )z_Uses the generator trick that if a cls instance is returned, the __init__ method is not called.r   )
TraitErrorN)__IPYTHON__	traitletsr{   ro   IPython.utils.traitletsrf   	NameErrorrJ   )r@   rt   rA   r{   r   r   r   __new__   s   zProgressAuto.__new__N)rC   rD   rE   rF   r   r   r   r   r   rz      s    
rz   c                  C  s*   dd l } td}|D ]}| d qd S )Nr      r.   )timerJ   r   sleep)r   tst_r   r   r   main  s
   
r   __main__)rF   
__future__r   r   rQ   rk   abcr   r   plumbum.cli.termsizer   r   rJ   rf   rz   registerr   rC   r   r   r   r   <module>   s"    t8,


