
    ?g                        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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 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m Z  d!dZ!ded   ddfdZ"dejF                  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jF                  ddddfd Z)y)"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)
mypyc_attr)	WriteBackformat_file_in_place)Cache)Mode)err)ChangedReportreturnc                  L    	 ddl } | j                          y# t        $ r Y y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.12/site-packages/black/concurrency.pymaybe_install_uvloopr      s&     s    	##taskszasyncio.Future[Any]c                 H    t        d       | D ]  }|j                           y)zFasyncio signal handler that cancels all `tasks` and reports to stderr.zAborted!N)r   cancel)r   tasks     r   r   r   )   s    
O     loopc                    	 t        j                  |       D cg c]  }|j                         r| }}|sF	 t        j                  d      }|j                  t        j                         | j                          y|D ]  }|j                           | j                  t        j                  |ddi       t        j                  d      }|j                  t        j                         | j                          yc c}w # t        j                  d      }|j                  t        j                         | j                          w xY w)zFCancel all pending tasks on `loop`, wait for them, and close the loop.zconcurrent.futuresNreturn_exceptionsT)asyncio	all_tasksdonelogging	getLoggersetLevelCRITICALcloser   run_until_completegather)r   r   	to_cancel	cf_loggers       r   shutdownr.   0   s    &-&7&7&=Q&=dTYY[T&=	Q %%&:;	7++,

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

 R %%&:;	7++,

s'   D C=C=D =;D =D AET)	patchablesourcesfast
write_backmodereportworkersc                 $   t                |Et        t        j                  j	                  dd            }|xs t        j
                         xs d}t        j                  dk(  rt        |d      }	 t        |      }t        j                         }t        j                   |       	 |j#                  t%        | ||||||             	 t'        |       t        j                   d       ||j'                          yy# t        t        t        f$ r t        d      }Y w xY w# t        j                   d       w xY w# 	 t'        |       t        j                   d       n# t        j                   d       w xY w||j'                          w w xY w)	z4Reformat multiple files using a ProcessPoolExecutor.NBLACK_NUM_WORKERSr      win32<   )max_workers)r0   r1   r2   r3   r4   r   executor)r   intosenvironget	cpu_countsysplatformminr   r   NotImplementedErrorOSErrorr   r"   new_event_loopset_event_loopr*   schedule_formattingr.   )r0   r1   r2   r3   r4   r5   r<   r   s           r   reformat_manyrJ   F   s]     bjjnn%8!<=0R\\^0q
||wgr"5&7; !!#D4  %!
	
	)TN""4(  5 ,g6 5
 &!452 ""4(	)TN""4(G""4(  sB   2C> (!D> 
D$ > D! D!$D;>F E!F!E88Fr   r<   r   c                 &  K   t        j                  |      }|t        j                  t        j                  fvrD|j                  |       \  } }t        |      D ]"  }	|j                  |	t        j                         $ | syg }
g }d}|t        j                  t        j                  fv rt               }|j                         }t        |       D 	ci c]1  }	t        j                  |j                  |t        |	||||            |	3 }}	|j!                         }	 |j#                  t$        j&                  t(        |       |j#                  t$        j*                  t(        |       |r]t        j.                  |t        j0                         d{   \  }}|D ]#  }|j3                  |      }	|j5                         r|
j7                  |       7|j9                         x}rR|j:                  r*t=        j>                  tA        |      ||jB                         |jE                  |	tG        |             |jI                         rt        jJ                  nt        jL                  }|t        jJ                  u s$|t        jN                  u r#|t        jL                  u r|j7                  |	       |j                  |	|       & |r]|
rt        jP                  |
ddi d{    |r|jS                  |       yyc c}	w # t,        $ r Y w xY w7 ~7 1w)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`.
    N)return_whenr!   T)*r   readr   DIFF
COLOR_DIFFfiltered_cachedsortedr$   r   CACHEDr   Lockr"   ensure_futurerun_in_executorr   keysadd_signal_handlersignalSIGINTr   SIGTERMrE   waitFIRST_COMPLETEDpop	cancelledappend	exceptionverbose	tracebackprint_exceptiontype__traceback__failedstrresultYESNOCHECKr+   write)r0   r1   r2   r3   r4   r   r<   cachecachedsrcr^   sources_to_cachelockmanagerr   pendingr$   _r   excchangeds                        r   rI   rI   y   s      JJtE)..)*>*>??//8&>CKKW^^, "IDinni&:&:;; )||~ '? #C 	  .T4T	
 		
 # 
  jjlGvw?@ W':Q:QRRaD))D/C~~  &(((>>--d3ic>O>OPc3s8,)-'++GJJ .)//1g6K$++C0C)!  & nni@4@@@$% I   S& 	As\   CL6K7LA
K< #+LLD2LLLL<	L	LL		LL)r   N)*__doc__r"   r%   r>   rX   rB   rb   concurrent.futuresr   r   r   multiprocessingr   pathlibr   typingr   r	   r
   mypy_extensionsr   blackr   r   black.cacher   
black.moder   black.outputr   black.reportr   r   r   r   AbstractEventLoopr.   setboolr=   rJ   rI    r   r   <module>r      sA     	  
  P P #  * * & 1    ((01 d 7,,  , d/ Y/ 
/  /  	/ 
 /  c]/  
/  / dE&YE&
E& E& 	E&
 E& 
#
#E& E& 
E&r   