o
    FDi                     @  s  d Z ddlm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
 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 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# d0ddZ$d1ddZ%d2ddZ&eddd3d)d*Z'd4d.d/Z(dS )5z
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.
    )annotationsN)Iterable)ExecutorProcessPoolExecutorThreadPoolExecutor)Manager)Path)AnyOptional)
mypyc_attr)	WriteBackformat_file_in_place)Cache)Mode)err)ChangedReportreturnNonec                  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   J/var/www/Datamplify/venv/lib/python3.10/site-packages/black/concurrency.pymaybe_install_uvloop   s   r   tasksIterable['asyncio.Future[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   loopasyncio.AbstractEventLoopc              	   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>7   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   shutdown3   s    
	



r0   T)	patchablesources	set[Path]fastbool
write_backr   moder   reportr   workersOptional[int]c                 C  s0  t   |du rttjdd}|pt pd}tjdkr"t|d}d}|dkr<zt	|d}W n t
ttfy;   Y nw |du rEtdd}t }t| z-|t| ||||||d W zt| W td ntd w |durz|  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)r2   r4   r6   r7   r8   r   executor)r   intosenvironget	cpu_countsysplatformminr   r   NotImplementedErrorOSErrorr   r%   new_event_loopset_event_loopr,   schedule_formattingr0   )r2   r4   r6   r7   r8   r9   r@   r   r   r   r   reformat_manyI   sR   







rN   'Report'r@   
'Executor'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@   r4   lockr   r7   r6   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   SIGTERMrI   r%   waitFIRST_COMPLETEDpop	cancelledappend	exceptionverbose	tracebackprint_exceptiontype__traceback__failedstrresultYESNOCHECKr-   write)r2   r4   r6   r7   r8   r   r@   cachecachedrS   rg   sources_to_cachemanagerr   pendingr!   _r   excchangedr   rT   r   rM      sZ   



rM   )r   r   )r   r   r   r   )r   r    r   r   )r2   r3   r4   r5   r6   r   r7   r   r8   r   r9   r:   r   r   )r2   r3   r4   r5   r6   r   r7   r   r8   rO   r   r    r@   rP   r   r   ))__doc__
__future__r   r%   r'   rB   ra   rF   rk   collections.abcr   concurrent.futuresr   r   r   multiprocessingr   pathlibr   typingr	   r
   mypy_extensionsr   blackr   r   black.cacher   
black.moder   black.outputr   black.reportr   r   r   r   r0   rN   rM   r   r   r   r   <module>   s2    


7