o
    ZhF                     @   s  d dl mZ d dlZ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 d dlmZ d d	lmZmZ d d
lmZ d dlmZ erdd dlmZ d dlmZ d dlmZmZ zd dlZW n e yu   dZY nw z d dl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d dl(m)Z)m*Z*m+Z+m,Z,m-Z- W n e y   e	dw zd dl.m/Z/ W n e y   d dl0m/Z/ Y nw dZ1G dd de
Z2dd Z3dd Z4dd Z5dd Z6dd Z7d d! Z8d"d# Z9dS )$    )absolute_importN)_should_send_default_piiHub)DidNotEnableIntegration)_filter_headers)SentryAsgiMiddleware)Scope)SOURCE_FOR_STYLE)capture_internal_exceptionsevent_from_exception)wraps)TYPE_CHECKING)Any)Union)EventEventProcessor)has_request_contexthas_websocket_contextRequestQuartrequest	websocket)got_background_exceptiongot_request_exceptiongot_websocket_exceptionrequest_startedwebsocket_startedzQuart is not installed)Scaffold)endpointurlc                   @   s*   e Zd ZdZdZd	ddZedd ZdS )
QuartIntegrationquart r   c                 C   s"   |t vrtd|t f || _d S )Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES
ValueErrortransaction_style)selfr&    r(   T/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/quart.py__init__A   s   
zQuartIntegration.__init__c                   C   sB   t t tt tt tt tt t  t	  d S N)
r   connect_request_websocket_startedr   r   _capture_exceptionr   r   patch_asgi_apppatch_scaffold_router(   r(   r(   r)   
setup_onceJ   s   





zQuartIntegration.setup_onceN)r   )__name__
__module____qualname__
identifierr&   r*   staticmethodr1   r(   r(   r(   r)   r!   <   s    
	r!   c                        t j  fdd} | t _d S )Nc                    sR   t jtd u r |||I d H S t fdd}|j|_||||I d H S )Nc                     s    g| R i |S r+   r(   )akw)old_appr'   r(   r)   <lambda>a   s    zApatch_asgi_app.<locals>.sentry_patched_asgi_app.<locals>.<lambda>)r   currentget_integrationr!   r   Z
_run_asgi3__call__)r'   scopeZreceivesendZ
middlewarer:   )r'   r)   sentry_patched_asgi_app\   s   z/patch_asgi_app.<locals>.sentry_patched_asgi_app)r   r>   )rB   r(   rA   r)   r/   X   s   
	r/   c                     r7   )Nc                     s   | i |  fdd}|S )Nc                    s8   t  rt st  fdd}|S  S )Nc                     sx   t j}|t}|d u r | i |S | }|jd ur$t j|j_	 | i |W  d    S 1 s5w   Y  d S r+   )
r   r<   r=   r!   configure_scopeZprofile	threadingcurrent_threadidentZactive_thread_id)argskwargshubintegrationZsentry_scopeold_funcr(   r)   _sentry_funcw   s   


$zTpatch_scaffold_route.<locals>._sentry_route.<locals>.decorator.<locals>._sentry_func)inspect
isfunctionasyncioiscoroutinefunctionr   )rL   rM   Zold_decoratorrK   r)   	decoratorp   s   z>patch_scaffold_route.<locals>._sentry_route.<locals>.decoratorr(   )rG   rH   rS   Z	old_routerR   r)   _sentry_routel   s   z+patch_scaffold_route.<locals>._sentry_route)r   Zroute)rU   r(   rT   r)   r0   h   s   
!r0   c                 C   sD   z|j j|j jd}| j|| t| d W d S  ty!   Y d S w )N)r    r   )source)Zurl_ruleruler   Zset_transaction_namer
   	Exception)r?   r&   r   Zname_for_styler(   r(   r)    _set_transaction_name_and_source   s   rY   c                    s   t j}|t}|d u rd S | )}t rt }t r"t	 }t
||j| t| ||}|| W d    d S 1 s?w   Y  d S r+   )r   r<   r=   r!   rC   r   r   Z_get_current_objectr   r   rY   r&   _make_request_event_processorZadd_event_processor)apprH   rI   rJ   r?   Zrequest_websocketZevt_processorr(   r(   r)   r-      s$   

"r-   c                    s    fdd}|S )Nc                    s    d u r| S t  ? | di } j|d<  j|d<  j|d< tt j|d< t r@d j	d i|d< t
|  W d    | S W d    | S 1 sKw   Y  | S )	Nr   r    query_stringmethodheadersZREMOTE_ADDRr   env)r   
setdefaultr    r\   r]   r   dictr^   r   Zaccess_route_add_user_to_event)eventhintZrequest_infor   r(   r)   inner   s$   





z,_make_request_event_processor.<locals>.innerr(   )r[   r   rJ   rf   r(   re   r)   rZ      s   rZ   c                    sL   t j}|td u rd S |j}t||jdddd\}}|j||d d S )Nr"   F)typeZhandled)Zclient_optionsZ	mechanism)rd   )r   r<   r=   r!   clientr   optionsZcapture_event)Zsender	exceptionrH   rI   rh   rc   rd   r(   r(   r)   r.      s   
r.   c                 C   sb   t d u rd S t j}|d u rd S t  | di }t jj|d< W d    d S 1 s*w   Y  d S )Nuserid)
quart_authcurrent_userr   r`   Z_auth_id)rc   rk   Z	user_infor(   r(   r)   rb      s   "rb   ):
__future__r   rP   rN   rD   Zsentry_sdk.hubr   r   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk.integrations.asgir   Zsentry_sdk.scoper	   Zsentry_sdk.tracingr
   Zsentry_sdk.utilsr   r   Zsentry_sdk._functoolsr   Zsentry_sdk._typesr   typingr   r   r   r   rm   ImportErrorr"   r   r   r   r   r   r   Zquart.signalsr   r   r   r   r   Zquart.scaffoldr   Zflask.sansio.scaffoldr$   r!   r/   r0   rY   r-   rZ   r.   rb   r(   r(   r(   r)   <module>   sT      (