a
    xd/,                     @   s  d dl Z d dl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mZmZm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 erd dlmZ d dlmZ d dl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lm$Z$m%Z%m&Z& d dl'm(Z( z8d dl)m*Z*m+Z, d dl-m.Z. d dl'm/Z/ d dl0m1Z1 W n e2yf   edY n0 e1j3Z4e*j5Z6e/j7Z8z
e*j9Z:W n e;y   Y n0 G dd deZ<G dd deZ=dd Z>dd  Z?d!d" Z9d#d$ Z@d%d& ZAd'd( ZBd)d* ZCd+d, ZDd-d. ZEd/d0 ZFd1d2 ZGdS )3    N)isawaitable)urlparsereraise)Hub)TRANSACTION_SOURCE_COMPONENT)capture_internal_exceptionsevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGE)IntegrationDidNotEnable)RequestExtractor_filter_headers)ignore_logger)MYPY)Any)Callable)Optional)Union)Tuple)Dict)RequestRequestParameters)EventEventProcessorHint)Route)Sanic__version__)SanicException)Router)ErrorHandlerzSanic not installedc                   @   s    e Zd ZdZdZedd ZdS )SanicIntegrationsanic)r   r   c                	   C   s   zt tttdt_W n$ ttfy>   t	d
tY n0 tjdk rRt	dtsbt	dt tdrttd tjdk rt  d S t  d S )	N.zUnparsable Sanic version: {})r      zSanic 0.8 or newer required.zUThe sanic integration for Sentry requires Python 3.7+  or the aiocontextvars package.z0.8.root   	   )tuplemapintSANIC_VERSIONsplitr"   version	TypeError
ValueErrorr   formatr	   r
   
startswithr   _setup_legacy_sanic_setup_sanic r6   r6   U/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/integrations/sanic.py
setup_once8   s$    



zSanicIntegration.setup_onceN)__name__
__module____qualname__
identifierr/   staticmethodr8   r6   r6   r6   r7   r"   4   s   r"   c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )SanicRequestExtractorc                 C   s   | j jd u rdS t| j jS )Nr   )requestbodylenselfr6   r6   r7   content_length`   s    z$SanicRequestExtractor.content_lengthc                 C   s   t | jjS N)dictr?   cookiesrB   r6   r6   r7   rG   f   s    zSanicRequestExtractor.cookiesc                 C   s   | j jS rE   )r?   r@   rB   r6   r6   r7   raw_dataj   s    zSanicRequestExtractor.raw_datac                 C   s   | j jS rE   )r?   formrB   r6   r6   r7   rI   n   s    zSanicRequestExtractor.formc                 C   s
   t  d S rE   )NotImplementedErrorrB   r6   r6   r7   is_jsonr   s    zSanicRequestExtractor.is_jsonc                 C   s   | j jS rE   )r?   jsonrB   r6   r6   r7   rL   v   s    zSanicRequestExtractor.jsonc                 C   s   | j jS rE   )r?   filesrB   r6   r6   r7   rM   z   s    zSanicRequestExtractor.filesc                 C   s   t |jp
dS )Nr6   )rA   r@   )rC   filer6   r6   r7   size_of_file~   s    z"SanicRequestExtractor.size_of_fileN)r9   r:   r;   rD   rG   rH   rI   rK   rL   rM   rO   r6   r6   r6   r7   r>   _   s   r>   c                   C   s   t t_ tt_d S rE   )_startupr   _sentry_error_handler_lookupr!   lookupr6   r6   r6   r7   r5      s    r5   c                   C   s   t t_tt_tt_d S rE   )	_legacy_handle_requestr   handle_request_legacy_router_getr    getrQ   r!   rR   r6   r6   r6   r7   r4      s    r4   c                    s<   |  dt |  dt |  dt t| I d H  d S )Nzhttp.lifecycle.requestzhttp.lifecycle.responsezhttp.routing.after)signal
_hub_enter	_hub_exit_set_transactionold_startuprB   r6   r6   r7   rP      s    rP   c                    s   t j}|td u| j_| jjs$d S t| }t || j_| jj	  | jj
 &}|  |t| W d    n1 s~0    Y  d S rE   )r   currentget_integrationr"   ctxZ_sentry_do_integrationweakrefref_sentry_hub	__enter__configure_scopeclear_breadcrumbsadd_event_processor_make_request_processor)r?   hubweak_requestscoper6   r6   r7   rX      s    
rX   c                    s   | j jd d d  d S rE   )r^   ra   __exit__)r?   _r6   r6   r7   rY      s    rY   c              	      s   t j}|td urt ^ | 6}|j| jjd	d}|j
|td W d    n1 s`0    Y  W d    n1 s~0    Y  d S )N r$   source)r   r\   r]   r"   r   rc   namereplaceappstripset_transaction_namer   )r?   Zroutekwargsrg   ri   Z
route_namer6   r6   r7   rZ      s    
rZ   c                    sP   t | t| |g|R i |  d u r,d S tjtd u r@ S  fdd}|S )Nc                    s   zz< | |}t |r |I d H }|W W tjdkr>t| I d H  S  tyh   t }t| t|  Y n0 W tjdkrt| I d H  ntjdkrt| I d H  0 d S )Nr'   )	r   r"   r/   rY   	Exceptionsysexc_info_capture_exceptionr   )r?   	exceptionresponserw   Zold_error_handlerr6   r7   sentry_wrapped_error_handler   s     




zB_sentry_error_handler_lookup.<locals>.sentry_wrapped_error_handler)rx   old_error_handler_lookupr   r\   r]   r"   )rC   ry   argsrt   r|   r6   r{   r7   rQ      s    rQ   c              	      s   t j}|td u r,t| |g|R i |S t|}t ||}| &}|  |	t
| W d    n1 st0    Y  t| |g|R i |}t|r|I d H }|W  d    S 1 s0    Y  d S rE   )r   r\   r]   r"   old_handle_requestr_   r`   rc   rd   re   rf   r   )rC   r?   r~   rt   rg   rh   ri   rz   r6   r6   r7   rS      s    


,
rS   c              	   G   s   t | g|R  }tj}|td urt  | z}tjrtjdkr| jj	j
}|d j
}|d| r||t|d d  }|j|td n|j|d jtd W d    n1 s0    Y  W d    n1 s0    Y  |S )N)r(      r   z%s.   rm   )old_router_getr   r\   r]   r"   r   rc   r/   r^   rq   ro   r3   rA   rs   r   r9   )rC   r~   rvrg   ri   Zsanic_app_nameZsanic_router6   r6   r7   rU      s"    



BrU   c                 C   st   t j}|t}|d u rd S |j}t 8 t| |jdddd\}}|j||d W d    n1 sf0    Y  d S )Nr#   F)typeZhandled)Zclient_optionsZ	mechanism)hint)	r   r\   r]   r"   clientr   r   optionsZcapture_event)ry   rg   Zintegrationr   eventr   r6   r6   r7   rx     s    

rx   c                    s    fdd}|S )Nc                    s   z |rt |d d trW d S W n ty2   Y n0   }|d u rF| S t  t|}||  | d }t|j}d|j	|j
|jf |d< |j|d< |j|d< d|ji|d	< tt|j|d
< W d    n1 s0    Y  | S )Nrw   r   r?   z	%s://%s%surlZquery_stringmethodZREMOTE_ADDRenvheaders)
issubclassr   KeyErrorr   r>   Zextract_into_eventr   urlsplitr   schemenetlocpathqueryr   Zremote_addrr   rF   r   )r   r   r?   Z	extractorZrequest_infourlpartsrh   r6   r7   sanic_processor2  s.    




0z0_make_request_processor.<locals>.sanic_processorr6   )rh   r   r6   r   r7   rf   0  s    !rf   )Hrv   r_   inspectr   Zsentry_sdk._compatr   r   Zsentry_sdk.hubr   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r   r	   r
   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr   r   Zsentry_sdk.integrations.loggingr   Zsentry_sdk._typesr   typingr   r   r   r   r   r   Zsanic.requestr   r   r   r   r   Zsanic.routerr   r#   r   r   r-   Zsanic.exceptionsr   r    Zsanic.handlersr!   ImportErrorrR   r}   rT   r   rV   r   rP   r[   AttributeErrorr"   r>   r5   r4   rX   rY   rZ   rQ   rS   rU   rx   rf   r6   r6   r6   r7   <module>   s^   
+$"