o
    0 i                     @   s  d dl Z d dlZd dlmZ d dl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mZ d d	lmZ erXd d
lmZ d dlmZ d dlmZmZ zd dlZW n eyi   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l,m+Z+ Y nw dZ-G dd deZ.dd Z/dd Z0dd Z1dd Z2dd Z3dd Z4d d! Z5dS )"    N)wraps)DidNotEnableIntegration)_filter_headers)SentryAsgiMiddleware)should_send_default_pii)SOURCE_FOR_STYLE)capture_internal_exceptionsensure_integration_enabledevent_from_exception)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                   @   s4   e Zd ZdZde ZdZd
ddZedd Zd	S )QuartIntegrationquartz
auto.http. 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&   i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/quart.py__init__?   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_onceH   s   





zQuartIntegration.setup_onceN)r   )	__name__
__module____qualname__
identifieroriginr$   r(   staticmethodr/   r&   r&   r&   r'   r   9   s    

	r   c                        t j  fdd} | t _d S )Nc                    sT   t  td u r |||I d H S t fddtjdd}||||I d H S )Nc                     s    g| R i |S r)   r&   )akw)old_appr%   r&   r'   <lambda>`   s    zApatch_asgi_app.<locals>.sentry_patched_asgi_app.<locals>.<lambda>   )Zspan_originZasgi_version)
sentry_sdk
get_clientget_integrationr   r   r4   )r%   scopeZreceivesendZ
middlewarer9   )r%   r'   sentry_patched_asgi_appZ   s   z/patch_asgi_app.<locals>.sentry_patched_asgi_app)r   __call__)rB   r&   rA   r'   r-   V   s   
r-   c                     r6   )Nc                     s   | i |  fdd}|S )Nc                    sB   t  rt st tt  fdd}|S  S )Nc                     sF   t  }|jd ur|j  t  }|jd ur|j   | i |S r)   )r<   get_current_scopeZtransactionZupdate_active_threadget_isolation_scopeZprofileZupdate_active_thread_id)argskwargsZcurrent_scopeZsentry_scopeold_funcr&   r'   _sentry_funcx   s   



zTpatch_scaffold_route.<locals>._sentry_route.<locals>.decorator.<locals>._sentry_func)inspect
isfunctionasyncioiscoroutinefunctionr   r
   r   )rI   rJ   Zold_decoratorrH   r'   	decoratorq   s   z>patch_scaffold_route.<locals>._sentry_route.<locals>.decoratorr&   )rF   rG   rP   Z	old_routerO   r'   _sentry_routem   s   z+patch_scaffold_route.<locals>._sentry_route)r   Zroute)rR   r&   rQ   r'   r.   i   s   
r.   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   rV   c                    sl   t  t}|d u rd S t rt }t rt }t	t 
 |j| t  }t| ||}|| d S r)   )r<   r=   r>   r   r   r   Z_get_current_objectr   r   rV   rD   r$   rE   _make_request_event_processorZadd_event_processor)apprG   integrationZrequest_websocketr?   Z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   rZ   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&   )rX   r   rY   rd   r&   rc   r'   rW      s   rW   c                    sL   t  t}|d u rd S t|t  jdddd\}}t j||d d S )Nr    F)typeZhandled)Zclient_optionsZ	mechanism)rb   )r<   r=   r>   r   r   optionsZcapture_event)Zsender	exceptionrG   rY   ra   rb   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)ra   rh   Z	user_infor&   r&   r'   r`      s   "r`   )6rM   rK   	functoolsr   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
   r   typingr   r   r   Zsentry_sdk._typesr   r   rj   ImportErrorr    r   r   r   r   r   r   Zquart.signalsr   r   r   r   r   Zquart.scaffoldr   Zflask.sansio.scaffoldr"   r   r-   r.   rV   r+   rW   r,   r`   r&   r&   r&   r'   <module>   sP      &