o
    hG                     @   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&   a/var/www/html/aiguide_backend/venv/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%   environ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   
push_scopeconfigure_scope_nameadd_event_processor_make_request_event_processorbottle_request)r%   r1   r3   r4   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)typehandled)client_options	mechanism)hint)r   	Exceptionr   optionscapture_event)argskwargsr@   	exceptioneventrG   clientr3   prepared_callbackr&   r'   wrapped_callback   s   
zUBottleIntegration.setup_once.<locals>.patched_make_callback.<locals>.wrapped_callback)r   r/   r0   r   rP   )r%   rK   rL   r4   rR   )old_make_callbackrO   r'   patched_make_callbackt   s   
z;BottleIntegration.setup_once.<locals>.patched_make_callback)	r   BOTTLE_VERSIONr
   formatr   __call___handler   _make_callback)versionr6   rB   rT   r&   )r-   rA   rS   r'   
setup_once9   s   
zBottleIntegration.setup_onceN)r   )__name__
__module____qualname__
identifierr$   r(   staticmethodr[   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   r5   r&   r&   r'   env      zBottleRequestExtractor.envc                 C   rb   r*   )r   cookiesr5   r&   r&   r'   re      rd   zBottleRequestExtractor.cookiesc                 C   s   | j j S r*   )r   bodyreadr5   r&   r&   r'   raw_data   s   zBottleRequestExtractor.raw_datac                 C   s   |   rd S | jj S r*   )is_jsonr   formsdecoder5   r&   r&   r'   form   s   zBottleRequestExtractor.formc                 C   s   |   rd S | jjS r*   )ri   r   filesr5   r&   r&   r'   rm      s   zBottleRequestExtractor.filesc                 C   s   |j S r*   )content_length)r%   filer&   r&   r'   size_of_file   s   z#BottleRequestExtractor.size_of_fileN)	r\   r]   r^   rc   re   rh   rl   rm   rp   r&   r&   r&   r'   ra      s    ra   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   transactionsourcetransaction_info)routerulenamer   callbackr   )rN   r$   r   rv   r&   r&   r'    _set_transaction_name_and_source   s   
rx   c                    s    fdd}|S )Nc                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r*   )rx   r$   r   ra   extract_into_event)rN   rG   r4   r   r&   r'   event_processor   s   
z6_make_request_event_processor.<locals>.event_processorr&   )r>   r   r4   r{   r&   rz   r'   r;      s   	r;   N)-
__future__r   sentry_sdk.hubr   sentry_sdk.tracingr   sentry_sdk.utilsr   r   r   r   sentry_sdk.integrationsr	   r
   sentry_sdk.integrations.wsgir   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk._typesr   r   typingr   r   r   r   r    r   r   r   r   r   r   r   r   r<   r   r   rU   ImportErrorr"   r   ra   rx   r;   r&   r&   r&   r'   <module>   s4     l