
    wi41                     2   d dl Z d dlZd dlmZmZ d dl mZmZ d dlmZmZm	Z	m
Z
 d dlZd dlmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZmZ d d
lm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% n# e&$ r dZ!d Z#d Z%Y nw xY wdZ' G d d          Z(dS )    N)datetime	timedelta)EventThread)AnyDictOptionalType)TqdmExperimentalWarningtqdm)WorkerCommsPOISON_PILL)remove_highlighting)WorkerInsights)WorkerMapParamsWorkerPoolParams)DisableKeyboardInterruptSignal)get_tqdmTqdmManager)format_seconds)DASHBOARD_STARTED_EVENT)get_function_details)get_manager_client_dictsc                     d S N )_s    j/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/mpire/progress_bar.pyr   r      s        c                      t           r   )NotImplementedErrorr   r   r   r   r      s    !!r   z%Y-%m-%d, %H:%M:%Sc                      e Zd Zdedededeeef         de	e         de
dedd	fd
ZddZdedd	fdZddZdedd	fdZ	 	 ddedede	e         dd	fdZ	 ddedede	e         deeef         fdZdedd	fdZdedd	fdZd	S )ProgressBarHandlerpool_params
map_paramsshow_progress_barprogress_bar_optionsprogress_bar_styleworker_commsworker_insightsreturnNc                    || _         || _        || _        || _        || _        |rCt
          <t          j                    r)t          |j                  | _	        |j
        | j	        d<   nd| _	        d| _        t                      | _        d| _        d| _        t                      | _        d| _        t%          j        t%          j                              | _        d| _        d| _        d| _        dS )a=  
        :param pool_params: WorkerPool parameters
        :param map_params: Map parameters
        :param show_progress_bar: When ``True`` will display a progress bar
        :param progress_bar_options: Dictionary containing keyword arguments to pass to the ``tqdm`` progress bar. See
         ``tqdm.tqdm()`` for details.
        :param progress_bar_style: The progress bar style to use
        :param worker_comms: Worker communication objects (queues, locks, events, ...)
        :param worker_insights: WorkerInsights object which stores the worker insights
        Nn_jobs)lock)r&   r'   r(   r)   r*   r   is_setr   funcfunction_detailsr-   threadr   thread_startedprogress_bar_idtotaltotal_updatedexception_traceback_str	threading	ConditionLock%exception_traceback_str_set_conditiondashboard_dictdashboard_details_dictstart_t)selfr$   r%   r&   r'   r(   r)   r*   s           r   __init__zProgressBarHandler.__init__%   s     "3$8!"4(. 	)!8!DI`IgIiIi!D$8$I$ID!.9.@D!(++$(D!#gg#
"WW'+$5>5HinN^N^5_5_5_2"&*#r   c                     | j         rrt                      5  t          | j                  | _        | j                                         | j                                         ddd           n# 1 swxY w Y   | S )z
        Enables the use of the ``with`` statement. Starts a new progress handler thread if a progress bar should be
        shown

        :return: self
        )targetN)r&   r   r   _progress_bar_handlerr2   startr3   wait)r?   s    r   	__enter__zProgressBarHandler.__enter__H   s     ! 	+ 011 + +$D,FGGG!!####((***+ + + + + + + + + + + + + + +
 s   AA//A36A3exc_typec                    | j         r| j                                        rj|| j                                         | j                                        s| j                                         | j                                         dS dS dS )zs
        Enables the use of the ``with`` statement. Terminates the progress handler thread if there is one
        N)r&   r2   is_aliver)   signal_kill_signal_receivedexception_thrownsignal_progress_bar_shutdownjoin)r?   rG   r   s      r   __exit__zProgressBarHandler.__exit__Y   s     ! 
	dk&:&:&<&< 
	 #!==??? $5577 A!>>@@@K
	 
	 
	 
	r   c                    t          | j                  }t                      }|                                \  }}|                    |           |                    |                    | j        d                              t          j	                    5  t          j
        dt                     d|_         |d
i | j        }ddd           n# 1 swxY w Y   t          j        |j                  | _        | j                                         |                     |           	 | j                                        }|t*          u r| j                                        s| j                                        r|                    d           | j                                        r]| j        5  | j        | j                                         ddd           n# 1 swxY w Y   |                     |d| j                   n1| j                                        r|                     |dd           |                    |                                           dS | j                                         rH|!                    | j"                   |                     |           | j        #                                 |d	k    r||j$        k    r|%                    ||j$        z
             |j$        |j"        k    rG| j        &                                 | j        '                                 |                     |           |j$        |j(        k    r|                     |           I)zj
        Keeps track of the progress made by the workers and updates the progress bar accordingly
        positionignoreFNTz$Exception occurred, terminating ... )failedtraceback_strzKill signal receivedr   r   ))r   r(   r   get_connection_detailsset_lockset_main_progress_barregister_progress_bar_positionr'   warningscatch_warningssimplefilterr   monitor_intervalr   fromtimestampr>   r3   set_register_progress_barr)    get_tasks_completed_progress_barr   rK   kill_signal_receivedset_descriptionr;   r7   rE   _send_dashboard_updatefinal_refresh!get_highest_progress_bar_positionr6   r/   update_totalr5   clearnupdatesignal_progress_bar_complete#wait_until_progress_bar_is_completelast_print_n)r?   r   tqdm_manager	tqdm_locktqdm_position_registerprogress_bartasks_completeds          r   rC   z(ProgressBarHandler._progress_bar_handleri   s   
 /00 #}},8,O,O,Q,Q)	)i   """AA$B[\fBghh	
 	
 	

 $&& 	= 	=!(,CDDD$)D!4<<$";<<L	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=  -l.BCC 	!!! 	##L111.	:"/PPRRO +-- $5577 
u4;L;a;a;c;c 
u 001WXXX(99;; u!G R R#;C $ J O O Q Q QR R R R R R R R R R R R R R R 33LBFB^ 4 ` ` ` `*??AA u33L]s3ttt **+A+c+c+e+efff !((** +))$*555++L999"((*** "",.'H'H ,. @AAA~!333!>>@@@!EEGGG++L999 ~!:::++L999].	:s$   /CCC2!GG#&G#ro   c                    | j         t          t          j                    rt                      \  | _        | _        }|                                 t          | j                                                  dz   | _         | j        	                    | j         | j
        fg           |                     |           |                                 dS dS dS dS )zv
        Register this progress bar to the dashboard

        :param progress_bar: tqdm progress bar instance
        N   )r4   r   r/   r   r<   r=   acquirelenkeysrh   r1   rb   release)r?   ro   dashboard_tqdm_locks      r   r^   z)ProgressBarHandler._register_progress_bar   s     ',C,OTkTrTtTt,OTlTnTnQD!<>Q  '')))#&t':'?'?'A'A#B#BQ#FD '..1EtG\0]/^___''555''))))) (',O,O,O,Or   FrR   rS   c                     | j         9| j                            | j         |                     |||          fg           dS dS )aT  
        Adds a progress bar update to the shared dict so the dashboard process can use it, only when a dashboard has
        started

        :param progress_bar: tqdm progress bar instance
        :param failed: Whether or not the operation failed or not
        :param traceback_str: Traceback string, if an exception was raised
        N)r4   r<   rh   _get_progress_bar_update_dict)r?   ro   rR   rS   s       r   rb   z)ProgressBarHandler._send_dashboard_update   sg     +&&)=)-)K)KLZ`bo)p)p)r (s t t t t t ,+r   c                    |j         }|d         }|d         }t          j                    }|d         r|d         n|d         r||d         z  nd}|r
|r||z
  |z  nd}	| j        | |||r||z  ndt	          || j        z
                                dd          d         t          |	d	          | j        | j                            t                    |	|t          |	
          z   nd|	+|t          |	
          z                       t                    nd||                                nd| j                                        dS )aS  
        Obtain update dictionary with all the information needed for displaying on the dashboard

        :param progress_bar: tqdm progress bar instance
        :param failed: Whether or not the operation failed or not
        :param traceback_str: Traceback string, if an exception was raised
        :return: Update dictionary
        rg   r5   rateelapsedN.rr   r   F)seconds )idsuccessrg   r5   
percentageduration	remainingstarted_rawstartedfinished_rawfinished	tracebackinsights)format_dictr   nowr4   strr>   rsplitr   strftimeDATETIME_FORMATr   stripr*   get_insights)
r?   ro   rR   rS   detailsrg   r5   r   r{   remaining_times
             r   ry   z0ProgressBarHandler._get_progress_bar_update_dict   sr    *CL lnn")&/mwvQXYbQc7mq79;M7M7Mim/4GG%!)t++4*%:+0:a%iiddl 233::3BB1E+NEBB#|<00AAKYKei&G&G&G G Gko . : "In$E$E$EEOOP_```@B6C6O]00222UY 0==??A A 	Ar   r5   c                 F    || _         | j                                         dS )zW
        Set a new total for the progress bar

        :param total: New total
        N)r5   r6   r]   )r?   r5   s     r   set_new_totalz ProgressBarHandler.set_new_total   s&     
     r   traceback_errc                 &   |j         -d                    t          j        |j                            }nt          |          }| j        5  t          |          | _        | j        	                                 ddd           dS # 1 swxY w Y   dS )z
        Set the exception traceback string and notify the progress bar handler that it's ready

        :param traceback_err: Traceback error
        Nr   )
	__cause__rM   r   	format_tb__traceback__r   r;   r   r7   notify)r?   r   rS   s      r   set_exceptionz ProgressBarHandler.set_exception   s     ".GGI$78S$T$TUUMM..M7 	@ 	@+>}+M+MD(6==???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   .BB
B
)r+   r#   )r+   N)FNr   )__name__
__module____qualname__r   r   boolr   r   r   r	   r   r   r@   rF   r
   rN   rC   	tqdm_typer^   rb   ry   intr   	Exceptionr   r   r   r   r#   r#   #   s       !$4 !/ !fj !'+CH~!KSTW=!hs!"0!59! ! ! !F   " d     K: K: K: K:Z*9 * * * * *" NS>Bt t9 td t.6smtGKt t t t FJA A) AT A5=c]ANRSVX[S[nA A A AB!3 !4 ! ! ! !@9 @ @ @ @ @ @ @r   r#   ))r8   r   r   r   r   r   typingr   r   r	   r
   rX   r   r   r   mpire.commsr   r   mpire.exceptionr   mpire.insightsr   mpire.paramsr   r   mpire.signalr   mpire.tqdm_utilsr   r   mpire.utilsr   mpire.dashboard.dashboardr   mpire.dashboard.utilsr   mpire.dashboard.managerr   ImportErrorr   r#   r   r   r   <module>r      s           ( ( ( ( ( ( ( ( # # # # # # # # , , , , , , , , , , , ,  ; ; ; ; ; ; ; ; 0 0 0 0 0 0 0 0 / / / / / / ) ) ) ) ) ) : : : : : : : : 7 7 7 7 7 7 2 2 2 2 2 2 2 2 & & & & & &"AAAAAA::::::@@@@@@@ " " ""  " " " " "" 'i@ i@ i@ i@ i@ i@ i@ i@ i@ i@s   "A5 5BB