
    !.d                        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mZmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZ ddlmZ ddlmZmZ dd	lmZmZmZmZ dd
lmZ ddlm Z  ddl!m"Z"m#Z# d!dZ$ded         ddfdZ%dej&        ddfdZ' ed          dee         de(dedede#dee)         ddfd            Z*dee         de(dededddej&        ddddfd Z+dS )"z
Formatting many files at once via multiprocessing. Contains entrypoint and utilities.

NOTE: this module is only imported if we need to format several files at once.
    N)ExecutorProcessPoolExecutorThreadPoolExecutor)Manager)Path)AnyIterableOptionalSet)
mypyc_attr)	WriteBackformat_file_in_place)Cachefilter_cached
read_cachewrite_cache)Mode)err)ChangedReportreturnc                  Z    	 ddl } |                                  dS # t          $ r Y dS w xY w)zIf our environment has uvloop installed we use it.

    This is called only from command-line entry points to avoid
    interfering with the parent process if Black is used as a library.
    r   N)uvloopinstallImportError)r   s    1lib/python3.11/site-packages/black/concurrency.pymaybe_install_uvloopr      sH       s    
**taskszasyncio.Task[Any]c                 V    t          d           | D ]}|                                 dS )zFasyncio signal handler that cancels all `tasks` and reports to stderr.zAborted!N)r   cancel)r   tasks     r   r    r    (   s5    
OOO       loopc                 |   	 d t          j        |           D             }|sJ	 t          j        d          }|                    t          j                   |                                  dS |D ]}|                                 |                     t          j	        |ddi           t          j        d          }|                    t          j                   |                                  dS # t          j        d          }|                    t          j                   |                                  w xY w)zFCancel all pending tasks on `loop`, wait for them, and close the loop.c                 :    g | ]}|                                 |S  )done).0r!   s     r   
<listcomp>zshutdown.<locals>.<listcomp>3   s%    QQQdTYY[[QTQQQr"   zconcurrent.futuresNreturn_exceptionsT)
asyncio	all_taskslogging	getLoggersetLevelCRITICALcloser    run_until_completegather)r#   	to_cancel	cf_loggerr!   s       r   shutdownr6   /   s&   QQg&7&=&=QQQ	 	 %&:;;	7+,,,

  	 	DKKMMMM	 RT R RSSS
 %&:;;	7+,,,

 %&:;;	7+,,,

s    C2 ,=C2 2A	D;T)	patchablesourcesfast
write_backmodereportworkersc                    t                       |t          j                    pd}t          j        dk    rt          |d          }	 t          |          }n-# t          t          t          f$ r t          d          }Y nw xY wt          j                    }t          j        |           	 |                    t          | ||||||                     	 t!          |           t          j        d           n# t          j        d           w xY w||                                 dS dS # 	 t!          |           t          j        d           n# t          j        d           w xY w||                                 w w xY w)z4Reformat multiple files using a ProcessPoolExecutor.N   win32<   )max_workers)r8   r9   r:   r;   r<   r#   executor)r   os	cpu_countsysplatformminr   r   NotImplementedErrorOSErrorr   r+   new_event_loopset_event_loopr2   schedule_formattingr6   )r8   r9   r:   r;   r<   r=   rC   r#   s           r   reformat_manyrN   E   s     ,..%A
|wgr""5&7;;;,g6 5 5 5
 &!4445 !##D4    %!  
	
 
	
 
	
	)TNNN"4((((G"4((((  		)TNNN"4((((G"4((((  sB   A 'BB-)D. C; ;D.F0E?FE**Fr   rC   r   c                   K   i }t           j        t           j        fvrTt                    }t	          ||           \  } }t          |          D ]"}	|                    |	t          j                   #| sdS g }
g }dt           j        t           j        fv r"t                      }|
                                fdt          |           D             }|                                }	                     t          j        t          |                               t          j        t          |           n# t"          $ r Y nw xY w|r=t%          j        |t$          j                   d{V \  }}|D ]}|                    |          }	|                                r|
                    |           B|                                r6|                    |	t5          |                                                     |                                rt          j        nt          j        }t           j        u st           j        u r#|t          j        u r|                    |	           |                    |	|           |=|
rt%          j        |
ddi d{V  |rtA          ||           dS dS )zRun formatting of `sources` in parallel using the provided `executor`.

    (Use ProcessPoolExecutors for actual parallelism.)

    `write_back`, `fast`, and `mode` options are passed to
    :func:`format_file_in_place`.
    Nc                 v    i | ]5}t          j                            t          |                    |6S r&   )r+   ensure_futurerun_in_executorr   )r(   srcrC   r9   lockr#   r;   r:   s     r   
<dictcomp>z'schedule_formatting.<locals>.<dictcomp>   s`         	  .T4T 	
 	
   r"   )return_whenr*   T)!r   DIFF
COLOR_DIFFr   r   sortedr'   r   CACHEDr   Lockkeysadd_signal_handlersignalSIGINTr    SIGTERMrI   r+   waitFIRST_COMPLETEDpop	cancelledappend	exceptionfailedstrresultYESNOCHECKr3   r   )r8   r9   r:   r;   r<   r#   rC   cachecachedrS   rd   sources_to_cachemanagerr   pendingr'   _r!   changedrT   s    ``` ``            @r   rM   rM   w   s       E).)*>???4  'w77&>> 	- 	-CKKW^,,,, IDini&:;;; ))||~~         '??  E jjllGvw???@@@@     *W':QRRRRRRRRRa 	* 	*D))D//C~~ *  &&&&!! 
*c3t~~'7'7#8#89999)-F'++GJ ..)/11g6K6K$++C000C))))!  *"  Ani@4@@@@@@@@@ 3E+T222223 3s   4AE 
EE)r   N),__doc__r+   r-   rD   r^   rF   concurrent.futuresr   r   r   multiprocessingr   pathlibr   typingr   r	   r
   r   mypy_extensionsr   blackr   r   black.cacher   r   r   r   
black.moder   black.outputr   black.reportr   r   r   r    AbstractEventLoopr6   boolintrN   rM   r&   r"   r   <module>r      s      				  



 P P P P P P P P P P # # # # # #       / / / / / / / / / / / / & & & & & & 1 1 1 1 1 1 1 1 E E E E E E E E E E E E             ( ( ( ( ( ( ( (   (./ D    7,     , d. Y. 
.  .  	. 
 .  c].  
.  .  .  . bD3YD3
D3 D3 	D3
 D3 
#D3 D3 
D3 D3 D3 D3 D3 D3r"   