
     @g-                        d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZ d dl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  ej        e          Z G d	 d
          Z	 d dlm Z  e j!        Z"ej!        e _!        d Z#n # e$$ r e%                    d           Y nw xY w G d d          Z&dee'e(e&ef         de)de'fdZ*ee)e'e(e&f         Z+ee+         Z,ee+         Z-ee-e,f         Z. G d de          Z/dS )    N)datetime)partial)	AnyAsyncIterable	AwaitableCallable	CoroutineIteratorMappingOptionalUnion)BackgroundTask)iterate_in_threadpool)Response)ReceiveScopeSendc                   V    e Zd ZU dZdZdZeej        df         e	d<   e
d             ZdS )	AppStatusz8helper for monkey-patching the signal-handler of uvicornFNshould_exit_eventc                      dt           _        t           j        t           j                                         t	          | i | d S )NT)r   should_exitr   setoriginal_handler)argskwargss     E/var/www/piapp/venv/lib/python3.11/site-packages/sse_starlette/sse.pyhandle_exitzAppStatus.handle_exit"   sC     !%	&2'++---$)&)))))    )__name__
__module____qualname____doc__r   r   r   anyioEvent__annotations__staticmethodr    r   r   r   r      sW         BBK26uU[$./666* * \* * *r   r   )Serverc                  (    t           t          _        dS )zxrestores original signal-handler and rolls back monkey-patching.
        Normally this should not be necessary.
        N)r   r)   r   r(   r   r   unpatch_uvicorn_signal_handlerr+   2   s     .r   zUvicorn not used.c                       e Zd Z	 ddddddddee         dee         dee         dee         dee         dee         d	dfd
Zd	efdZ	dS )ServerSentEventN)eventidretrycommentsepdatar.   r/   r0   r1   r2   returnc                    || _         || _        || _        || _        || _        d| _        t          j        d          | _        ||n| j        | _	        dS )a  Send data using EventSource protocol

        :param str data: The data field for the message.
        :param str id: The event ID to set the EventSource object's last
            event ID value to.
        :param str event: The event's type. If this is specified, an event will
            be dispatched on the browser to the listener for the specified
            event name; the web site would use addEventListener() to listen
            for named events. The default event type is "message".
        :param int retry: Instruct the client to try reconnecting after *at least* the
            given number of milliseconds has passed in case of connection loss. Setting
            to 0 does not prevent reconnect attempts, a clean disconnect must be
            implemented on top of the SSE protocol if required (eg. as a special event
            type). The spec requires client to not attempt reconnecting if it receives a
            HTTP 204 No Content response. If a non-integer value is specified, the field
            is ignored.
        :param str comment: A colon as the first character of a line is essence
            a comment, and is ignored. Usually used as a ping message to keep connecting.
            If set, this will be a comment message.
        
z
\r\n|\r|\nN)
r3   r.   r/   r0   r1   DEFAULT_SEPARATORrecompileLINE_SEP_EXPR_sep)selfr3   r.   r/   r0   r1   r2   s          r   __init__zServerSentEvent.__init__=   sW    < 	

!'Z66?CC0F			r   c                 t   t          j                    }| j        a| j                            t          | j                            D ]4}|                    d|            |                    | j                   5| j        P|                    | j        	                    dd| j                              |                    | j                   | j
        P|                    | j        	                    dd| j
                              |                    | j                   | j        a| j                            t          | j                            D ]4}|                    d|            |                    | j                   5| j        `t          | j        t                    st          d          |                    d| j                    |                    | j                   |                    | j                   |                                                    d          S )	Nz:  zid: zevent: zdata: zretry argument must be intzretry: zutf-8)ioStringIOr1   r:   splitstrwriter;   r/   subr.   r3   r0   
isinstanceint	TypeErrorgetvalueencode)r<   bufferchunks      r   rJ   zServerSentEvent.encoded   s   <#+11#dl2C2CDD ( (\%\\***TY''''7LL+//4D474D4DEEFFFLL###:!LL+//4Jdj4J4JKKLLLLL###9 +11#di..AA ( (-e--...TY'''':!dj#.. > <===LL/4://000LL###TY  ''000r   N)
r    r!   r"   r   r   rC   rG   r=   bytesrJ   r(   r   r   r-   r-   <   s         #%G  $ #!%!%G %G %Gsm%G }	%G
 SM%G }%G #%G c]%G 
%G %G %G %GN1 1 1 1 1 1 1r   r-   r3   r2   r4   c                 R   t          | t                    r| S t          | t                    r|                                 S t          | t                    r#|| d<   t          di |                                 S t          t          |           |                                          S )Nr2   )r2   r(   )rF   rN   r-   rJ   dictrC   )r3   r2   s     r   ensure_bytesrQ      s    $ <	D/	*	* <{{}}	D$		 <U&&&&--///s4yyc22299;;;r   c                      e Zd ZU dZeed<   dZ	 	 	 	 	 	 	 	 d!deded	e	e
eef                  d
ede	e         de	e         de	e         de	eg ef                  de	eg ed         f                  ddfdZededdfd            Zed"d            ZdeddfdZdedededdfdZd#deddfdZedeeef         fd            Zej        deeef         ddfd            Zdeddfd Z dS )$EventSourceResponsezImplements the ServerSentEvent Protocol:
    https://www.w3.org/TR/2009/WD-eventsource-20090421/

    Responses must not be compressed by middleware in order to work.
    implementation based on Starlette StreamingResponse
    body_iterator      Ntext/event-streamcontentstatus_codeheaders
media_type
backgroundpingr2   ping_message_factorydata_sender_callable)NNNr4   c
                 &   ||dvrt          d|           d| _        ||n| j        | _        || _        t	          |t
                    r|| _        nt          |          | _        || _        || j	        n|| _	        || _
        |	| _        i }
||
                    |           |
                    dd           d|
d<   d|
d	<   |                     |
           || j        n|| _        d
| _        d | _        t'          j                    | _        d S )N)r6   
z'sep must be one of: \r\n, \r, \n, got: r6   zCache-Controlzno-cachez
keep-alive
ConnectionnozX-Accel-BufferingT)
ValueErrorr7   r2   r^   rF   r   rT   r   rY   r[   r\   r_   update
setdefaultinit_headersDEFAULT_PING_INTERVALping_intervalactive
_ping_taskr$   Lock
_send_lock)r<   rX   rY   rZ   r[   r\   r]   r2   r^   r_   _headerss              r   r=   zEventSourceResponse.__init__   s7    ?s*>>>P3PPQQQ!'/33t/E$8!g}-- 	@!(D!6w!?!?D&-7-?$//Z$$8!#%OOG$$$
 	OZ888!-(,$%(###;?<T77T  *,,r   receivec                 z   K   	  |              d {V }|d         dk    rt                               d           d S 9)NTtypezhttp.disconnectz+Got event: http.disconnect. Stop streaming.)_logdebug)rp   messages     r   listen_for_disconnectz)EventSourceResponse.listen_for_disconnect   sO      	#GIIooooooGv"333

HIII		r   c                     K   t           j        rd S t           j        t          j                    t           _        t           j        rd S t           j                                         d {V  d S rM   )r   r   r   r$   r%   waitr(   r   r   listen_for_exit_signalz*EventSourceResponse.listen_for_exit_signal   sr         	F &.*/+--I'   	F )..00000000000r   sendc                   K    |d| j         | j        d           d {V  | j        2 3 d {V }t          || j                  }t
                              d|                                             |d|dd           d {V  a6 | j        4 d {V  d| _	         |dddd           d {V  d d d           d {V  d S # 1 d {V swxY w Y   d S )	Nzhttp.response.start)rr   statusrZ   zchunk: http.response.bodyTrr   body	more_bodyFr   )
rY   raw_headersrT   rQ   r2   rs   rt   decodern   rk   )r<   rz   r3   rL   s       r   stream_responsez#EventSourceResponse.stream_response   s     d-*+ 
 
 	
 	
 	
 	
 	
 	
 	
 , 	Y 	Y 	Y 	Y 	Y 	Y 	Y$ tx00EJJ111222$ 4eRVWWXXXXXXXXXX -
 ? 	X 	X 	X 	X 	X 	X 	X 	XDK$ 4cPUVVWWWWWWWWW	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	Xs   B
C
CCscopec                 t  K   t          j                    4 d {V dt          g t          d          f         dd ffd}                    |t          | j        |                                         |t          | j        |                                         || j                   | j	        r                    | j	                    |t          | j
        |                     d {V  d d d           d {V  n# 1 d {V swxY w Y   | j        |                                  d {V  d S d S )Nfuncr4   c                 ^   K    |              d {V  j                                          d S rM   )cancel_scopecancel)r   
task_groups    r   wrapz*EventSourceResponse.__call__.<locals>.wrap  s9      dff'..00000r   )r$   create_task_groupr   r   
start_soonr   r   _pingry   r_   rv   r\   )r<   r   rp   rz   r   r   s        @r   __call__zEventSourceResponse.__call__   s
     *,, 	E 	E 	E 	E 	E 	E 	E
1"io*=!> 14 1 1 1 1 1 1
 !!$0Dd(K(KLLL!!$
D(A(ABBB!!$(CDDD( A%%d&?@@@$wt97CCDDDDDDDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E  ?&//########### '&s   CD
DDFforcec                     t           rM   )NotImplementedError)r<   r   s     r   enable_compressionz&EventSourceResponse.enable_compression  s    !!r   c                     | j         S )z'Time interval between two ping massages)_ping_interval)r<   s    r   rj   z!EventSourceResponse.ping_interval  s     ""r   valuec                     t          |t          t          f          st          d          |dk     rt	          d          || _        dS )zoSetter for ping_interval property.

        :param int value: interval in sec between two ping values.
        zping interval must be intr   z$ping interval must be greater then 0N)rF   rG   floatrH   re   r   )r<   r   s     r   rj   z!EventSourceResponse.ping_interval  sO     %#u.. 	97888199CDDD#r   c                 r  K   | j         r,t          j        | j                   d {V  | j        rt          | j        t                    sJ | j        6t          dt          j	                               
                                n&t          |                                 | j                  }t                              d|                                            | j        4 d {V  | j         r |d|dd           d {V  d d d           d {V  n# 1 d {V swxY w Y   | j         *d S d S )Nzping - )r1   zping: r}   Tr~   )rk   r$   sleepr   r^   rF   r   r-   r   utcnowrJ   rQ   r2   rs   rt   r   rn   )r<   rz   r]   s      r   r   zEventSourceResponse._ping(  s      k 	+d1222222222( G!$";XFFFFF ,4  (E(/2C2C(E(EFFFMMOOO!$";";"="=txHH 
 JJ///000        ; $!5tRVWW                                   k 	 	 	 	 	s   -D
D&)D&)rV   NrW   NNNNN)r4   N)F)!r    r!   r"   r#   AsyncContentStreamr&   ri   ContentStreamrG   r   r   rC   r   r   r-   r	   r=   r'   r   rv   ry   r   r   r   r   boolr   propertyr   r   rj   setterr   r(   r   r   rS   rS      sh          &%%% /3-/3"!HL 1' 1'1' 1' '#s(+,	1'
 1' ^,1' sm1' c]1' 'xO0C'DE1' 'R#3445
1' 
1' 1' 1' 1'f W     \ 1 1 1 \1 X$ X4 X X X X"$E $G $4 $D $ $ $ $(" " " " " " " #uS%Z0 # # # X# $5e#4 $ $ $ $ $       r   rS   )0r@   loggingr8   r   	functoolsr   typingr   r   r   r   r	   r
   r   r   r   r$   starlette.backgroundr   starlette.concurrencyr   starlette.responsesr   starlette.typesr   r   r   	getLoggerr    rs   r   uvicorn.mainr)   r   r   r+   ModuleNotFoundErrorrt   r-   rN   rP   rC   rQ   ContentSyncContentStreamr   r   rS   r(   r   r   <module>r      s   				  				            
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  / / / / / / 7 7 7 7 7 7 ( ( ( ( ( ( 0 0 0 0 0 0 0 0 0 0w""* * * * * * * * $######)".F. . . .  $ $ $JJ"#####$C1 C1 C1 C1 C1 C1 C1 C1L	<uUD/3>? 	<c 	<e 	< 	< 	< 	< UD/1
2W% "7+ (*;;<h h h h h( h h h h hs   -B
 
B'&B'