o
    0 i                     @   s\  d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
mZmZmZmZ d dlmZ d dlmZ d dlmZ errd 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mZ d dl m!Z!m"Z" zd dlm#Z#m$Z$m%Z%m&Z'm(Z) W n e*y   edw dZ+G dd deZ,G dd deZ-dd Z.dd Z/dd Z0dS )    N)SOURCE_FOR_STYLE)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionparse_versiontransaction_from_function)IntegrationDidNotEnable$_DEFAULT_FAILED_REQUEST_STATUS_CODES_check_minimum_version)SentryWsgiMiddleware)RequestExtractor)TYPE_CHECKING)Set)_ScopedResponse)Any)Dict)Callable)Optional)
FileUpload	FormsDictLocalRequest)EventProcessorEvent)BottleHTTPResponseRouterequest__version__zBottle not installed)endpointurlc                   @   s<   e Zd ZdZde ZdZ	dedddZedd	 Z	d
S )BottleIntegrationbottlez
auto.http. r   )failed_request_status_codesc                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_styler$   )selfr'   r$    r)   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/bottle.py__init__8   s   
zBottleIntegration.__init__c                     s|   t t} tt|  tj tt  fdd}|t_tjt	fdd}|t_t
jt	fdd}|t
_d S )Nc                    s"   t  fddtjd}|||S )Nc                     s    g| R i |S Nr)   )akw)old_appr(   r)   r*   <lambda>T   s    zOBottleIntegration.setup_once.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)Zspan_origin)r   r!   origin)r(   environZstart_responseZ
middleware)r/   r(   r*   sentry_patched_wsgi_appP   s
   
z=BottleIntegration.setup_once.<locals>.sentry_patched_wsgi_appc                    sN   t  t}|d u r | |S t  }d|_|t| t|  | |}|S )Nr"   )	
sentry_sdk
get_clientget_integrationr!   Zget_isolation_scope_nameZadd_event_processor_make_request_event_processorbottle_request)r(   r2   integrationscoperes)
old_handler)   r*   _patched_handle^   s   


z5BottleIntegration.setup_once.<locals>._patched_handlec                    sB   | g|R i |t  t  d u rS  fdd}|S )Nc               
      sb   z	| i |}W n t y } zt|dd |d }~ww t|tr/|j jv r/t|dd |S )NF)handledT)	Exception_capture_exception
isinstancer   status_coder$   )argskwargsr=   	exceptionr;   Zprepared_callbackr)   r*   wrapped_callback{   s   zUBottleIntegration.setup_once.<locals>.patched_make_callback.<locals>.wrapped_callback)r5   r6   r7   r!   )r(   rE   rF   rI   )old_make_callbackrH   r*   patched_make_callbackr   s   z;BottleIntegration.setup_once.<locals>.patched_make_callback)r   BOTTLE_VERSIONr   r!   r   __call__r   Z_handle	functoolswrapsr   Z_make_callback)versionr4   r?   rK   r)   )r/   r>   rJ   r*   
setup_onceH   s   
	
zBottleIntegration.setup_onceN)r   )
__name__
__module____qualname__
identifierr1   r'   r
   r+   staticmethodrQ   r)   r)   r)   r*   r!   2   s    
r!   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )BottleRequestExtractorc                 C      | j jS r,   )r   r2   r3   r)   r)   r*   env      zBottleRequestExtractor.envc                 C   rX   r,   )r   cookiesr3   r)   r)   r*   r[      rZ   zBottleRequestExtractor.cookiesc                 C   s   | j j S r,   )r   bodyreadr3   r)   r)   r*   raw_data   s   zBottleRequestExtractor.raw_datac                 C   s   |   rd S | jj S r,   )is_jsonr   Zformsdecoder3   r)   r)   r*   form   s   zBottleRequestExtractor.formc                 C   s   |   rd S | jjS r,   )r_   r   filesr3   r)   r)   r*   rb      s   zBottleRequestExtractor.filesc                 C   s   |j S r,   )content_length)r(   filer)   r)   r*   size_of_file   s   z#BottleRequestExtractor.size_of_fileN)	rR   rS   rT   rY   r[   r^   ra   rb   re   r)   r)   r)   r*   rW      s    rW   c                 C   s   d}|dkrz|j jpd}W n% ty   Y nw |dkr4z|j jp(t|j jp(d}W n	 ty3   Y nw || d< dt| i| d< d S )Nr#   r    r   ZtransactionsourceZtransaction_info)ZrouteruleRuntimeErrornamer   callbackr   )eventr'   r   ri   r)   r)   r*    _set_transaction_name_and_source   s&   
rl   c                    s    fdd}|S )Nc                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r,   )rl   r'   r   rW   Zextract_into_event)rk   hintr;   r   r)   r*   event_processor   s   
z6_make_request_event_processor.<locals>.event_processorr)   )appr   r;   ro   r)   rn   r*   r9      s   	r9   c                 C   s0   t | t jd|dd\}}tj||d d S )Nr"   )typer@   )Zclient_optionsZ	mechanism)rm   )r   r5   r6   optionsZcapture_event)rG   r@   rk   rm   r)   r)   r*   rB      s   
rB   )1rN   r5   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r   r   r   r   Zsentry_sdk.integrationsr   r	   r
   r   Zsentry_sdk.integrations.wsgir   Z$sentry_sdk.integrations._wsgi_commonr   typingr   collections.abcr   r   r   r   r   r   r"   r   r   r   Zsentry_sdk._typesr   r   r   r   r   r   r:   r   rL   ImportErrorr%   r!   rW   rl   r9   rB   r)   r)   r)   r*   <module>   s8     ^