o
    Zh5                     @   sD  d dl 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 d dlmZ d dlmZ erjd 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S )    )absolute_import)Hub)SOURCE_FOR_STYLE)capture_internal_exceptionsevent_from_exceptionparse_versiontransaction_from_function)IntegrationDidNotEnable)SentryWsgiMiddleware)RequestExtractor)TYPE_CHECKING)_ScopedResponse)Any)Dict)Callable)Optional)
FileUpload	FormsDictLocalRequest)EventProcessorEvent)BottleRouterequestHTTPResponse__version__zBottle not installed)endpointurlc                   @   s*   e Zd ZdZdZd	ddZedd ZdS )
BottleIntegrationbottle 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&   U/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/bottle.py__init__/   s   
zBottleIntegration.__init__c                     sz   t t} | d u rtdt| dk rtdtj  fdd}|t_tjfdd}|t_tjfdd	}|t_d S )
NzUnparsable Bottle version: {})r      zBottle 0.12 or newer required.c                    s<   t j}|t}|d u r ||S t fdd||S )Nc                     s    g| R i |S Nr&   )akw)old_appr%   r&   r'   <lambda>P   s    zOBottleIntegration.setup_once.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)r   currentget_integrationr   r   )r%   environZstart_responsehubintegration)r-   r%   r'   sentry_patched_wsgi_appH   s   
z=BottleIntegration.setup_once.<locals>.sentry_patched_wsgi_appc              	      s   t j}|t}|d u r | |S | }|0 | }| }d|_|t|t	| W d    n1 s5w   Y   | |}W d    |S 1 sJw   Y  |S )Nr    )
r   r/   r0   r   Z
push_scopeZconfigure_scope_nameZadd_event_processor_make_request_event_processorbottle_request)r%   r1   r2   r3   Zscope_managerappscoperes)
old_handler&   r'   _patched_handleY   s$   






z5BottleIntegration.setup_once.<locals>._patched_handlec                    sL   t jt}| g|R i ||d u rS j  fdd}|S )Nc               
      sh   z
| i |}W |S  t y     ty3 } zt| jdddd\}}j||d |d }~ww )Nr    F)typeZhandled)Zclient_optionsZ	mechanism)hint)r   	Exceptionr   optionsZcapture_event)argskwargsr;   	exceptioneventr?   clientr2   Zprepared_callbackr&   r'   wrapped_callback   s   
zUBottleIntegration.setup_once.<locals>.patched_make_callback.<locals>.wrapped_callback)r   r/   r0   r   rG   )r%   rB   rC   r3   rH   )old_make_callbackrF   r'   patched_make_callbackt   s   
z;BottleIntegration.setup_once.<locals>.patched_make_callback)	r   BOTTLE_VERSIONr
   formatr   __call__Z_handler   Z_make_callback)versionr5   r=   rJ   r&   )r-   r<   rI   r'   
setup_once9   s   
zBottleIntegration.setup_onceN)r   )__name__
__module____qualname__
identifierr$   r(   staticmethodrO   r&   r&   r&   r'   r   *   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   r1   r4   r&   r&   r'   env      zBottleRequestExtractor.envc                 C   rV   r*   )r   cookiesr4   r&   r&   r'   rY      rX   zBottleRequestExtractor.cookiesc                 C   s   | j j S r*   )r   bodyreadr4   r&   r&   r'   raw_data   s   zBottleRequestExtractor.raw_datac                 C   s   |   rd S | jj S r*   )is_jsonr   Zformsdecoder4   r&   r&   r'   form   s   zBottleRequestExtractor.formc                 C   s   |   rd S | jjS r*   )r]   r   filesr4   r&   r&   r'   r`      s   zBottleRequestExtractor.filesc                 C   s   |j S r*   )content_length)r%   filer&   r&   r'   size_of_file   s   z#BottleRequestExtractor.size_of_fileN)	rP   rQ   rR   rW   rY   r\   r_   r`   rc   r&   r&   r&   r'   rU      s    rU   c                 C   sV   d}|dkr|j jpd}n|dkr|j jpt|j jpd}|| d< dt| i| d< d S )Nr!   r   r   ZtransactionsourceZtransaction_info)Zrouterulenamer   callbackr   )rE   r$   r   rf   r&   r&   r'    _set_transaction_name_and_source   s   
rh   c                    s    fdd}|S )Nc                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r*   )rh   r$   r   rU   Zextract_into_event)rE   r?   r3   r   r&   r'   event_processor   s   
z6_make_request_event_processor.<locals>.event_processorr&   )r9   r   r3   rj   r&   ri   r'   r7      s   	r7   N)-
__future__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.wsgir   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk._typesr   r   typingr   r   r   r   r    r   r   r   r   r   r   r   r   r8   r   r   rK   ImportErrorr"   r   rU   rh   r7   r&   r&   r&   r'   <module>   s4     l