
    Dj              
          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tries)selfr   s     XC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\sentry_sdk/spotlight.py__init__zSpotlightClient.__init__%   s     '')	
    c                 J   t        j                         }|j                  |       	 | j                  j	                  | j
                  |j                         dddi      }|j                          y # t        $ r(}t        j                  t        |             Y d }~y d }~ww xY w)NPOSTContent-Typezapplication/x-sentry-envelope)r   bodymethodheaders)ioBytesIOserialize_intor   requestr   getvalueclose	Exceptionsentry_loggerwarningstr)r   enveloper   reqes        r   capture_envelopez SpotlightClient.capture_envelope+   s    zz|%	*))##HH]]_"$C	 $ C IIK 	*!!#a&))	*s   A	A1 1	B":BB"N)__name__
__module____qualname__r   r/    r   r   r   r   $   s    *r   r   )MiddlewareMixin)HttpResponseServerErrorHttpResponseHttpRequest)settingsz/assets/main.jsz4<script type="module" crossorigin src="{}"></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>r?   M   s)      ) 	rwwu~&)s   *,c                   >     e 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_responserF   spotlight_client	__class__s       r   r   zSpotlightMiddleware.__init__U   ss    G\*!(nnDO#99;EE'%%f "(,,"6"67G7K7KU"SDr   c                    | j                   	 t        j                  j                  | j                  t
              }t        j                  j                  |d      }t        j                  j                  |       t        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    zUCannot get Spotlight JS to inject at %s. SpotlightMiddleware will not be very useful.)exc_info)_spotlight_scriptrH   rI   rJ   rK   SPOTLIGHT_JS_ENTRY_PATHr%   RequesturlopenSPOTLIGHT_JS_SNIPPET_PATTERNr;   errorURLErrorr)   debug)r   spotlight_js_urlr-   errs       r   spotlight_scriptz$SpotlightMiddleware.spotlight_scriptf   s     %%-'-||';';++-D($ !..00(% 1 C NN**3/-I-P-P(.D* )))4))) ||,, !''o(!$  )))s   BB. .C8C33C8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   r?   z7SpotlightMiddleware.process_response.<locals>.<genexpr>   s      ( 	(s   r   r:   ;r      utf-8z	text/htmlc              3   *   K   | ]  }|d kD  r|  yw)Nr3   )r=   idxs     r   r?   z7SpotlightMiddleware.process_response.<locals>.<genexpr>   s!       8 s   c              3   r   K   | ].  }j                   j                  |j                               0 y wr_   )contentrfindencode)r=   body_variantencodingresponses     r   r?   z7SpotlightMiddleware.process_response.<locals>.<genexpr>   s5      $ , %,,22<3F3Fx3PQ$s   47zContent-Length)tupler!   getlowersplitlen
startswithCHARSET_PREFIXr\   	streamingri   rk   nextBODY_CLOSE_TAG_POSSIBILITIES
has_header)	r   _requestrn   content_type_headercontent_typecontent_length	injectioninjection_siterm   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                  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)Nrd   z<html>)spotlight_urlevent_id)r8   DEBUGrH   r%   rU   rK   readdecoderF   capture_exceptionr5   replaceSPOTLIGHT_ERROR_PAGE_SNIPPETr;   rW   rX   )r   rz   	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)
r0   r1   r2   rR   r   propertyr\   r   r   __classcell__)rN   s   @r   rA   rA   R   s.     	T" 
	* 
	*0+	Z	r   rA   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        t        j                  rt!        t"        j$                  j                  dd            rt!        t"        j$                  j                  dd            rit'               5  t        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INFOrp   DEFAULT_SPOTLIGHT_URL
isinstancer+   r8   r   r   osenvironr   
MIDDLEWARE DJANGO_SPOTLIGHT_MIDDLEWARE_PATHtyper   infor   )options_handlerr   
middlewareclients        r   setup_spotlightr      s7   $$SZZ0H'++,UVW
h
OOGLL!
++k
"C
d{#c3 	NN

'BCHI

'DcJK(* 	H!,,J/zA&6d:&6*'G&IJ'# FG	H S!F
KK7=M	H 	Hs   AF  F))3r"   r   r   urllib.parserH   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.deprecationr4   django.httpr5   r6   r7   django.confr8   rS   rV   r   ru   BODY_TAG_NAMEro   zipupperrq   rx   rA   ImportErrorr   r3   r   r   <module>r      s   	  	     
 $   
 ) 
		;	' 7 #M  * *4E8NN$/> !	{ !  NM#( )c-"5"5"79L9L9NOP) $ 
no nh 	  H 	s    A C$ $C10C1