a
    xd                     @   s  d dl mZ d dlm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 erd 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mZ zd dlZW n e y   dZY n0 zDd dl!m"Z"m#Z#m$Z$ d dl!m%Z& d dl!m'Z( d dl)m*Z*m+Z+m,Z, W n e y@   edY n0 zd dl-Z-W n e yj   edY n0 dZ.G dd de	Z/dd Z0dd Z1dd Z2G dd deZ3d d! Z4d"d# Z5d$d% Z6dS )&    )absolute_import)MYPY)Hub_should_send_default_pii)DidNotEnableIntegration)RequestExtractor)SentryWsgiMiddleware)Scope)SOURCE_FOR_STYLE)capture_internal_exceptionsevent_from_exception)AnyCallableDictUnion)EventProcessor)_ScopedResponse)FileStorageImmutableMultiDictN)FlaskMarkupRequest)__version__request)before_render_templategot_request_exceptionrequest_startedzFlask is not installedzblinker is not installed)endpointurlc                   @   s*   e Zd ZdZdZd	ddZedd ZdS )
FlaskIntegrationflask 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/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/integrations/flask.py__init__5   s    zFlaskIntegration.__init__c               	      s   z t tttdd d } W n ttfy6   Y n0 | dk rHtdt	t
 t	t t	t tj  fdd}|t_d S )N.   )r   
   z Flask 0.10 or newer is required.c                    s4   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>X       zNFlaskIntegration.setup_once.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)r   currentget_integrationr!   r	   )r'   environZstart_responser1   r'   r)   sentry_patched_wsgi_appS   s
    z<FlaskIntegration.setup_once.<locals>.sentry_patched_wsgi_app)tuplemapintFLASK_VERSIONsplitr%   	TypeErrorr   r   connect_add_sentry_tracer   _request_startedr   _capture_exceptionr   __call__)versionr9   r(   r7   r)   
setup_once>   s     


	zFlaskIntegration.setup_onceN)r   )__name__
__module____qualname__
identifierr&   r*   staticmethodrF   r(   r(   r(   r)   r!   0   s
   
	r!   c                 K   s8   d|v rd S t jjj}|r,td| f nd|d< d S )NZsentry_tracez)<meta name="sentry-trace" content="%s" />r#   )r   r4   scopespanr   Zto_traceparent)sendertemplatecontextextraZsentry_spanr(   r(   r)   rA   _   s    
rA   c                 C   sD   z,|j j|j jd}| j|| t| d W n ty>   Y n0 d S )N)r    r   )source)Zurl_ruleZruler   Zset_transaction_namer   	Exception)rL   r&   r   Zname_for_styler(   r(   r)    _set_transaction_name_and_sourcep   s    
rT   c                 K   st   t j}|t}|d u rd S | <}t }t||j| t	| ||}|
| W d    n1 sf0    Y  d S r.   )r   r4   r5   r!   Zconfigure_scopeflask_requestZ_get_current_objectrT   r&   _make_request_event_processorZadd_event_processor)appkwargshubintegrationrL   r   Zevt_processorr(   r(   r)   rB      s    

rB   c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )FlaskRequestExtractorc                 C   s   | j jS r.   )r   r6   r8   r(   r(   r)   env   s    zFlaskRequestExtractor.envc                 C   s   dd | j j D S )Nc                 S   s4   i | ],\}}|t |tr,t|d kr,|d n|qS )   r   )
isinstancelistlen).0kvr(   r(   r)   
<dictcomp>   s   z1FlaskRequestExtractor.cookies.<locals>.<dictcomp>)r   cookiesitemsr8   r(   r(   r)   re      s    
zFlaskRequestExtractor.cookiesc                 C   s
   | j  S r.   )r   get_datar8   r(   r(   r)   raw_data   s    zFlaskRequestExtractor.raw_datac                 C   s   | j jS r.   )r   formr8   r(   r(   r)   ri      s    zFlaskRequestExtractor.formc                 C   s   | j jS r.   )r   filesr8   r(   r(   r)   rj      s    zFlaskRequestExtractor.filesc                 C   s   | j jS r.   )r   is_jsonr8   r(   r(   r)   rk      s    zFlaskRequestExtractor.is_jsonc                 C   s
   | j  S r.   )r   Zget_jsonr8   r(   r(   r)   json   s    zFlaskRequestExtractor.jsonc                 C   s   |j S r.   )content_length)r'   filer(   r(   r)   size_of_file   s    z"FlaskRequestExtractor.size_of_fileN)rG   rH   rI   r\   re   rh   ri   rj   rk   rl   ro   r(   r(   r(   r)   r[      s   r[   c                    s    fdd}|S )Nc                    sx    d u r| S t   t |  W d    n1 s60    Y  t rtt   t|  W d    n1 sj0    Y  | S r.   )r   r[   Zextract_into_eventr   _add_user_to_event)eventhintr   r(   r)   inner   s    ,&z,_make_request_event_processor.<locals>.innerr(   )rW   r   rZ   rs   r(   r   r)   rV      s    rV   c                 K   sJ   t j}|td u rd S |j}t||jdddd\}}|j||d d S )Nr"   F)typeZhandled)Zclient_optionsZ	mechanism)rr   )r   r4   r5   r!   clientr   optionsZcapture_event)rN   	exceptionrX   rY   ru   rq   rr   r(   r(   r)   rC      s    
rC   c              	   C   s   t d u rd S t j}|d u rd S t  | di }z|d|  W n tyX   Y n0 z|d|j W n ty~   Y n0 z |d|j |d|j W n ty   Y n0 W d    n1 s0    Y  d S )Nuseridemailusername)	flask_loginZcurrent_userr   
setdefaultZget_idAttributeErrorrz   rS   r{   )rq   rx   Z	user_infor(   r(   r)   rp      s(    	rp   )7
__future__r   Zsentry_sdk._typesr   Zsentry_sdk.hubr   r   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk.integrations.wsgir	   Zsentry_sdk.scoper
   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r   typingr   r   r   r   r   r   Zwerkzeug.datastructuresr   r   r|   ImportErrorr"   r   r   r   r   r=   r   rU   Zflask.signalsr   r   r   Zblinkerr$   r!   rA   rT   rB   r[   rV   rC   rp   r(   r(   r(   r)   <module>   sJ   
/%