o
    8f"                     @   s2  d dl Z d dlZd dlmZmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZ zd dlmZ d dlmZ d	d
lmZ W n eyY Z zede dd d ddZ[ww d dlZd dlZd dlmZ d dlmZ e  ZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)ee e!e"e#e$e%e&e'e(e)gZ*dZ+G dd de,Z-G dd  d e,Z.ej/d!d"d#gid$ej0d%e1d&d'd(d)ej0d*e2d+d,d(d)ej3ejdd-e1d.e2d/dfd0d1Z4d/ej5fd2d3Z6d4ej7d5ed/ej8fd6d7Z9d8ee1 d/ej:fd9d:Z;d;e1d/ee<eej= f fd<d=Z>dAd>d?Z?e@d@kre?  dS dS )B    N)ExecutorProcessPoolExecutor)datetimetimezone)partial)freeze_support)SetTuple)web)MultiMapping   )corsz%aiohttp dependency is not installed: z. z5Please re-install black with the '[d]' extra install z.to obtain aiohttp_cors: `pip install black[d]`)version)maybe_install_uvloopzX-Protocol-VersionzX-Line-LengthzX-Python-VariantzX-Skip-Source-First-LinezX-Skip-String-NormalizationzX-Skip-Magic-Trailing-Commaz	X-Previewz
X-UnstablezX-Enable-Unstable-FeaturezX-Fast-Or-SafezX-DiffzX-Black-Versionc                   @      e Zd ZdS )HeaderErrorN__name__
__module____qualname__ r   r   H/home/ertert/spirit/venv/lib/python3.10/site-packages/blackd/__init__.pyr   =       r   c                   @   r   )InvalidVariantHeaderNr   r   r   r   r   r   A   r   r   help_option_namesz-hz--help)context_settingsz--bind-hostzAddress to bind the server to.	localhostT)typehelpdefaultshow_defaultz--bind-portzPort to listen oni  	bind_host	bind_portreturnc                 C   sN   t jt jd t }tj}td| d|  d|  tj|| |dd d d S )N)levelzblackd version z listening on z port T)hostporthandle_signalsprint)	loggingbasicConfigINFOmake_appblack__version__outr
   run_app)r!   r"   appverr   r   r   mainE   s
   r3   c               	   C   sD   t jtg tdR dgd} t }| t dtt|dg | S )NzContent-Type)allow_headers)middlewares/)executor)	r
   Applicationr   BLACK_HEADERSr   
add_routespostr   handle)r1   r7   r   r   r   r,   Y   s   r,   requestr7   c              
      s  t ti}z| jtddkrtjdddW S d}| jtddkr$d}zt| j}W n t	yH } ztjd	|j
d
 dW  Y d }~W S d }~ww | j I d H }| jd urY| jnd}||}ttj}	d}
|jr~|dd }|d | }
||d  }t }||ttj|||dI d H }|d}|d
kr||d  dkr|dd}||krtj|
| }|
| }t| jtd}|rttj}d|	 }d| }t }||ttj||||I d H }tj| j |||dW S  tjy   tjd|d Y S  tj!y  } ztjd	|t"|dW  Y d }~S d }~w t#yA } zt$%d tjd|t"|dW  Y d }~S d }~ww )N1i  z,This server only supports protocol version 1)statustextFsafefastTi  r   utf8 
r   )rB   modez
zIn	zOut	)content_typecharsetheadersr@      )r?   rJ   )r?   rJ   r@   z#Exception during handling a requesti  )&BLACK_VERSION_HEADERr.   rJ   getPROTOCOL_VERSION_HEADERr
   ResponseFAST_OR_SAFE_HEADER
parse_moder   argscontentreadrI   decoder   nowr   utcskip_source_first_linefindasyncioget_event_looprun_in_executorr   r-   format_file_contentsreplaceNothingChangedboolDIFF_HEADERdiffrH   InvalidInputstr	Exceptionr)   	exception)r=   r7   rJ   rB   rF   e	req_bytesrI   req_strthenheaderfirst_newline_positionloopformatted_strnl	only_diffrV   src_namedst_namer   r   r   r<   b   s|   "





 
 r<   rJ   c                 C   sh  zt | ttj}W n ty   tdd w t| v rC| t }zt|\}}W n t	yB } ztdt d|j
d  d d }~ww d}t }t| td}t| td}t| td}t| td}	t| td}
t }| tdd}|D ]%}| }|rz
|tj|  W q~ ty   tdt d| d w q~tj||||| | |	|
|d	S )	Nz Invalid line length header valuezInvalid value for z: r   FrD   ,)	target_versionsis_pyiline_lengthrX   string_normalizationmagic_trailing_commapreviewunstableenabled_features)intrM   LINE_LENGTH_HEADERr-   DEFAULT_LINE_LENGTH
ValueErrorr   PYTHON_VARIANT_HEADERparse_python_variant_headerr   rR   setr`    SKIP_STRING_NORMALIZATION_HEADERSKIP_MAGIC_TRAILING_COMMASKIP_SOURCE_FIRST_LINEPREVIEWUNSTABLEENABLE_UNSTABLE_FEATUREsplitstripaddPreviewKeyErrorFileMode)rJ   rv   valuepyiversionsrg   skip_string_normalizationskip_magic_trailing_commarX   ry   rz   enable_featuresenable_unstable_featurespiecer   r   r   rQ      sh   

rQ   r   c              
   C   s<  | dkr	dt  fS t  }| dD ]}|dr |tdd  }d|v r,|d^}}n|d }t|dkr=|dd  gng }zLt|}|dvrLtd	t|dkrat|d }|d
kr`tdn|d
krgdnd}d| | }|dkrttj|std| d|	tj|  W q t
tfy   tdd w d|fS )Nr   Trs   py.r   r   )      zmajor version must be 2 or 3r   zPython 2 is not supported   r   PYz3.z is not supportedzexpected e.g. '3.7', 'py3.5'F)r   r   
startswithlenr|   r   hasattrr-   TargetVersionr   r   r   )r   r   r   	major_strrestmajorminorversion_strr   r   r   r      s8   


r   c                   C   s   t   t  t  d S )N)r   r   r3   r   r   r   r   patched_main   s   
r   __main__)r#   N)ArZ   r)   concurrent.futuresr   r   r   r   	functoolsr   multiprocessingr   typingr   r	   aiohttpr
   	multidictr   r5   r   ImportErrorieclickr-   _black_versionr   r.   black.concurrencyr   Event_stop_signalrN   r}   r   r   r   r   r   r   r   rP   ra   r9   rL   re   r   r   commandoptionrd   r|   version_optionr3   r8   r,   RequestrO   r<   ModerQ   r`   r   r   r   r   r   r   r   r   <module>   s    

	F 3
!

