a
    xd{@                     @   s  d dl Z d dlZd dlZd dlmZ d dlZd dlmZmZmZ d dl	m
Z
mZmZmZ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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% 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-m.Z. d dl/m0Z0 edZ1dd Z2G dd de3Z4d dl$m%Z% e%rG dd dee)e5e'f Z6G dd dee4Z7ndd  Z6d d  Z7dS )!    N)datetime)string_types	text_type	iteritems)capture_internal_exceptionscurrent_stacktracedisable_capture_eventformat_timestampget_type_nameget_default_releasehandle_in_applogger)	serialize)make_transport)DEFAULT_OPTIONSSDK_INFOClientConstructor)setup_integrations)
ContextVar)SessionFlusher)Envelope)has_tracestate_enabledreinflate_tracestate)MYPY)Any)Callable)Dict)Optional)Scope)EventHint)SessionZclient_init_debugc                  O   s6  | r:t | d tttfs$| d d u r:| d }| dd  } nd }t| dkrRtdtt}t| i |}|d ur|dd u r||d< t	|D ]&\}}||vrtd|f |||< q|d d u rt
jd|d< |d d u rt |d< |d d u rt
jd	pd
|d< |d d u r2ttdr2t |d< |S )Nr      z+Only single positional argument is expecteddsnzUnknown option %rZ
SENTRY_DSNreleaseenvironmentZSENTRY_ENVIRONMENT
productionserver_namegethostname)
isinstancer   bytesstrlen	TypeErrordictr   getr   osenvironr   hasattrsocketr(   )argskwargsr#   rvoptionskeyvalue r:   I/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/client.py_get_options+   s.    $

r<   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zed
d Z	dd Z
dd Zd#ddZdd Zdd Zd$ddZdd Zd%ddZd&ddZdd  Zd!d" ZdS )'_ClientzThe client is internally responsible for capturing the events and
    forwarding them to sentry through the configured transport.  It takes
    the client options as keyword arguments and optionally the DSN as first
    argument.
    c                 O   s   t |i || _|   d S N)get_optionsr7   
_init_impl)selfr4   r5   r:   r:   r;   __init__V   s    z_Client.__init__c                 C   s
   d| j iS Nr7   r7   rA   r:   r:   r;   __getstate__[   s    z_Client.__getstate__c                 C   s   |d | _ |   d S rC   )r7   r@   )rA   stater:   r:   r;   __setstate___   s    
z_Client.__setstate__c              	      s   t d} fdd}zxt  jd  t j _t|d _d} jd |vr`td	|t
 jd	  jd
  jd d _W t | nt | 0 d S )NFc                    s    j d ur j |  d S r>   )	transportcapture_envelope)enveloperE   r:   r;   _capture_envelopeh   s    
z-_Client._init_impl.<locals>._capture_envelopedebug)Zcapture_func)alwaysneversmallZmediumrequest_bodiesz3Invalid value for request_bodies. Must be one of {}integrationsZdefault_integrationsZauto_enabling_integrations)Zwith_defaultsZwith_auto_enabling_integrations)_client_init_debugr/   setr7   r   rI   r   session_flusher
ValueErrorformatr   rR   )rA   Z	old_debugrL   rQ   r:   rE   r;   r@   d   s*    

z_Client._init_implc                 C   s
   | j d S )z%Returns the configured DSN as string.r#   rD   rE   r:   r:   r;   r#      s    z_Client.dsnc           
      C   s(  | dd u rt |d< |d urj| ddk}|||}|d u rf| jrb| jjd|rZdndd d S |}| jd rd|vrd	|vrd
|vrt 0 dt| jd dddgi|d
< W d    n1 s0    Y  dD ]6}| |d u r| j| d urt	| j| 
 ||< q| dd u r@tt}t| j |d< ||d< | dd u rXd|d< t|| jd | jd }|d urt|| jd  dd}| jd }|d ur$| ddkr$d }	t   |||pi }	W d    n1 s0    Y  |	d u r td| | jr | jjddd |	}|S )N	timestamptypetransactionZevent_processorerrorZdata_categoryZattach_stacktrace	exception
stacktracethreadsvaluesZwith_localsFT)r^   crashedcurrent)r$   r%   r'   distZsdkrR   platformpythonZin_app_excludeZin_app_includeZ_experimentssmart_transaction_trimming)rf   before_sendzbefore send dropped event (%s))r/   r   utcnowZapply_to_eventrI   record_lost_eventr7   r   r   r   stripr.   r   sortedrR   keysr   r   r   info)
rA   eventhintscopeis_transactionZevent_r8   Zsdk_inforg   Z	new_eventr:   r:   r;   _prepare_event   s~    
&


0
z_Client._prepare_eventc                 C   s   | d}|d u rdS |d }t|d }d|d j|f }| jd D ]6}t|trl||ksd||kr| dS qFt||rF dS qFdS )Nexc_infoFr   z%s.%signore_errorsT)r/   r
   
__module__r7   r)   r   
issubclass)rA   rn   ro   rs   r[   Zerror_type_nameZerror_full_nameZignored_errorr:   r:   r;   _is_ignored_error   s    


z_Client._is_ignored_errorNc                 C   sF   | ddk}|rdS |d uo$|j }|r.dS | ||}|rBdS dS )NrY   rZ   TF)r/   _should_capturerw   )rA   rn   ro   rp   rq   Zignoring_prevents_recursionZignored_by_config_optionr:   r:   r;   rx      s    z_Client._should_capturec                 C   sB   | j d dk ot | j d k}|r>| jr:| jjddd dS dS )NZsample_rateg      ?r[   r\   FT)r7   randomrI   ri   )rA   rn   Znot_in_sample_rater:   r:   r;   _should_sample_error  s    z_Client._should_sample_errorc                 C   s   d}d}d }| dpi  d}|rVd}|D ](}| d}|r,| ddu r,d} qVq,| d}	|jd u r| dpvi  d	}
t|
pi D ]\}}| d
kr|} qq|j|rdnd |	||j|p| d d S )NFr]   r`   T	mechanismZhandleduserrequestheadersz
user-agentra   )statusr|   
user_agenterrors)r/   r   r   lowerupdater   )rA   sessionrn   ra   Zerroredr   
exceptionsr[   r{   r|   r~   kvr:   r:   r;   _update_session_from_event  s0    



z"_Client._update_session_from_eventc                 C   s   t drdS | jdu rdS |du r(i }|d}t|p:d}|du rXt j |d< }| |||sjdS | |||}|du rdS |r|j	nd}|r| 
|| |ddk}|s| |sdS |d}|di d	i d
d}	|di d	i di }
|s|r|d tt d}t rT|	oBt|	dd}|rb||d	< n|
rb|
|d	< t|d}|rd|v r|d |d d< |d |d d< ||d || n
|| |pdD ]}||  q| j| n| j| |S )a  Captures an event.

        :param event: A ready-made event that can be directly sent to Sentry.

        :param hint: Contains metadata about the event that can be read from `before_send`, such as the original exception object or a HTTP request object.

        :returns: An event ID. May be `None` if there is no DSN set or of if the SDK decided to discard the event for other reasons. In such situations setting `debug=True` on `init()` may help.
        FNevent_idr:   rY   rZ   attachmentsZcontextstraceZ
tracestate dynamic_sampling_context)r   Zsent_atzsentry=)r~   ZprofileZtransaction_idr$   Zversion_name)r   r/   rI   r.   uuiduuid4hexrx   rr   _sessionr   rz   popr	   r   rh   r   r   replacer   Zadd_profileZadd_transactionZ	add_eventZadd_itemZto_envelope_itemrJ   capture_event)rA   rn   ro   rp   r   Z	event_optr   rq   r   Zraw_tracestater   r~   Ztracestate_datarK   
attachmentr:   r:   r;   r   B  sn    









z_Client.capture_eventc                 C   s"   |j std n| j| d S )Nz3Discarded session update because of missing release)r$   r   rm   rU   Zadd_session)rA   r   r:   r:   r;   capture_session  s    z_Client.capture_sessionc                 C   s6   | j dur2| j||d | j  | j   d| _ dS )z
        Close the client and shut down the transport. Arguments have the same
        semantics as :py:meth:`Client.flush`.
        Ntimeoutcallback)rI   flushrU   killrA   r   r   r:   r:   r;   close  s
    



z_Client.closec                 C   s:   | j dur6|du r| jd }| j  | j j||d dS )a#  
        Wait for the current events to be sent.

        :param timeout: Wait for at most `timeout` seconds. If no `timeout` is provided, the `shutdown_timeout` option value is used.

        :param callback: Is invoked with the number of pending events and the configured timeout.
        NZshutdown_timeoutr   )rI   r7   rU   r   r   r:   r:   r;   r     s
    


z_Client.flushc                 C   s   | S r>   r:   rE   r:   r:   r;   	__enter__  s    z_Client.__enter__c                 C   s   |    d S r>   )r   )rA   exc_type	exc_valuetbr:   r:   r;   __exit__  s    z_Client.__exit__)N)NN)NN)NN)__name__ru   __qualname____doc__rB   rF   rH   r@   propertyr#   rr   rw   rx   rz   r   r   r   r   r   r   r   r:   r:   r:   r;   r=   O   s2   !
U 
'  
b  
  
r=   c                   @   s   e Zd ZdS )r?   Nr   ru   r   r:   r:   r:   r;   r?     s   r?   c                   @   s   e Zd ZdS )ClientNr   r:   r:   r:   r;   r     s   r   c                   C   s   t S r>   )r<   r:   r:   r:   r;   <lambda>      r   c                   C   s   t S r>   )r=   r:   r:   r:   r;   r     r   )8r0   r   ry   r   r3   Zsentry_sdk._compatr   r   r   Zsentry_sdk.utilsr   r   r   r	   r
   r   r   r   Zsentry_sdk.serializerr   Zsentry_sdk.transportr   Zsentry_sdk.constsr   r   r   Zsentry_sdk.integrationsr   r   Zsentry_sdk.sessionsr   Zsentry_sdk.enveloper   Zsentry_sdk.tracing_utilsr   r   Zsentry_sdk._typesr   typingr   r   r   r   Zsentry_sdk.scoper   r   r    Zsentry_sdk.sessionr!   rS   r<   objectr=   r+   r?   r   r:   r:   r:   r;   <module>   sF   (
$   	
