o
    Zh                     @   s  d dl mZ d dlZd dl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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 zd dlmZ d dlmZ W n eyc   edw d dlmZ erd 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) d dl*m+Z+ d dlm,Z, d dlm-Z-m.Z. e/eddrdd Z0nd dl1m0Z0 dZ2G dd deZ3dd  Z4d!d" Z5G d#d$ d$eZ6d%d& Z7dS )'    )absolute_importN)Hub_should_send_default_pii)Scope)SOURCE_FOR_STYLE)capture_internal_exceptionsevent_from_exception)reraise	iteritems)IntegrationDidNotEnable)RequestExtractor)SentryWsgiMiddleware)HTTPException)RequestzPyramid not installed)TYPE_CHECKING)Response)Any)_ScopedResponse)Callable)Dict)Optional)RequestCookies)cgi_FieldStorage)ExcInfo)EventEventProcessorauthenticated_useridc                 C   s   | j S Nr   )request r!   V/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/pyramid.pyr   ,   s   r   
route_nameZroute_patternc                   @   s*   e Zd ZdZdZd	ddZedd ZdS )
PyramidIntegrationpyramid 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!   r!   r"   __init__=   s   
zPyramidIntegration.__init__c                     sf   ddl m}  | j  fdd}|| _ttdr#tjfdd}|t_| jjfdd	}|| j_d S )
Nr   )routerc                    s|   t j}|t}|d ur2| }t||j| |tt	
|| W d    n1 s-w   Y   | |g|R i |S r   )r   currentget_integrationr%   Zconfigure_scope _set_transaction_name_and_sourcer*   Zadd_event_processor_make_event_processorweakrefref)registryr    argskwargshubintegrationscope)old_call_viewr!   r"   sentry_patched_call_viewM   s   

z?PyramidIntegration.setup_once.<locals>.sentry_patched_call_viewinvoke_exception_viewc                    sN    | g|R i |}| j r%t| j r%|jdkr%tjtd ur%t| j  |S )Ni  )exc_infoallZ
status_intr   r.   r/   r%   _capture_exception)r+   r5   r6   rv)old_invoke_exception_viewr!   r"   $sentry_patched_invoke_exception_viewb   s   

zKPyramidIntegration.setup_once.<locals>.sentry_patched_invoke_exception_viewc                    s@   t j}|t}|d u r ||S  fdd}t|||S )Nc                    s<   z | |W S  t y   t }t| t|  Y d S w r   )	Exceptionsysr=   r?   r	   )environstart_responseZeinfo)old_wsgi_callr+   r!   r"   sentry_patched_inner_wsgi_call{   s   zgPyramidIntegration.setup_once.<locals>.sentry_patched_wsgi_call.<locals>.sentry_patched_inner_wsgi_call)r   r.   r/   r%   r   )r+   rE   rF   r7   r8   rH   )rG   r+   r"   sentry_patched_wsgi_callt   s   
	z?PyramidIntegration.setup_once.<locals>.sentry_patched_wsgi_call)r&   r-   Z
_call_viewhasattrr   r<   ZRouter__call__)r-   r;   rB   rJ   r!   )r:   rA   rG   r"   
setup_onceF   s   
zPyramidIntegration.setup_onceN)r$   )__name__
__module____qualname__
identifierr*   r,   staticmethodrM   r!   r!   r!   r"   r%   8   s    
	r%   c                 C   sh   | d d u st | d trd S tj}|td u rd S |j}t| |jdddd\}}|j	||d d S )Nr   r&   F)typeZhandled)Zclient_optionsZ	mechanism)hint)

issubclassr   r   r.   r/   r%   clientr   optionsZcapture_event)r=   r7   rV   eventrT   r!   r!   r"   r?      s   
r?   c                 C   sD   z|j j|j jd}| j|| t| d W d S  ty!   Y d S w )Nr#   )source)Zmatched_routenamepatternZset_transaction_namer   rC   )r9   r*   r    Zname_for_styler!   r!   r"   r0      s   r0   c                   @   sD   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S )PyramidRequestExtractorc                 C      | j jS r   )r    path_urlrI   r!   r!   r"   url      zPyramidRequestExtractor.urlc                 C   r]   r   )r    rE   rI   r!   r!   r"   env   r`   zPyramidRequestExtractor.envc                 C   r]   r   )r    cookiesrI   r!   r!   r"   rb      r`   zPyramidRequestExtractor.cookiesc                 C   r]   r   )r    textrI   r!   r!   r"   raw_data   r`   z PyramidRequestExtractor.raw_datac                 C      dd t | jjD S )Nc                 S   s"   i | ]\}}t |d ds||qS filenameNgetattr.0keyvaluer!   r!   r"   
<dictcomp>       
z0PyramidRequestExtractor.form.<locals>.<dictcomp>r
   r    POSTrI   r!   r!   r"   form      
zPyramidRequestExtractor.formc                 C   re   )Nc                 S   s"   i | ]\}}t |d dr||qS rf   rh   rj   r!   r!   r"   rn      ro   z1PyramidRequestExtractor.files.<locals>.<dictcomp>rp   rI   r!   r!   r"   files   rs   zPyramidRequestExtractor.filesc                 C   s.   |j }z	t| jW S  ty   Y dS w )Nr   )fileosfstatfilenost_sizerC   )r+   Zpostdataru   r!   r!   r"   size_of_file   s   z$PyramidRequestExtractor.size_of_fileN)
rN   rO   rP   r_   ra   rb   rd   rr   rt   rz   r!   r!   r!   r"   r\      s    r\   c                    s    fdd}|S )Nc                    s     }|d u r	| S t   t||  W d    n1 sw   Y  t rHt   | di }|dt| W d    | S 1 sCw   Y  | S )Nuserid)r   r\   Zextract_into_eventr   
setdefaultr   )rX   rT   r    Z	user_infoweak_requestr!   r"   pyramid_event_processor   s   
z6_make_event_processor.<locals>.pyramid_event_processorr!   )r   r8   r   r!   r~   r"   r1      s   r1   )8
__future__r   rv   rD   r2   Zsentry_sdk.hubr   r   Zsentry_sdk.scoper   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r   Zsentry_sdk._compatr	   r
   Zsentry_sdk.integrationsr   r   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk.integrations.wsgir   Zpyramid.httpexceptionsr   Zpyramid.requestr   ImportErrorZsentry_sdk._typesr   Zpyramid.responser   typingr   r   r   r   r   Zwebob.cookiesr   Zwebob.compatr   r   r   r   ri   r   Zpyramid.securityr(   r%   r?   r0   r\   r1   r!   r!   r!   r"   <module>   sN    
S*