o
    ^is                     @  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# d2ddZ$d3ddZ%d4ddZ&edd	d5d6d+d,Z'	d5d7d0d1Z(dS )8z
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   V/var/www/html/stock_analysis/be/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)	patchableFsources	set[Path]fastbool
write_backr   moder   reportr   workersOptional[int]no_cachec           	      C  s2  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r{|  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   executorr;   )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;   rA   r   r   r   r   reformat_manyI   sT   






rO   'Report'rA   
'Executor'c                   s  |rdnt }|dur+tjtjfvr+|| \} }	t|	D ]	}
||
tj	 q!| s/dS g }g }dtjtjfv rDt
 }|  fddt| D }| }ztjt| tjt| W n	 tys   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|sv|rtj%|ddiI dH  |r|s|dur|&| dS dS 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rA   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   SIGTERMrJ   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   rA   r;   cachecachedrT   rh   sources_to_cachemanagerr   pendingr!   _r   excchangedr   rU   r   rN      sf   



rN   )r   r   )r   r   r   r   )r   r    r   r   )F)r2   r3   r4   r5   r6   r   r7   r   r8   r   r9   r:   r;   r5   r   r   )r2   r3   r4   r5   r6   r   r7   r   r8   rP   r   r    rA   rQ   r;   r5   r   r   ))__doc__
__future__r   r%   r'   rC   rb   rG   rl   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   rO   rN   r   r   r   r   <module>   s6    


A