
    ⨽g3                         d Z ddlmZmZ ddlmZ ddlZddiZddZddZ G d	 d
      Z	 G d de	      Z
 G d de
      Z G d d      Z G d dee
      Z G d de
      Z	 	 ddZy)zIImplements the generic progress logger class, and the ProgressBar class.
    )tqdmtqdm_notebook)OrderedDictNnotebookFc                 4    | dk(  r
dt         d<   y dt         d<   y )NonTFr   )SETTINGS)turns    X/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/proglog/proglog.pyr   r      s    $(DL4HZuHZ    c                 2    t        |       |k  r| S | d | dz   S )Nz...)len)s
max_lengths     r   troncate_stringr      s#    Q*$1CAkzNU,BCr   c                   D    e Zd ZdZddZd ZddZd Zd Zd Z	d	 Z
d
 Zy)ProgressLoggerzGeneric class for progress loggers.

    A progress logger contains a "state" dictionnary.

    Parameters
    ----------

    init_state
      Dictionnary representing the initial state.
    Nc                 x    i | _         i | _        g | _        d| _        || j                   j	                  |       y y )Nr   )statestoredlogs
log_indentupdate)self
init_states     r   __init__zProgressLogger.__init__   s;    
	!JJj) "r   c                 Z    | j                   j                  d| j                  z  |z          y )N )r   appendr   )r   messages     r   logzProgressLogger.log'   s"    		#/7:;r   c                     |@t        |d      5 }|j                  dj                  | j                               d d d        y dj                  | j                        S # 1 sw Y   y xY w)Na
)openwritejoinr   )r   filepathfs      r   	dump_logszProgressLogger.dump_logs*   s\    h$ .		$)),-. . 99TYY''. .s   +AA'c                      y)zExecute something after the state has been updated by the given
        state elements.

        This default callback does nothing, overwrite it by subclassing
        N r   kws     r   callbackzProgressLogger.callback1        	r   c                 ^    | j                   j                  |        | j                  di | y)a  Store objects in the logger and trigger ``self.store_callback``.

        This works exactly like ``logger()``, but the later is meant for simple
        data objects (text, numbers) that will be sent over the network or
        written to a file. The ``store`` method expects rather large objects
        which are not necessarily serializable, and will be used eg to draw
        plots on the fly.
        Nr,   )r   r   store_callbackr-   s     r   storezProgressLogger.store9   s)     	2!b!r   c                      y)zExecute something after the store has been updated by the given
        state elements.

        This default callback does nothing, overwrite it by subclassing
        Nr,   r-   s     r   r2   zProgressLogger.store_callbackE   r0   r   c              +   f   K   |j                         D ]  \  }}|D ]  } | di ||i |   yw)a  Iterate through a list while updating the state.

        Examples
        --------

        >>> for username in logger.iter(user=['tom', 'tim', 'lea']:
        >>>     # At every loop, logger.state['user'] is updated
        >>>     print (username)

        Nr,   )items)r   r.   fielditerableits        r   iterzProgressLogger.iterM   sD       "xxz 	OE8 #r{#	s   /1c                 ^    | j                   j                  |        | j                  di | y )Nr,   )r   r   r/   r-   s     r   __call__zProgressLogger.__call__`   s%    

"r   N)__name__
__module____qualname____doc__r   r!   r*   r/   r3   r2   r:   r<   r,   r   r   r   r      s/    	*<(
"&r   r   c                   X    e Zd ZdZdZ	 	 ddZed        Zd Zd Z	d Z
dd	Zdd
Zd Zy)ProgressBarLoggera  Generic class for progress loggers.

    A progress logger contains a "state" dictionnary

    Parameters
    ----------

    init_state
      Initial state of the logger

    bars
      Either None (will be initialized with no bar) or a list/tuple of bar
      names (``['main', 'sub']``) which will be initialized with index -1 and
      no total, or a dictionary (possibly ordered) of bars, of the form
      ``{bar_1: {title: 'bar1', index: 2, total:23}, bar_2: {...}}``

    ignored_bars
      Either None (newly met bars will be added) or a list of blacklisted bar
      names, or ``'all_others'`` to signify that all bar names not already in
      ``self.bars`` will be ignored.
       Nc                 j   t         j                  | |       |t               }n?t        |t        t
        f      r)t        |D cg c]  }|t        |dd d d      f c}      }t        |t        t
        f      rt        |      }|| _        || _	        || j                  d<   || _        || _        y c c}w )Nr   )titleindextotalr    indentbars)r   r   r   
isinstancelisttupledictsetignored_barslogged_barsr   min_time_intervalignore_bars_under)r   r   rK   rQ   rR   rS   rT   bs           r   r   zProgressBarLogger.__init__}   s    j1<=DtUm,    Dq$ !# $  D
 lT5M2|,L(&!

6!2!2 s   B0c                      | j                   d   S )zReturn ``self.state['bars'].``rK   )r   )r   s    r   rK   zProgressBarLogger.bars   s     zz&!!r   c                 r    | j                   y| j                   dk(  r|| j                  vS || j                   v S )NF
all_others)rQ   rK   r   bars     r   bar_is_ignoredz ProgressBarLogger.bar_is_ignored   s?    $,.tyy()$++++r   c                 X    | j                   sy| j                   dk(  ry|| j                   v S )NFallT)rR   rY   s     r   bar_is_loggedzProgressBarLogger.bar_is_logged   s0      &$****r   c                 ^    t        |d      rt        |      nd }|d uxr || j                  k  S )N__len__)hasattrr   rT   )r   r8   lengths      r   iterable_is_too_shortz'ProgressBarLogger.iterable_is_too_short   s1    ")(I">XDd"I$2H2H)HIr   c                     d|v r|j                  d      nd|j                         \   j                        s j                        rS |z   t	        d      r  di dz   t              i  fd} |       S )aJ  Iterate through a list while updating a state bar.

        Examples
        --------
        >>> for username in logger.iter_bar(user=['tom', 'tim', 'lea']):
        >>>     # At every loop, logger.state['bars']['user'] is updated
        >>>     # to {index: i, total: 3, title:'user'}
        >>>     print (username)

        bar_messageNr`   __totalc            	   3   j  K   t        j                          } d}t              D ]X  \  }}t        j                          }|dk(  s|| z
  j                  kD  r$ di dz    |      i  di dz   |i |} | Z j                     d   |k7  r di dz   |i  di dz   |dz   i y w)Nr   	__message__indexrH      r,   )time	enumeraterS   rK   )	last_timeir9   now_timerZ   re   r8   r   s       r   new_iterablez0ProgressBarLogger.iter_bar.<locals>.new_iterable   s     		IA"8, 299;F9 4t7M7M M".Dk 1;r?CD0C)OQ/0 (I yy~g&!+,i+,,C)OQU+,s   B0B3r,   )poppopitemr[   rc   ra   r   )r   
bar_prefixr.   rp   rZ   re   r8   s   `   @@@r   iter_barzProgressBarLogger.iter_bar   s     B&&/KK

Xs#t'A'A('KO38Y'4C)OS]34	-  ~r   c                      y)a  Execute a custom action after the progress bars are updated.

        Parameters
        ----------
        bar
          Name/ID of the bar to be modified.

        attr
          Attribute of the bar attribute to be modified

        value
          New value of the attribute

        old_value
          Previous value of this bar's attribute.

        This default callback does nothing, overwrite it by subclassing.
        Nr,   )r   rZ   attrvalue	old_values        r   bars_callbackzProgressBarLogger.bars_callback   s    & 	r   c           	         t        |j                         d       }|D ]5  \  }}d|v s|j                  d      \  }}| j                  |      r2|j	                  |       || j
                  vrt        |dd d       | j
                  |<   | j
                  |   |   }| j                  |      r|dk(  xr ||k  }|dk(  s|r| j                  | j
                  |   d<   n| j
                  |   d   | _        | j                  d	|d
|d|       | xj                  | j                  z  c_        || j
                  |   |<   | j                  ||||       8 | j                  j                  |        | j                  di | y )Nc                 ,    | d   j                  d       S )Nr   rI   )endswith)kvs    r   <lambda>z,ProgressBarLogger.__call__.<locals>.<lambda>   s    bennW6M2M r   )key__rF   )rG   rH   rI   r    rH   rI   rJ   [z] z: r,   )sortedr6   splitr[   rq   rK   rO   r^   r   r!   
bar_indentry   r   r   r/   )	r   r.   r6   r   rw   rZ   rv   rx   new_bars	            r   r<   zProgressBarLogger.__call__   s\   rxxz'MN 	@JCs{IIdO	T&&s+sdii'%)204d&DDIIcN IIcN40	%%c*#wGUY5FGW37??		#x0*.))C.*BHHc4?@OOt6O',		#t$""3eY?)	@* 	

"r   )NNNr]   r   r   ) r=   )r>   r?   r@   rA   r   r   propertyrK   r[   r^   rc   rt   ry   r<   r,   r   r   rC   rC   d   sM    , J@DKL3& " ",+J'R*r   rC   c                   8    e Zd ZdZ	 	 	 	 ddZd Zd Zd Zd Zy)	TqdmProgressBarLoggera  Tqdm-powered progress bar for console or Notebooks.

    Parameters
    ----------
    init_state
      Initial state of the logger

    bars
      Either None (will be initialized with no bar) or a list/tuple of bar
      names (``['main', 'sub']``) which will be initialized with index -1 and
      no total, or a dictionary (possibly ordered) of bars, of the form
      ``{bar_1: {title: 'bar1', index: 2, total:23}, bar_2: {...}}``

    ignored_bars
      Either None (newly met bars will be added) or a list of blacklisted bar
      names, or ``'all_others'`` to signify that all bar names not already in
      ``self.bars`` will be ignored.


    leave_bars

    notebook
      True will make the bars look nice (HTML) in the jupyter notebook. It is
      advised to leave to 'default' as the default can be globally set from
      inside a notebook with ``import proglog; proglog.notebook_mode()``.

    print_messages
      If True, every ``logger(message='something')`` will print a message in
      the console / notebook
    Nc
           	      &   t         j                  | |||||	|       || _        t        | j                  D 
cg c]  }
|
d f c}
      | _        |dk(  r	t        d   }|| _        || _        | j                  rt        | _
        y t        | _
        y c c}
w )N)r   rK   rQ   rR   rT   rS   defaultr   )rC   r   
leave_barsr   rK   	tqdm_barsr	   r   print_messagesr   r   )r   r   rK   r   rQ   rR   r   r   rS   rT   rZ   s              r   r   zTqdmProgressBarLogger.__init__#  s     	""4JT0</:5F5F	 	# 	H
 %$yy&
 $K&
  y 
+H ,&*mm]		&
s   Bc                     || j                   v r | j                   |   | j                  |       | j                  |   }| j                  |d   |d   t	        t        t        |d                     | j                        | j                   |<   y)z:Create a new tqdm bar, possibly replacing an existing one.NrI   rG   r    now)rI   descpostfixleave)r   close_tqdm_barrK   r   rO   r   strr   )r   rZ   infoss      r   new_tqdm_barz"TqdmProgressBarLogger.new_tqdm_bar7  s    4>>!s(;(G$		#"iiwgOCi0@,ABC	 ( 
sr   c                 v    | j                   |   j                          | j                  sd| j                   |<   yy)zClose and erase the tqdm barN)r   closer   rY   s     r   r   z$TqdmProgressBarLogger.close_tqdm_barB  s1    s!!#}}"&DNN3 r   c                    || j                   vs| j                   |   | j                  |       |dk(  r||k\  rM| j                  |   d   }|r||k\  r| j                  |       y | j                   |   j	                  ||z
         y | j                  |       | j                   |   j	                  |dz          y |dk(  rP| j                   |   j                  t        t        |                   | j                   |   j	                  d       y y )NrH   rI   rj   r    r   r   )r   r   rK   r   r   set_postfixr   r   )r   rZ   rv   rw   rx   rI   s         r   ry   z#TqdmProgressBarLogger.bars_callbackH  s    t~~%4>>#+>+Fc"7?	!		#w/eun'',NN3'..uy/@A!!#&s#**5195YNN3++E
0K+LNN3&&q) r   c                     | j                   rEd|v r@|d   r:| j                  rt        |d          y | j                  j	                  |d          y y y y )Nr    )r   r   printr   r&   r-   s     r   r/   zTqdmProgressBarLogger.callbackY  sI    IOI}}bm$		9.	 :GOr   )	NNFNr]   r   Tr   r   )	r>   r?   r@   rA   r   r   r   ry   r/   r,   r   r   r   r     s/    > ?D@I89#$?(

'*"/r   r   c                       e Zd Zd Zd Zy)RqWorkerProgressLoggerc                     || _         d| j                   j                  vr4i | j                   j                  d<   | j                   j                          y y Nprogress_data)jobmetasave)r   r   s     r   r   zRqWorkerProgressLogger.__init__a  s=    $((--/-/DHHMM/*HHMMO 0r   c                 ~    | j                   | j                  j                  d<   | j                  j                          y r   )r   r   r   r   r-   s     r   r/   zRqWorkerProgressLogger.callbackg  s%    )-o&r   N)r>   r?   r@   r   r/   r,   r   r   r   r   `  s    r   r   c                       e Zd Z	 	 ddZy)RqWorkerBarLoggerNc                 f    t         j                  | |       t        j                  | |||||       y )N)r   rK   rQ   rR   rS   )r   r   rC   )r   r   r   rK   rQ   rR   rS   s          r   r   zRqWorkerBarLogger.__init__m  s5    ''c2""4JT0</:5F 	# 	Hr   )NNr,   r]   r   )r>   r?   r@   r   r,   r   r   r   r   k  s    EG78Hr   r   c                       e Zd Zd Zy)MuteProgressBarLoggerc                      y)NTr,   rY   s     r   r[   z$MuteProgressBarLogger.bar_is_ignoredw  s    r   N)r>   r?   r@   r[   r,   r   r   r   r   u  s    r   r   c                 H    | dk(  rt        |||||      S | 
t               S | S )NrZ   )rK   rQ   rR   rS   rT   )r   r   )loggerrK   rQ   rR   rS   rT   s         r   default_bar_loggerr   z  s:    $%#//
 	
 
$&&r   )r   )   )NNr]   r   r   )rA   r   r   collectionsr   rk   r	   r   r   r   rC   r   r   r   r   r   r,   r   r   <module>r      s    % #  =DP Pd] ]~[/- [/z	 	H.0A H- 
 JO>?r   