
    lh!              
          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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mZ d d
lmZ  ej4                  d      ZdZdZ G d d      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  e e,e*j[                         e*j]                                D              Z/ G d de      Z0d Z2y# e1$ r dZ%Y d Z2yw xY w)    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                       e Zd Zd Zd Zy)SpotlightClientc                 R    || _         t        j                         | _        d| _        y )Nr   )urlurllib3PoolManagerhttpfails)selfr   s     f/var/www/pru.catia.catastroantioquia-mas.com/tasa/lib/python3.12/site-packages/sentry_sdk/spotlight.py__init__zSpotlightClient.__init__%   s     '')	
    c                 (   t        j                         }|j                  |       	 | j                  j	                  | j
                  |j                         dddi      }|j                          d| _        y # t        $ r}| j                  dk  r4t        j                  t        |             | xj                  dz  c_        nC| j                  dk(  r/| xj                  dz  c_        t        j                  d       Y d }~y Y d }~y Y d }~y d }~ww xY 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BytesIOserialize_intor   requestr   getvaluecloser   	Exceptionsentry_loggerwarningstr)r   enveloper   reqes        r   capture_envelopez SpotlightClient.capture_envelope+   s    zz|%	))##HH]]_"$C	 $ C IIKDJ 	zzA~%%c!f-

a
q

a
%%s  ! 	s   AA8 8	DA<DDN)__name__
__module____qualname__r   r1    r   r   r   r   $   s    r   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              #   \   K   | ]$  }d j                  dj                  |             & yw)z</{}> N)formatjoin).0charss     r   	<genexpr>rA   W   s)      ) 	rwwu~&)s   *,c                   B     e Zd ZdZdZ fdZed        Zd Zd Z	 xZ
S )SpotlightMiddlewareNc                 $   t         |   |       dd l}|j                  | _        | j                  j                         j                  }|t        j                  d       y t        j                  j                  |j                  d      | _        y )Nr   zPCannot find Spotlight client from SpotlightMiddleware, disabling the middleware.z../)superr   sentry_sdk.apiapi
sentry_sdk
get_clientr   r+   r,   urllibparseurljoinr   _spotlight_url)r   get_responserH   spotlight_client	__class__s       r   r   zSpotlightMiddleware.__init__`   ss    G\*!(nnDO#99;EE'%%f "(,,"6"67G7K7KU"SDr   c                 &   | j                   | j                  	 t        j                  j	                  | j                   t
              }t        j                  j                  |d      }t        j                  j                  |       t        j                  | j                   |      | _        | j                  S | j                  S # t        j                  j                  $ r-}t        j                  d|       Y d }~| j                  S d }~ww xY w)NHEAD)r    )spotlight_urlspotlight_js_urlzUCannot get Spotlight JS to inject at %s. SpotlightMiddleware will not be very useful.)exc_info)rM   _spotlight_scriptrJ   rK   rL   SPOTLIGHT_JS_ENTRY_PATHr'   RequesturlopenSPOTLIGHT_JS_SNIPPET_PATTERNr=   errorURLErrorr+   debug)r   rT   r/   errs       r   spotlight_scriptz$SpotlightMiddleware.spotlight_scriptq   s     "".43I3I3Q'-||';';++-D($ !..00(% 1 C NN**3/-I-P-P&*&9&9)9 .Q .D* )))4))) ||,, !''o(!$  )))s   BC D#DDc                    t        d j                  j                  dd      j                         j	                  d      D              }|d   }t        |      dkD  r.|d   j                  t              r|d   t        t              d  nd| j                  j                  s|dk(  rt        j                        }| j                  j                        }t        d	 fd
t        D        D        |      }j                  d | |z   j                  |d  z   _
        j                  d      r|t        |      z   j                  d<   S )Nc              3   <   K   | ]  }|j                           y wN)strip)r?   ps     r   rA   z7SpotlightMiddleware.process_response.<locals>.<genexpr>   s      ( 	(s   r   r<   ;r   r#   utf-8z	text/htmlc              3   *   K   | ]  }|d kD  r|  yw)Nr5   )r?   idxs     r   rA   z7SpotlightMiddleware.process_response.<locals>.<genexpr>   s!       8 s   c              3   r   K   | ].  }j                   j                  |j                               0 y wrb   )contentrfindencode)r?   body_variantencodingresponses     r   rA   z7SpotlightMiddleware.process_response.<locals>.<genexpr>   s5      $ , %,,22<3F3Fx3PQ$s   47zContent-Length)tupler!   getlowersplitlen
startswithCHARSET_PREFIXr_   	streamingrk   rm   nextBODY_CLOSE_TAG_POSSIBILITIES
has_header)	r   _requestrp   content_type_headercontent_typecontent_length	injectioninjection_sitero   s	     `     @r   process_responsez$SpotlightMiddleware.process_response   sc   "' (!))--nbAGGIOOPST( # /q1L&'!+0CA0F0Q0Q1 /q1#n2E2GH" %%1 ** K/!$X%5%5!6 1188B	!%$0L$ #
" $$_n5 &&~78   &&'789G#i.9XH$$%56Or   c           	         t         j                  r| j                  sy 	 t        j                  j                  | j                        j                         j                  d      }| j                  j                  |      }t        |j                  dt        j                  | j                  |                  S # t        j                  j                  $ r Y y w xY w)Nrf   z<html>)rS   event_id)r:   DEBUGrM   rJ   r'   rY   readdecoderH   capture_exceptionr7   replaceSPOTLIGHT_ERROR_PAGE_SNIPPETr=   r[   r\   )r   r|   	exceptionr   r   s        r   process_exceptionz%SpotlightMiddleware.process_exception   s    >>)<)<NN**4+>+>?DDFMMgV   ??<<YG.%% 4;;*.*=*= <  	 <<(( s   AB: :CC)r2   r3   r4   rV   rM   r   propertyr_   r   r   __classcell__)rP   s   @r   rC   rC   \   s3     	T" 
	* 
	*2+	Z	r   rC   c                 X   t        j                  t        j                        }|j	                  t        j
                  d             t        j                  |       t        j                  t         j                         | j                  d      }|du rt        }t        |t              sy t               5  t        t        j                   rt#        t$        j&                  j                  dd            rt#        t$        j&                  j                  dd            rVt        j(                  }t*        |vr> t-        |      t/        |t*        f            t        _        t        j1                  d       d d d        t3        |      }t        j1                  d|       |S # 1 sw Y   ,xY w)	Nz' [spotlight] %(levelname)s: %(message)sr   TSENTRY_SPOTLIGHT_ON_ERROR1SENTRY_SPOTLIGHT_MIDDLEWAREz(Enabled Spotlight integration for Djangoz%Enabled Spotlight using sidecar at %s)loggingStreamHandlersysstderrsetFormatter	Formatterr   
addHandlersetLevelINFOrr   DEFAULT_SPOTLIGHT_URL
isinstancer-   r   r:   r   r   osenviron
MIDDLEWARE DJANGO_SPOTLIGHT_MIDDLEWARE_PATHtyper   infor   )options_handlerr   
middlewareclients        r   setup_spotlightr      s5   $$SZZ0H'++,UVW
h
OOGLL!
++k
"C
d{#c3	$	& H BJJNN+FLMBJJNN+H#NO!,,J/zA&6d:&6*'G&IJ'# FGH S!F
KK7=M#H Hs   6B?F  F))3r$   r   r   urllib.parserJ   urllib.requesturllib.errorr   r   	itertoolsr   r   typingr   r   r   r   r	   r
   sentry_sdk.utilsr   r+   r   r   sentry_sdk.enveloper   	getLoggerr   r   r   django.utils.deprecationr6   django.httpr7   r8   r9   django.confr:   rW   rZ   r   rw   BODY_TAG_NAMErq   zipupperrs   rz   rC   ImportErrorr   r5   r   r   <module>r      s   	  	     
 $   
 ) 
		;	' 7 #M   FH8NN$/	Q !
	{ !  NM#( )c-"5"5"79L9L9NOP) $ 
po pl 	  H 	s    A C$ $C10C1