o
    h                     @   sh  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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dZ"ded ddfddZ#dej$ddfddZ%edddee de&dedede!dee' ddfdd Z(dee de&dededd!dej$d"d#ddf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)Mode)err)ChangedReportreturnc                  C   s,   zddl } |   W dS  ty   Y dS 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    r   G/var/www/html/ai/venv/lib/python3.10/site-packages/black/concurrency.pymaybe_install_uvloop   s   r   taskszasyncio.Task[Any]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   loopc              	   C   s   zDdd t | D }|sW td}|tj |   dS |D ]}|  q!| t j	|ddi W td}|tj |   dS td}|tj |   w )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>4   s    zshutdown.<locals>.<listcomp>zconcurrent.futuresNreturn_exceptionsT)
asyncio	all_taskslogging	getLoggersetLevelCRITICALcloser   run_until_completegather)r   	to_cancel	cf_loggerr   r   r   r   shutdown0   s    
	



r-   T)	patchablesourcesfast
write_backmodereportworkersc                 C   s  t   |du rttjdd}|p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w t }t| z-|t| ||||||d W zt| W td ntd w |durp|  dS dS zt| W td ntd w |dur|  w w )	z4Reformat multiple files using a ProcessPoolExecutor.NBLACK_NUM_WORKERSr      win32<   )max_workers)r/   r0   r1   r2   r3   r   executor)r   intosenvironget	cpu_countsysplatformminr   r   NotImplementedErrorOSErrorr   r"   new_event_loopset_event_loopr)   schedule_formattingr-   )r/   r0   r1   r2   r3   r4   r:   r   r   r   r   reformat_manyF   sJ   






rH   r   r:   r   c                    s  t }tjtjfvr#|| \} }t|D ]	}	||	tj	 q| s'dS g }
g }dtjtjfv r<t
 }|  fddt| D }| }ztjt| tjt| W n	 tyk   Y nw |rtj|tjdI dH \}}|D ]Q}||}	| r|
| q}|  }r|jrtt|||j ||	t | q}|! rtj"ntj#}tj"u sÈtj$u r|tj#u r||	 ||	| q}|sn|
rtj%|
ddiI dH  |r|&| 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                    s*   i | ]}t  t||qS r   )r"   ensure_futurerun_in_executorr   )r   srcr:   r0   lockr   r2   r1   r   r   
<dictcomp>   s    z'schedule_formatting.<locals>.<dictcomp>)return_whenr!   T)'r   readr   DIFF
COLOR_DIFFfiltered_cachedsortedr   r   CACHEDr   Lockkeysadd_signal_handlersignalSIGINTr   SIGTERMrC   r"   waitFIRST_COMPLETEDpop	cancelledappend	exceptionverbose	tracebackprint_exceptiontype__traceback__failedstrresultYESNOCHECKr*   write)r/   r0   r1   r2   r3   r   r:   cachecachedrK   r_   sources_to_cachemanagerr   pendingr   _r   excchangedr   rL   r   rG   y   sZ   



rG   )r   N)*__doc__r"   r$   r<   rY   r@   rc   concurrent.futuresr   r   r   multiprocessingr   pathlibr   typingr   r   r	   r
   mypy_extensionsr   blackr   r   black.cacher   
black.moder   black.outputr   black.reportr   r   r   r   AbstractEventLoopr-   boolr;   rH   rG   r   r   r   r   <module>   sj    
2