o
    0 i!                     @   sx  d dl Z d dlZd dlZd dlZd dl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 erNd 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 edZdZdZG dd dZz<d dlmZ d dl m!Z!m"Z"m#Z# d dl$m%Z% dZ&dZ'dZ(dZ)dZ*e+dd ee,e*- e*.  D Z/G dd deZ0W n e1y   dZ%Y nw dd Z2dS )    N)chainproduct)TYPE_CHECKING)Any)Callable)Dict)Optional)Self)loggerenv_to_boolcapture_internal_exceptions)Envelope	spotlightzhttp://localhost:8969/streamz(sentry_sdk.spotlight.SpotlightMiddlewarec                   @   s   e Zd Zdd Zdd ZdS )SpotlightClientc                 C   s   || _ t | _d| _d S )Nr   )urlurllib3PoolManagerhttpfails)selfr    r   `/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/spotlight.py__init__%   s   

zSpotlightClient.__init__c              
   C   s   t  }|| z| jj| j| dddid}|  d| _W d S  t	yh } z:| jdk r>t
t| |  jd7  _n| jdkrV|  jd7  _t
d W Y d }~d S W Y d }~d S W Y d }~d S d }~ww )	NPOSTContent-Typezapplication/x-sentry-envelope)r   bodymethodheadersr         z]Looks like Spotlight is not running, will keep trying to send events but will not log errors.)ioBytesIOZserialize_intor   requestr   getvaluecloser   	Exceptionsentry_loggerwarningstr)r   enveloper   reqer   r   r   capture_envelope+   s2   


z SpotlightClient.capture_envelopeN)__name__
__module____qualname__r   r,   r   r   r   r   r   $   s    r   )MiddlewareMixin)HttpResponseServerErrorHttpResponseHttpRequest)settingsz/assets/main.jsz<script>window.__spotlight = {{ initOptions: {{ sidecarUrl: '{spotlight_url}', fullPage: false }} }};</script>
<script type="module" crossorigin src="{spotlight_js_url}"></script>
z<html><base href="{spotlight_url}">
<script>window.__spotlight = {{ initOptions: {{ fullPage: true, startFrom: "/errors/{event_id}" }}}};</script>
zcharset=r   c                 c   s     | ]}d  d|V  qdS )z</{}> N)formatjoin).0charsr   r   r   	<genexpr>W   s
    
r:   c                       s@   e Zd ZdZdZ fddZedd Zdd Zdd	 Z	  Z
S )
SpotlightMiddlewareNc                    sT   t  | dd l}|j| _| j j}|d u rtd d S t	j
|jd| _d S )Nr   zPCannot find Spotlight client from SpotlightMiddleware, disabling the middleware.z../)superr   Zsentry_sdk.apiapi
sentry_sdkZ
get_clientr   r&   r'   urllibparseurljoinr   _spotlight_url)r   Zget_responser>   Zspotlight_client	__class__r   r   r   `   s   zSpotlightMiddleware.__init__c              
   C   s   | j d urL| jd u rLz#tj| j t}tjj|dd}tj| t	j
| j |d| _W | jS  tjjyK } ztjd||d W Y d }~| jS d }~ww | jS )NHEAD)r   )spotlight_urlspotlight_js_urlzUCannot get Spotlight JS to inject at %s. SpotlightMiddleware will not be very useful.)exc_info)rB   _spotlight_scriptr?   r@   rA   SPOTLIGHT_JS_ENTRY_PATHr"   RequesturlopenSPOTLIGHT_JS_SNIPPET_PATTERNr6   errorURLErrorr&   debug)r   rG   r*   errr   r   r   spotlight_scriptq   s2   
z$SpotlightMiddleware.spotlight_scriptc                    s   t dd jdd dD }|d }t|dkr/|d tr/|d ttd   nd | jd uryj	sy|d	krytj
}| j }td
d  fddtD D |}j
d | | j
|d   _
dry|t| jd< S )Nc                 s   s    | ]}|  V  qd S N)strip)r8   pr   r   r   r:      s
    
z7SpotlightMiddleware.process_response.<locals>.<genexpr>r   r5   ;r   r   utf-8z	text/htmlc                 s   s    | ]	}|d kr|V  qdS )Nr   )r8   idxr   r   r   r:      s    c                 3   s"    | ]}j | V  qd S rS   )contentrfindencode)r8   Zbody_variantencodingresponser   r   r:      s
    
zContent-Length)tupler   getlowersplitlen
startswithCHARSET_PREFIXrR   Z	streamingrZ   r\   nextBODY_CLOSE_TAG_POSSIBILITIES
has_header)r   _requestr_   Zcontent_type_headercontent_typecontent_lengthZ	injectionZinjection_siter   r]   r   process_response   s@   


z$SpotlightMiddleware.process_responsec                 C   sn   t jr| jsd S ztj| j d}W n tjj	y"   Y d S w | j
|}t|dtj| j|dS )NrW   z<html>)rF   event_id)r4   DEBUGrB   r?   r"   rL   readdecoderN   rO   r>   Zcapture_exceptionr1   replaceSPOTLIGHT_ERROR_PAGE_SNIPPETr6   )r   rj   	exceptionr   rn   r   r   r   process_exception   s"   z%SpotlightMiddleware.process_exception)r-   r.   r/   rI   rB   r   propertyrR   rm   ru   __classcell__r   r   rC   r   r;   \   s    
-r;   c                 C   s   t tj}|t d t| tt j	 | 
d}|du r$t}t|ts+d S t 8 td ur_tjr_ttj
ddr_ttj
ddr_tj}t|vr_t|t|tft_td W d    n1 siw   Y  t|}td| |S )	Nz' [spotlight] %(levelname)s: %(message)sr   TZSENTRY_SPOTLIGHT_ON_ERROR1ZSENTRY_SPOTLIGHT_MIDDLEWAREz(Enabled Spotlight integration for Djangoz%Enabled Spotlight using sidecar at %s)loggingStreamHandlersysstderrsetFormatter	Formatterr
   
addHandlersetLevelINFOra   DEFAULT_SPOTLIGHT_URL
isinstancer(   r   r4   ro   r   osenvironZ
MIDDLEWARE DJANGO_SPOTLIGHT_MIDDLEWARE_PATHtyper   infor   )optionsZ_handlerr   Z
middlewareclientr   r   r   setup_spotlight   s8   




r   )3r    ry   r   urllib.parser?   urllib.requesturllib.errorr   r{   	itertoolsr   r   typingr   r   r   r   r   r	   Zsentry_sdk.utilsr
   r&   r   r   Zsentry_sdk.enveloper   	getLoggerr   r   r   Zdjango.utils.deprecationr0   Zdjango.httpr1   r2   r3   Zdjango.confr4   rJ   rM   rs   rf   ZBODY_TAG_NAMEr`   zipupperrb   rh   r;   ImportErrorr   r   r   r   r   <module>   sR    
#r