
    Dj-3                        d dl Z d dlZd dlmZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZ d dlmZmZ d dlmZ d d	lmZmZ d d
lmZmZmZmZmZmZmZ d dlmZ erDd 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( d dl)m*Z* d dl+m,Z,m-Z-m.Z.m/Z/ d dl0m1Z1 	 d dl2m3Z3m4Z5 d dl6m7Z7 d dl0m8Z8 d dl9m:Z: e:jx                  Z=e3j|                  Z?e8j                  ZA	 e3j                  ZC G d de      ZE G d de      ZFd ZGd  ZHd! ZBd" ZId*d#ZJd$ ZKd% ZLd& ZMd' ZN eeE      d(        ZOd) ZPy# e;$ r	  ed      w xY w# eD$ r Y \w xY w)+    N)isawaitable)urlsplit)continue_trace)OP)IntegrationDidNotEnable)RequestExtractor_filter_headers)ignore_logger)TRANSACTION_SOURCE_COMPONENTTRANSACTION_SOURCE_URL)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEparse_versionreraise)TYPE_CHECKING)	Container)Any)Callable)Optional)Union)Dict)RequestRequestParameters)BaseHTTPResponse)EventEventProcessorExcInfoHint)Route)Sanic__version__)SanicException)Router)ErrorHandlerzSanic not installedc                   F    e Zd ZdZde ZdZ edh      fdZed        Z	y)SanicIntegrationsanicz
auto.http.Ni  c                 *    |xs
 t               | _        y)aG  
        The unsampled_statuses parameter can be used to specify for which HTTP statuses the
        transactions should not be sent to Sentry. By default, transactions are sent for all
        HTTP statuses, except 404. Set unsampled_statuses to None to send transactions for all
        HTTP statuses, including 404.
        N)set_unsampled_statuses)selfunsampled_statusess     aC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\sentry_sdk/integrations/sanic.py__init__zSanicIntegration.__init__?   s     $6#>     c                     t        t              t        _        t        j                  t	        dj                  t                    t        j                  dk  rt	        d      t        st	        dt        z         t        j                  d      rt        d       t        j                  dk  rt                y t                y )NzUnparsable Sanic version: {})r      zSanic 0.8 or newer required.zUThe sanic integration for Sentry requires Python 3.7+  or the aiocontextvars package.z0.8.root   	   )r   SANIC_VERSIONr*   versionr   formatr   r   
startswithr   _setup_legacy_sanic_setup_sanic r3   r1   
setup_oncezSanicIntegration.setup_onceI   s     $1#? ##+=DD]STT##f,=>># 24MN 
 ##F+ &!##g-!r3   )
__name__
__module____qualname__
identifieroriginr;   	frozensetr2   staticmethodrA   r@   r3   r1   r*   r*   :   s;    J*&FG*3SE*: ? # #r3   r*   c                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
SanicRequestExtractorc                 n    | j                   j                  yt        | j                   j                        S )Nr   )requestbodylenr/   s    r1   content_lengthz$SanicRequestExtractor.content_lengthq   s*    <<$4<<$$%%r3   c                 @    t        | j                  j                        S N)dictrL   cookiesrO   s    r1   rT   zSanicRequestExtractor.cookiesw   s    DLL(())r3   c                 .    | j                   j                  S rR   )rL   rM   rO   s    r1   raw_datazSanicRequestExtractor.raw_data{       ||   r3   c                 .    | j                   j                  S rR   )rL   formrO   s    r1   rY   zSanicRequestExtractor.form   rW   r3   c                     t               rR   )NotImplementedErrorrO   s    r1   is_jsonzSanicRequestExtractor.is_json   s    !##r3   c                 .    | j                   j                  S rR   )rL   jsonrO   s    r1   r^   zSanicRequestExtractor.json   rW   r3   c                 .    | j                   j                  S rR   )rL   filesrO   s    r1   r`   zSanicRequestExtractor.files   s    ||!!!r3   c                 4    t        |j                  xs d      S )Nr@   )rN   rM   )r/   files     r1   size_of_filez"SanicRequestExtractor.size_of_file   s    499?##r3   N)rB   rC   rD   rP   rT   rV   rY   r\   r^   r`   rc   r@   r3   r1   rJ   rJ   p   s*    &*!!$!"$r3   rJ   c                  @    t         t        _         t        t        _        y rR   )_startupr$   _sentry_error_handler_lookupr(   lookupr@   r3   r1   r?   r?      s    EN6Lr3   c                  ^    t         t        _        t        t        _        t        t        _        y rR   )	_legacy_handle_requestr$   handle_request_legacy_router_getr'   getrf   r(   rg   r@   r3   r1   r>   r>      s    1E#FJ6Lr3   c                    K    | j                  d      t                | j                  d      t                | j                  d      t               t	        |        d {    y 7 w)Nzhttp.lifecycle.requestzhttp.lifecycle.responsezhttp.routing.after)signal_context_enter_context_exit_set_transactionold_startuprO   s    r1   re   re      sY      *DKK().9 +DKK)*=9 &DKK$%&67 d
s   A A*"A(#A*c                   K   t        j                         j                  t              d u| j                  _        | j                  j
                  sy t        j                  |       }t        j                         | j                  _	        | j                  j                  j                         }|j                          |j                  t        |             t        t        | j                         t"        j$                  | j&                  t(        t        j*                        }t        j,                  |      j                         | j                  _        y w)N)opnamesourcerF   )
sentry_sdk
get_clientget_integrationr*   ctx_sentry_do_integrationweakrefrefisolation_scope_sentry_scope	__enter__clear_breadcrumbsadd_event_processor_make_request_processorr   rS   headersr   HTTP_SERVERpathr   rF   start_transaction_sentry_transaction)rL   weak_requestscopetransactions       r1   ro   ro      s      	//0@AM KK& ;;--;;w'L * : : <GKKKK%%//1E		5lCD W__>>\\%&&K '1&B&B'ik KK#s   EEc                   K   t               5  | j                  j                  s
	 d d d        y t        j                         j                  t              }|d n|j                  }t               5  | j                  j                  j                  |       | j                  j                  xj                  t        |t              xr ||j                  vz  c_
        | j                  j                  j                  d d d        d d d        | j                  j                  j                  d d d        d d d        y # 1 sw Y   9xY w# 1 sw Y   y xY wwrR   )r   rz   r{   rw   rx   ry   r*   statusr   set_http_statussampled
isinstancer.   __exit__r   )rL   responseintegrationresponse_statuss       r1   rp   rp      s    	$	& ={{11= = !++-==>NO"*"2$ )* 	GKK++;;OLKK++33;(89 K#;+J+JJ3 KK++44T4F	G 	!!**4t<%= =	G 	G= =s@   EE	EAE/BD;/E2	E;E	 EEEc                 L  K   | j                   j                  r~t               5  t        j                         }|j
                  j                  | j                  j
                  d      j                  d      }|j                  |t               d d d        y y # 1 sw Y   y xY ww)N .rv   )rz   r{   r   rw   get_current_scoperu   replaceappstripset_transaction_namer   )rL   route_r   
route_names        r1   rq   rq      s     {{))(* 	X002E++GKK,<,<bAGGLJ&&z:V&W	X 	X *	X 	Xs   !B$A+B
B$B!B$c                     t        |       t        | |g|i |y t        j                         j	                  t
              S fd}|S )Nc                 $  K   	  | |      }t        |      r
| d {   }|t        j                  r(t        j                  dk(  rt        |        d {    S S S 7 >7 	# t        $ r* t        j                         }t        |       t        |  Y nw xY w	 t        j                  r)t        j                  dk(  rt        |        d {  7   y y y # t        j                  r)t        j                  dk(  rt        |        d {  7   w w w xY ww)Nr7   )	r   r*   r;   rp   	Exceptionsysexc_info_capture_exceptionr   )rL   	exceptionr   r   old_error_handlers       r1   sentry_wrapped_error_handlerzB_sentry_error_handler_lookup.<locals>.sentry_wrapped_error_handler   s     	-()<H8$!)>  '',<,D,D,O#G,,, -P' * -  	 ||~Hx(X	   '',<,D,D,O#G,,, -P''',<,D,D,O#G,,, -P'ss   DA! AA! 1DADA! D!0BC BC 1D	C
D2DDDD)r   old_error_handler_lookuprw   rx   ry   r*   )r/   r   argskwargsr   r   s        @r1   rf   rf      sY    y!0yR4R6R ../?@H  -( ('r3   c                   K   t        j                         j                  t              t	        | |g|i | d {   S t        j                  |      }t        j                         5 }|j                          |j                  t        |             t	        | |g|i |}t        |      r
| d {   }|cd d d        S 7 7 # 1 sw Y   y xY wwrR   )rw   rx   ry   r*   old_handle_requestr|   r}   r~   r   r   r   r   )r/   rL   r   r   r   r   r   s          r1   ri   ri     s     ../?@H'gGGGGG;;w'L		#	#	% !!!"9,"GH%dGEdEfEx %~H 	 H & s:   ;CC-C+AC
6C7C
<CC

CCc                 >   t        | g| }t        j                         j                  t              t               5  t        j                         }t        j                  rt        j                  dk\  rl| j                  j                  j                  }|d   j                  }|j                  d|z        r|t        |      dz   d  }|j                  |t               n$|j                  |d   j                  t               d d d        |S |S # 1 sw Y   |S xY w)N)r8      r   z%s.   r   )old_router_getrw   rx   ry   r*   r   get_isolation_scoper;   rz   r   ru   r=   rN   r   r   rB   )r/   r   rvr   sanic_app_namesanic_routes         r1   rk   rk   %  s   		$t	$B../?@L(* 	224E'',<,D,D,O "&!2!2 ejj))%.*@A #.c..AA.E.G"HK**(D +  **qENN+G + '	. I2I/	. Is   CDDc                 0   t               5  t        | t        j                         j                  ddd      \  }}|r.t        |d   d   d      r|d   d   j                  r
	 d d d        y t        j                  ||       d d d        y # 1 sw Y   y xY w)	Nr+   F)typehandled)client_options	mechanismr   r   quiet)hint)r   r   rw   rx   optionshasattrr   capture_event)r   eventr   s      r1   r   r   C  s     
%	& 
3*%002::&59
t GD,Q/9d:>Nq>Q>W>W
3 
3 	  T2
3 
3 
3s   AB,BBc                       fd}|S )Nc                    	 |rt        |d   d   t              ry         }|| S t               5  t	        |      }|j                  |        | d   }t        |j                        }|j                  d|j                  |j                  |d<   |j                  |d<   |j                  |d<   d|j                  i|d	<   t        t        |j                               |d
<   d d d        | S # t        $ r Y w xY w# 1 sw Y   | S xY w)Nr   r   rL   z://urlquery_stringmethodREMOTE_ADDRenvr   )
issubclassr&   KeyErrorr   rJ   extract_into_eventr   r   schemenetlocr   queryr   remote_addrr
   rS   r   )r   r   rL   	extractorrequest_infourlpartsr   s         r1   sanic_processorz0_make_request_processor.<locals>.sanic_processorU  s   	
4
#3A#6G .?L(* 	M-g6I((/ +L,H #L ,4>>L(%,^^L"#0'2E2E"FL&5d7??6K&LL#!	M$ 3  			M$ s   C, B1C;,	C87C8;Dr@   )r   r   s   ` r1   r   r   S  s    B r3   rR   )Qr   r|   inspectr   urllib.parser   rw   r   sentry_sdk.constsr   sentry_sdk.integrationsr   r   $sentry_sdk.integrations._wsgi_commonr	   r
   sentry_sdk.integrations.loggingr   sentry_sdk.tracingr   r   sentry_sdk.utilsr   r   r   r   r   r   r   typingr   collections.abcr   r   r   r   r   r   sanic.requestr   r   sanic.responser   sentry_sdk._typesr   r    r!   r"   sanic.routerr#   r+   r$   r%   r:   sanic.exceptionsr&   r'   sanic.handlersr(   ImportErrorrg   r   rj   r   rl   r   re   rr   AttributeErrorr*   rJ   r?   r>   ro   rp   rq   rf   ri   rk   r   r   r@   r3   r1   <module>r      s:   
   !  %   = R 9 S   !)8/FF".9/#+ (.. )) 	..K
3{ 3l!$, !$H77(8=.X(D$< ,-3 .3#O	  .
,
--.  		s   "D1 !E 1D?E
	E
