a
    g	g                  	   @   sR  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ddZ$ed ddddZ%ej&ddddZ'eddee e(eee#ee) ddddZ*ee e(eedej&dddd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                  C   s,   zddl } |   W n ty&   Y n0 dS )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    r   T/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/black/concurrency.pymaybe_install_uvloop   s
    r   zasyncio.Task[Any])tasksr   c                 C   s   t d | D ]}|  qdS )zFasyncio signal handler that cancels all `tasks` and reports to stderr.zAborted!N)r   cancel)r   taskr   r   r   r   (   s    r   )loopr   c              	   C   s   zdd t | D }|s>W td}|tj |   dS |D ]}|  qB| t j	|ddi W td}|tj |   n td}|tj |   0 dS )zFCancel all pending tasks on `loop`, wait for them, and close the loop.c                 S   s   g | ]}|  s|qS r   )done).0r   r   r   r   
<listcomp>3       zshutdown.<locals>.<listcomp>zconcurrent.futuresNreturn_exceptionsT)
asyncioZ	all_taskslogging	getLoggersetLevelCRITICALcloser   run_until_completegather)r    Z	to_cancelZ	cf_loggerr   r   r   r   shutdown/   s"    	




r.   T)Z	patchable)sourcesfast
write_backmodereportworkersr   c                 C   s  t   |du rt pd}tjdkr.t|d}zt|d}W n" ttt	fy^   t
dd}Y n0 t }t| zT|t| ||||||d W zt| W td ntd 0 |dur|  n6zt| W td ntd 0 |dur|  0 dS )z4Reformat multiple files using a ProcessPoolExecutor.N   win32<   )max_workers)r/   r0   r1   r2   r3   r    executor)r   os	cpu_countsysplatformminr   r   NotImplementedErrorOSErrorr   r&   Znew_event_loopZset_event_loopr,   schedule_formattingr.   )r/   r0   r1   r2   r3   r4   r9   r    r   r   r   reformat_manyE   sB    






rB   r   r   )r/   r0   r1   r2   r3   r    r9   r   c                    s  i }t jt jfvrFt}t|| \} }t|D ]}	||	tj q2| sNdS g }
g }dt jt jfv rxt	 }|
  fddt| D }| }z$tjt| tjt| W n ty   Y n0 |rtj|tjdI dH \}}|D ]}||}	| r |
| q| r@||	t|  q| rPtjntj}t ju szt ju r|tju r||	 ||	| qq|
rtj|
ddiI dH  |rt || 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                    s*   i | ]"}t  t||qS r   )r&   Zensure_futureZrun_in_executorr   )r"   srcr9   r0   lockr    r2   r1   r   r   
<dictcomp>   s   z'schedule_formatting.<locals>.<dictcomp>)Zreturn_whenr%   T)!r   ZDIFFZ
COLOR_DIFFr   r   sortedr!   r   ZCACHEDr   LockkeysZadd_signal_handlersignalSIGINTr   SIGTERMr?   r&   waitZFIRST_COMPLETEDpop	cancelledappend	exceptionfailedstrresultZYESNOZCHECKr-   r   )r/   r0   r1   r2   r3   r    r9   cachecachedrC   rO   Zsources_to_cachemanagerr   pendingr!   _r   changedr   rD   r   rA   w   sV    



rA   ),__doc__r&   r'   r:   rJ   r<   concurrent.futuresr   r   r   multiprocessingr   pathlibr   typingr   r   r	   r
   Zmypy_extensionsr   Zblackr   r   Zblack.cacher   r   r   r   Z
black.moder   Zblack.outputr   Zblack.reportr   r   r   r   ZAbstractEventLoopr.   boolintrB   rA   r   r   r   r   <module>   sH   2