a
    xdA                     @   sF  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 d dlmZ d dlmZ er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mZmZmZmZmZ d dlmZ d dl m!Z! ededef dZ"edZ#g Z$dd Z%dd Z&dd Z'G dd de(Z)dS )     )copy)deque)chainwraps)MYPY)loggercapture_internal_exceptions)Transaction)
Attachment)Any)Dict)Optional)Deque)List)Callable)TypeVar)
BreadcrumbEventEventProcessorErrorProcessorExcInfoHintType)Span)SessionF.)boundTc                 C   s   t |  d S N)global_event_processorsappend)	processor r#   H/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/scope.pyadd_global_event_processor(   s    r%   c                 C   s   t | | jdS )N)fsetdoc)property__doc__fnr#   r#   r$   _attr_setter-   s    r,   c                    s   t   fdd}|S )Nc                    s<   | j s
d S z$d| _  | g|R i |W d| _ S d| _ 0 d S )NFT)_should_capture)selfargskwargsr*   r#   r$   wrapper4   s    z!_disable_capture.<locals>.wrapperr   )r+   r1   r#   r*   r$   _disable_capture2   s    
r2   c                   @   s  e Zd ZdZdZdd Zdd Zedd Zd	d
 Z	edd Z
edd Zejdd Zd9ddZedd Zdd Zedd Zej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:d)d*Zd+d, Zd;d-d.Zed/d0 Zd1d2 Zd<d3d4Zd5d6 Z d7d8 Z!dS )=ScopezaThe scope holds extra information that should be sent with all
    events that belong to it.
    )_level_name_fingerprint_transaction_transaction_info_user_tags	_contexts_extras_breadcrumbs_event_processors_error_processorsr-   _span_session_attachments_force_auto_session_trackingc                 C   s   g | _ g | _d | _|   d S r   )r>   r?   r5   clearr.   r#   r#   r$   __init__c   s    zScope.__init__c                 C   sZ   d| _ d| _d| _i | _d| _i | _i | _i | _g | _| 	  d| _
d| _d| _d| _dS )zClears the entire scope.NT)r4   r6   r7   r8   r9   r:   r;   r<   rB   clear_breadcrumbsr-   r@   rA   rC   rE   r#   r#   r$   rD   k   s    zScope.clearc                 C   s
   || _ dS )zDWhen set this overrides the level. Deprecated in favor of set_level.Nr4   r.   valuer#   r#   r$   level   s    zScope.levelc                 C   s
   || _ dS )zSets the level for the scope.NrH   rI   r#   r#   r$   	set_level   s    zScope.set_levelc                 C   s
   || _ dS )z0When set this overrides the default fingerprint.N)r6   rI   r#   r#   r$   fingerprint   s    zScope.fingerprintc                 C   s&   | j du rdS | j jdu rdS | j jS )z8Return the transaction (root span) in the scope, if any.N)r@   containing_transactionrE   r#   r#   r$   transaction   s
    
zScope.transactionc                 C   s,   t d || _| jr(| jjr(|| jj_dS )zrWhen set this forces a specific transaction name to be set.

        Deprecated: use set_transaction_name instead.z`Assigning to scope.transaction directly is deprecated: use scope.set_transaction_name() instead.N)r   warningr7   r@   rN   namerI   r#   r#   r$   rO      s    Nc                 C   s>   || _ | jr,| jjr,|| jj_|r,|| jj_|r:|| jd< dS )z?Set the transaction name and optionally the transaction source.sourceN)r7   r@   rN   rQ   rR   r8   )r.   rQ   rR   r#   r#   r$   set_transaction_name   s    

zScope.set_transaction_namec                 C   s   |  | dS )zPWhen set a specific user is bound to the scope. Deprecated in favor of set_user.N)set_userrI   r#   r#   r$   user   s    z
Scope.userc                 C   s"   || _ | jdur| jj|d dS )zSets a user for the scope.N)rU   )r9   rA   updaterI   r#   r#   r$   rT      s    
zScope.set_userc                 C   s   | j S )z,Get/set current tracing span or transaction.)r@   rE   r#   r#   r$   span   s    z
Scope.spanc                 C   s&   || _ t|tr"|}|jr"|j| _d S r   )r@   
isinstancer
   rQ   r7   )r.   rW   rO   r#   r#   r$   rW      s
    
c                 C   s   || j |< dS )z)Sets a tag for a key to a specific value.N)r:   r.   keyrJ   r#   r#   r$   set_tag   s    zScope.set_tagc                 C   s   | j |d dS )zRemoves a specific tag.N)r:   popr.   rZ   r#   r#   r$   
remove_tag   s    zScope.remove_tagc                 C   s   || j |< dS )z5Binds a context at a certain key to a specific value.N)r;   rY   r#   r#   r$   set_context   s    zScope.set_contextc                 C   s   | j |d dS )zRemoves a context.N)r;   r\   r]   r#   r#   r$   remove_context   s    zScope.remove_contextc                 C   s   || j |< dS )z&Sets an extra key to a specific value.N)r<   rY   r#   r#   r$   	set_extra  s    zScope.set_extrac                 C   s   | j |d dS )zRemoves a specific extra key.N)r<   r\   r]   r#   r#   r$   remove_extra  s    zScope.remove_extrac                 C   s   t  | _dS )zClears breadcrumb buffer.N)r   r=   rE   r#   r#   r$   rG     s    zScope.clear_breadcrumbsFc              	   C   s   | j t|||||d dS )z)Adds an attachment to future events sent.)bytespathfilenamecontent_typeadd_to_transactionsN)rB   r!   r   )r.   rc   re   rd   rf   rg   r#   r#   r$   add_attachment  s    
zScope.add_attachmentc                 C   s8   t | jdkr(td| j | jdd= | j| dS )z}Register a scope local event processor on the scope.

        :param func: This function behaves like `before_send.`
           zLToo many event processors on scope! Clearing list to free up some memory: %rN)lenr>   r   rP   r!   )r.   funcr#   r#   r$   add_event_processor0  s    zScope.add_event_processorc                    s.   |dur| | fdd}| j | dS )a  Register a scope local error processor on the scope.

        :param func: A callback that works similar to an event processor but is invoked with the original exception info triple as second argument.

        :param cls: Optionally, only process exceptions of this type.
        Nc                    s<   zt |d  }W n ty(   d}Y n0 |r8| |S | S )N   F)rX   	Exception)eventexc_infoZis_instZcls_Z	real_funcr#   r$   rk   Q  s    

z'Scope.add_error_processor.<locals>.func)r?   r!   )r.   rk   clsr#   rq   r$   add_error_processorA  s
    
zScope.add_error_processorc              	   C   sH  dd }| ddk}| dp"g }| jD ]}|r8|jr*|| q*||d< | jdur`| j|d< |s|di d	g | j | d
du r| jdur| j|d
< | ddu r| j	dur| j	|d< | ddu r| j
dur| j
|d< | ddu r| jdur| j|d< | jr(|di | j | jrD|di | j | jr`|di | j | jdur|di }| ds| j |d< | d}|dur| jD ].}	|	||}
|
du r|||	d  S |
}qtt| jD ]Z}|}
t  |||}
W d   n1 s0    Y  |
du r<|||d  S |
}q|S )zBApplies the information contained on the scope to the given event.c                 S   s   t d|||  d S )Nz%s (%s) dropped event (%s))r   info)ro   causetyr#   r#   r$   _dropf  s    z#Scope.apply_to_event.<locals>._droptyperO   attachmentsNrK   ZbreadcrumbsvaluesrU   Ztransaction_inforM   extratagscontextstracerp   zerror processorzevent processor)getrB   rg   r!   r4   
setdefaultextendr=   r9   r7   r8   r6   r<   rV   r:   r;   r@   Zget_trace_contextr?   r   r    r>   r	   )r.   ro   hintrw   Zis_transactionZattachments_to_send
attachmentr}   rp   Zerror_processorZ	new_eventZevent_processorr#   r#   r$   apply_to_event]  s\    	












*
zScope.apply_to_eventc                 C   s   |j d ur|j | _ |jd ur$|j| _|jd ur6|j| _|jd urN| j|j |jd ur`|j| _|jrt| j|j |jr| j|j |jr| j|j |j	r| j	
|j	 |jr|j| _|jr| j
|j d S r   )r4   r6   r7   r8   rV   r9   r:   r;   r<   r=   r   r@   rB   )r.   Zscoper#   r#   r$   update_from_scope  s,    




zScope.update_from_scopec                 C   sj   |d ur|| _ |d ur|| _|d ur0| j| |d urD| j| |d urX| j| |d urf|| _d S r   )r4   r9   r<   rV   r;   r:   r6   )r.   rU   rK   extrasr}   r|   rM   r#   r#   r$   update_from_kwargs  s    
zScope.update_from_kwargsc                 C   s   t | j}| j|_| j|_| j|_| j|_t| j|_| j	|_	t| j
|_
t| j|_t| j|_t| j|_t| j|_t| j|_| j|_| j|_| j|_| j|_t| j|_|S r   )object__new__	__class__r4   r5   r6   r7   dictr8   r9   r:   r;   r<   r   r=   listr>   r?   r-   r@   rA   rC   rB   )r.   rvr#   r#   r$   __copy__  s&    zScope.__copy__c                 C   s   d| j jtt| | jf S )Nz<%s id=%s name=%s>)r   __name__hexidr5   rE   r#   r#   r$   __repr__  s
    
zScope.__repr__)N)NNNNF)N)NNNNNN)"r   
__module____qualname__r)   	__slots__rF   rD   r,   rK   rL   rM   r(   rO   setterrS   rU   rT   rW   r[   r^   r_   r`   ra   rb   rG   rh   rl   rs   r2   r   r   r   r   r   r#   r#   r#   r$   r3   B   sb   









			     
 

L      
r3   N)*r   collectionsr   	itertoolsr   Zsentry_sdk._functoolsr   Zsentry_sdk._typesr   Zsentry_sdk.utilsr   r	   Zsentry_sdk.tracingr
   Zsentry_sdk.attachmentsr   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zsentry_sdk.sessionr   r   r   r    r%   r,   r2   r   r3   r#   r#   r#   r$   <module>   s2   $
