
    wi,                        d dl Z d dlZd dl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 d dlmZ 	 d dlmZ dZn# e$ r dZd	ZY nw xY wd d
lmZ d dlmZ d dlmZ dZeedf         Z  e j!        e"          Z# G d d          Z$ G d de$e          Z%er G d de$e          Z&n G d de$          Z& G d de$e          Z' G d de$e          Z(dee)         dee$         fdZ* G d d          Z+ G d d          Z,dS )     N)redirect_stderrredirect_stdout)StringIO)Lock)OptionalTupleType)TqdmExperimentalWarningtqdm)r   TF)mp_dill)DisableKeyboardInterruptSignal)create_sync_managerstdTqdmPositionRegisterc                        e Zd ZdZdZededdfd            Zddeddf fd	Z	d
eddfdZ
ddee         ddfdZededdfd            Z xZS )	TqdmMpirez7 Abstract class for tqdm classes that are used in mpireFmainreturnNc                     || _         dS )z
        Marks this progress bar as the main progress bar

        :param main: Whether this progress bar is the main progress bar
        N)main_progress_bar)clsr   s     h/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/mpire/tqdm_utils.pyset_main_progress_barzTqdmMpire.set_main_progress_bar!   s     !%       nc                     t                                          |           | j        | j        k    r|                                  dS dS )z
        Update the progress bar. Forces a final refresh when the progress bar is finished.

        :param n: Number of steps to update the progress bar with
        N)superupdater   totalfinal_refresh)selfr   	__class__s     r   r   zTqdmMpire.update*   sJ     	q6TZ       r   r    c                 <    || _         |                                  dS )z
        Update the total number of steps of the progress bar. Forces a refresh to show the new total.

        :param total: Total number of steps
        N)r    refresh)r"   r    s     r   update_totalzTqdmMpire.update_total4   s     
r   highest_progress_bar_positionc                 V    |                                   |                                  dS )a*  
        Final refresh of the progress bar. This function is called when the progress bar is finished. It should
        perform a final refresh of the progress bar and close it.

        :param highest_progress_bar_position: Highest progress bar position in case of multiple progress bars
        N)r%   closer"   r'   s     r   r!   zTqdmMpire.final_refresh=   s"     	

r   optionsc                     t          t                                5  t          t                                5   | di | ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z
        Check whether the options passed to the tqdm class are valid. This function should raise an exception when the
        options are invalid.

        :param options: Options passed to the tqdm class
        N )r   r   r   r   r+   s     r   check_optionszTqdmMpire.check_optionsG   s     XZZ(( 	 	/(***E*E 	 	CNN'NNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s4   A%	AA%A	A%A	A%%A),A))r   N)__name__
__module____qualname____doc__r   classmethodboolr   intr   r&   r   r!   dictr/   __classcell__r#   s   @r   r   r      s       AA% %$ % % % [%! ! !D ! ! ! ! ! !# $     8C= TX     D T    [    r   r   c                   0    e Zd ZdZddee         ddfdZdS )TqdmMpireStdz2 A tqdm class that shows a standard progress bar. Nr'   r   c                     |                                   d| _        | j        r$|$| j                            d|dz   z             dS dS dS )a  
        Final refresh of the progress bar. This function is called when the progress bar is finished. It should
        perform a final refresh.

        When we're using a standard progress bar and this is the main progress bar, we add as many newlines as the
        highest progress bar position, such that new output is added after the progress bars.

        :param highest_progress_bar_position: Highest progress bar position in case of multiple progress bars
        TN
r   )r%   disabler   fpwriter*   s     r   r!   zTqdmMpireStd.final_refreshV   sa     	! 	F&C&OGMM$"?!"CDEEEEE	F 	F&O&Or   r0   )r1   r2   r3   r4   r   r7   r!   r-   r   r   r<   r<   S   sO        <<F F8C= FTX F F F F F Fr   r<   c                   D     e Zd ZdZededdfd            Zd fdZ xZS )TqdmMpireRichz. A tqdm class that shows a rich progress bar. r+   r   Nc                     |                                 }d|vrddi|d<   nd|d         d<   t          j                    5  t          j        dt                      | di | ddd           dS # 1 swxY w Y   dS )a  
            Check whether the options passed to the tqdm class are valid. This function should raise an exception when the
            options are invalid.

            For rich progress bars we disable the progress bar, because we don't want to show the progress bar in the
            terminal. For some reason, redirecting stdout/stderr makes the rich progress bar not work properly afterwards.

            :param options: Options passed to the tqdm class
            r+   r?   TignoreNr-   )copywarningscatch_warningssimplefilterr
   r.   s     r   r/   zTqdmMpireRich.check_optionsj   s     llnnG''&/%6	""04	"9-(**  %h0GHHHg                 s   #A//A36A3c                 l     t                      j        |i | | j                                         dS )z
            Display the progress bar and force a refresh of the widget. The refresh is needed to show the final update.
            N)r   display_progr%   r"   argskwargsr#   s      r   rK   zTqdmMpireRich.display~   s:     EGGOT,V,,,J     r   r   N)	r1   r2   r3   r4   r5   r8   r/   rK   r9   r:   s   @r   rC   rC   g   so        <<		 	 	 	 	 
	&	! 	! 	! 	! 	! 	! 	! 	! 	! 	!r   rC   c                       e Zd ZddZdS )rC   r   Nc                      t          d          )NzErich is not installed. Please install rich to use rich progress bars.)ImportErrorr"   rN   rO   s      r   __init__zTqdmMpireRich.__init__   s    efffr   rP   )r1   r2   r3   rU   r-   r   r   rC   rC      s.        	g 	g 	g 	g 	g 	gr   c                   X     e Zd ZdZd	 fdZdeddf fdZededdfd            Z	 xZ
S )
TqdmMpireNotebookz4 A tqdm class that shows a GUI widget in notebooks. r   Nc                 l    | j         st          ddd            t                      j        |i | dS )z
        In case we're running tqdm in a notebook we need to apply a dirty hack to get progress bars working.
        Solution adapted from https://github.com/tqdm/tqdm/issues/485#issuecomment-473338308
          T)endflushN)r   printr   rU   rM   s      r   rU   zTqdmMpireNotebook.__init__   sG    
 % 	+#2T****$)&)))))r   r    c                 t    || j         j        d         _        t                                          |          S )z
        Update the total number of steps of the progress bar. Forces a refresh to show the new total.

        In a notebook we also need to update the max value of the progress bar widget.

        :param total: Total number of steps
        r   )	containerchildrenmaxr   r&   )r"   r    r#   s     r   r&   zTqdmMpireNotebook.update_total   s/     */"&ww##E***r   r+   c                 H    |                                 }d|d<    | di | dS )aa  
        Check whether the options passed to the tqdm class are valid. This function should raise an exception when the
        options are invalid.

        For notebook progress bars we set display to false, because redirecting stdout/stderr doesn't work for notebook
        widgets.

        :param options: Options passed to the tqdm class
        FrK   Nr-   )rF   r.   s     r   r/   zTqdmMpireNotebook.check_options   s0     ,,.."	gr   rP   )r1   r2   r3   r4   rU   r7   r&   r5   r8   r/   r9   r:   s   @r   rW   rW      s        >>* * * * * *	+# 	+$ 	+ 	+ 	+ 	+ 	+ 	+ D T    [    r   rW   c                   ,     e Zd ZdZd fdZddZ xZS )TqdmMpireDashboardOnlyz
    A tqdm class that gives no output, but will still update the internal progress-bar attributes that the
    dashboard relies on.
    r   Nc                 \    t                      |d<    t                      j        |i | dS )z> Set the file to a StringIO object so that no output is given fileN)r   r   rU   rM   s      r   rU   zTqdmMpireDashboardOnly.__init__   s2    !v$)&)))))r   c                     dS )z Don't display anything Nr-   rT   s      r   rK   zTqdmMpireDashboardOnly.display   s    r   rP   )r1   r2   r3   r4   rU   rK   r9   r:   s   @r   rd   rd      s[         
* * * * * *
       r   rd   progress_bar_styler   c                     | t           } | dk    rt          S | dk    rt          S | dk    rt          S | dk    rt          S t          d|  d          )a8  
    Get the tqdm class to use based on the progress bar style

    :param progress_bar_style: The progress bar style to use. Can be one of ``None``, ``std``, or ``notebook``
    :return: A tuple containing the tqdm class to use and a boolean indicating whether the progress bar is a notebook
        widget
    Nr   richnotebook	dashboardzInvalid progress bar style: z2. Use either None (=default), "std", or "notebook")PROGRESS_BAR_DEFAULT_STYLEr<   rC   rW   rd   
ValueError)rh   s    r   get_tqdmro      s     !7U""	v	%	%	z	)	)  	{	*	*%% M8J M M M N N 	Nr   c                   R    e Zd ZdZdeddfdZdedefdZdee         fdZ	d
d	Z
dS )r   z
    Class that keeps track of all the registered progress bar positions. Needed to properly display multiple tqdm
    progress bars
    use_dillr   Nc                 d    |rt          j                    nt                      | _        d| _        dS )zP
        :param use_dill: Whether dill is used as serialization library
        N)r   r   mp_Locklockhighest_position)r"   rq   s     r   rU   zTqdmPositionRegister.__init__   s,     '/=GLNNNGII	 $r   positionc                     | j         5  | j        du }| j        || j        k    r|| _        ddd           n# 1 swxY w Y   |S )z
        Register new progress bar position. Returns True when it's the first one to register

        :param position: Progress bar position
        :return: Whether this progress bar is the first one to register
        Nrt   ru   )r"   rv   	first_ones      r   register_progress_bar_positionz3TqdmPositionRegister.register_progress_bar_position   s     Y 	1 	1-5I$,4;P0P0P(0%	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1
 s   #7;;c                 R    | j         5  | j        cddd           S # 1 swxY w Y   dS )zu
        Obtain the highest registered progress bar position

        :return: Highest progress bar position
        Nrx   r"   s    r   !get_highest_progress_bar_positionz6TqdmPositionRegister.get_highest_progress_bar_position   sv     Y 	) 	)(	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s     c                 T    | j         5  d| _        ddd           dS # 1 swxY w Y   dS )z=
        Reset the registered progress bar positions
        Nrx   r|   s    r   reset_progress_bar_positionsz1TqdmPositionRegister.reset_progress_bar_positions   sx     Y 	) 	)$(D!	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   !!rP   )r1   r2   r3   r4   r6   rU   r7   rz   r   r}   r   r-   r   r   r   r      s         
% %$ % % % %s t    )8C= ) ) ) )) ) ) ) ) )r   c                       e Zd ZdZdZdZdZededefd            Z	ed
d            Z
edefd            Zededdfd	            ZdS )TqdmManagerzbTqdm manager wrapper for syncing multiple progress bars, independent of process start method used.Nrq   r   c                    | j         dS t                              d           t                      5  t	          |          | _        | j                            dt                     | j                                         | j        	                                | _         | j                            |          | _
        ddd           n# 1 swxY w Y   dS )z
        Sets up and starts the tqdm manager

        :param use_dill: Whether dill is used as serialization library
        :return: Whether the manager was started
        NFzStarting TQDM managerr   T)LOCKloggerdebugr   r   MANAGERregisterr   startr   POSITION_REGISTER)r   rq   s     r   start_managerzTqdmManager.start_manager  s	    85,--- ,-- 	O 	O-h77CKK  !79MNNNK{''))CH$'K$D$DX$N$NC!	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O ts   BC		CCc                 b    | j                                          d| _         d| _        d| _        dS )z(
        Stops the tqdm manager
        N)r   shutdownr   r   r   s    r   stop_managerzTqdmManager.stop_manager'  s4    
 	 $r   c                     | j         | j        fS )z
        Obtains the connection details of the tqdm manager. These details are needed to be passed on to child process
        when the start method is either forkserver or spawn.

        :return: TQDM lock and position register
        r   r   r   s    r   get_connection_detailsz"TqdmManager.get_connection_details1  s     x...r   tqdm_connection_detailsc                 $    |\  | _         | _        dS )z|
        Sets the tqdm connection details.

        :param tqdm_connection_details: TQDM lock and position register
        Nr   )r   r   s     r   set_connection_detailsz"TqdmManager.set_connection_details;  s     +B'#'''r   rP   )r1   r2   r3   r4   r   r   r   r5   r6   r   r   TqdmConnectionDetailsr   r   r-   r   r   r   r     s        llGDT d    [. % % % [% /'< / / / [/ B=R BW[ B B B [B B Br   r   )-loggingrG   
contextlibr   r   ior   multiprocessingr   rs   multiprocessing.synchronizeLockTypetypingr   r   r	   r   r
   tqdm_stdtqdm.notebooktqdm_notebook	tqdm.rich	tqdm_richRICH_AVAILABLErS   mpire.contextr   mpire.signalr   mpire.utilsr   rm   r   	getLoggerr1   r   r   r<   rC   rW   rd   strro   r   r   r-   r   r   <module>r      s     7 7 7 7 7 7 7 7       + + + + + + 8 8 8 8 8 8 ( ( ( ( ( ( ( ( ( ( : : : : : : : : / / / / / /++++++NN   INNN " ! ! ! ! ! 7 7 7 7 7 7 + + + + + +" h(>>? 		8	$	$4 4 4 4 4 4 4 4nF F F F F9h F F F&  #g! ! ! ! !	9 ! ! ! !>g g g g g	 g g g$ $ $ $ $	= $ $ $N    Y    N# N4	? N N N N.*) *) *) *) *) *) *) *)Z;B ;B ;B ;B ;B ;B ;B ;B ;B ;Bs   A 	AA