
    qi                     N    d Z ddlZdZdZdgZdZ G d de      Z G d d	e      Zy)
a{  
Vendored file: Server Side Events (SSE) client for Python.

Source:
- Author: Maxime Petazzoni <maxime.petazzoni@bulix.org>
- Repository: https://github.com/mpetazzoni/sseclient
- File: https://github.com/mpetazzoni/sseclient/blob/main/sseclient/__init__.py

License:
- Apache-2.0 (from upstream project)

Provides a generator of SSE received through an existing HTTP response.
    Nz-Maxime Petazzoni <maxime.petazzoni@bulix.org>zmaxime.petazzoni@bulix.org	SSEClient:c                   *    e Zd ZdZddZd Zd Zd Zy)r   z{Implementation of a SSE client.

    See http://www.w3.org/TR/2009/WD-eventsource-20091029/ for the
    specification.
    c                     t        j                  | j                  j                        | _        | j                  j                  d|       || _        || _        y)a3  Initialize the SSE client over an existing, ready to consume
        event source.

        The event source is expected to be a binary stream and have a close()
        method. That would usually be something that implements
        io.BinaryIOBase, like an httplib or urllib3 HTTPResponse object.
        z+Initialized SSE client from event source %sN)logging	getLogger	__class__
__module___loggerdebug_event_source	_char_enc)selfevent_sourcechar_encs      X/opt/pipecat/venv/lib/python3.12/site-packages/huggingface_hub/_hot_reload/sse_client.py__init__zSSEClient.__init__   sG     (()B)BCH'	))!    c              #      K   d}| j                   D ]5  }|j                  d      D ]  }||z  }|j                  d      s| d}! 7 |r| yyw)ar  Read the incoming event source stream and yield event chunks.

        Unfortunately it is possible for some servers to decide to break an
        event into multiple HTTP chunks in the response. It is thus necessary
        to correctly stitch together consecutive response chunks and find the
        SSE delimiter (empty new line) to yield full, correct event chunks.r   T)s   s   

s   

N)r   
splitlinesendswith)r   datachunklines       r   _readzSSEClient._read-   sk      '' 	E((. ==!@AJD		 J s
   <AAc              #   .  K   | j                         D ]}  }t               }|j                         D ]  }|j                  | j                        }|j                         r|j                  t              rD|j                  t        d      }|d   }||j                  vr| j                  j                  d|       t        |      dkD  r#|d   j                  d      r	|d   dd  }n|d   }nd}|dk(  r|j                  |xx   |dz   z  cc<   ||j                  |<    |j                  s|j                  j                  d      r|j                  dd |_        |j                  xs d	|_        | j                  j                  d
|       |  y w)N   r   z4Saw invalid field %s while parsing Server Side Event  r   
messagezDispatching %s...)r   Eventr   decoder   strip
startswith_FIELD_SEPARATORsplit__dict__r   r   lenr   r   event)r   r   r+   r   r   fieldvalues          r   eventszSSEClient.events>   s|    ZZ\ 6	EGE((* $2{{4>>2 zz|t7G'Hzz"2A6Q .LL&& (;<ACt9q= Aw))#. $Q $Q E F?NN5)UT\9),1ENN5)I$2N :: zz""4("ZZ"-
  ++2EK LL2E:Km6	s   FFc                 8    | j                   j                          y)z'Manually close the event source stream.N)r   close)r   s    r   r0   zSSEClient.closew   s      "r   N)zutf-8)__name__r
   __qualname____doc__r   r   r.   r0    r   r   r   r      s    ""7r#r   c                       e Zd ZdZddZd Zy)r#   z1Representation of an event from the event stream.Nc                 <    || _         || _        || _        || _        y )N)idr+   r   retry)r   r7   r+   r   r8   s        r   r   zEvent.__init__   s    
	
r   c                    dj                  | j                        }| j                  r|dj                  | j                        z  }| j                  r@|dj                  t	        | j                        t	        | j                        rdnd      z  }n|dz  }| j
                  r|dj                  | j
                        z  }|S )Nz	{0} eventz #{0}z, {0} byte{1}sr   z	, no dataz, retry in {0}ms)formatr+   r7   r   r*   r8   )r   r:   s     r   __str__zEvent.__str__   s    tzz*77((A99''DII/2499~2G GA A::#**4::66Ar   )Nr"   r   N)r1   r
   r2   r3   r   r<   r4   r   r   r#   r#   |   s    ;r   r#   )	r3   r   
__author__	__email____all__r'   objectr   r#   r4   r   r   <module>rA      sA    <
(	- a# a#HF r   