o
    hv                     @   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rhd dlmZ d dlmZ d dlmZ d dlmZ zd dlZW n e yy   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)Dict)Union)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(   `/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/sentry_sdk/integrations/quart.py__init__B   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_onceK   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>b   s    zApatch_asgi_app.<locals>.sentry_patched_asgi_app.<locals>.<lambda>)r   currentget_integrationr!   r   
_run_asgi3__call__)r'   scopereceivesend
middlewarer:   )r'   r)   sentry_patched_asgi_app]   s   z/patch_asgi_app.<locals>.sentry_patched_asgi_app)r   r?   )rE   r(   rD   r)   r/   Y   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_scopeprofile	threadingcurrent_threadidentactive_thread_id)argskwargshubintegrationsentry_scopeold_funcr(   r)   _sentry_funcx   s   


$zTpatch_scaffold_route.<locals>._sentry_route.<locals>.decorator.<locals>._sentry_func)inspect
isfunctionasyncioiscoroutinefunctionr   )rR   rS   old_decoratorrQ   r)   	decoratorq   s   z>patch_scaffold_route.<locals>._sentry_route.<locals>.decoratorr(   )rL   rM   rZ   	old_routerX   r)   _sentry_routem   s   z+patch_scaffold_route.<locals>._sentry_route)r   route)r]   r(   r[   r)   r0   i   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)url_ruleruler   set_transaction_namer
   	Exception)r@   r&   r   name_for_styler(   r(   r)    _set_transaction_name_and_source   s   re   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!   rF   r   r   _get_current_objectr   r   re   r&   _make_request_event_processoradd_event_processor)apprM   rN   rO   r@   request_websocket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headersREMOTE_ADDRr   env)r   
setdefaultr    rl   rm   r   dictrn   r   access_route_add_user_to_event)eventhintrequest_infor   r(   r)   inner   s$   





z,_make_request_event_processor.<locals>.innerr(   )ri   r   rO   ry   r(   rx   r)   rg      s   rg   c                    sL   t j}|td u rd S |j}t||jdddd\}}|j||d d S )Nr"   F)typehandled)client_options	mechanism)rv   )r   r<   r=   r!   clientr   optionscapture_event)sender	exceptionrM   rN   r~   ru   rv   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   rq   _auth_id)ru   r   	user_infor(   r(   r)   rt      s   "rt   ):
__future__r   rV   rT   rH   sentry_sdk.hubr   r   sentry_sdk.integrationsr   r   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk.integrations.asgir   sentry_sdk.scoper	   sentry_sdk.tracingr
   sentry_sdk.utilsr   r   sentry_sdk._functoolsr   sentry_sdk._typesr   typingr   r   r   r   r   ImportErrorr"   r   r   r   r   r   r   quart.signalsr   r   r   r   r   quart.scaffoldr   flask.sansio.scaffoldr$   r!   r/   r0   re   r-   rg   r.   rt   r(   r(   r(   r)   <module>   sV      (