
    vKg3O                     >   S r SSKJr  SSKJr  SSKJr  SrSrSr	 " S S	\
5      r " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S5      rS"S jr " S S5      r " S S5      r " S S5      r " S S5      r " S S 5      rg!)#zBinary Event Stream Decoding    )crc32)unpack)EventStreamError   i   i   c                       \ rS rSrSrSrg)ParserError   z,Base binary flow encoding parsing exception. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r
       T/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/botocore/eventstream.pyr   r      s    6r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )DuplicateHeader    z$Duplicate header found in the event.c                 0   > SU S3n[         TU ]  U5        g )NzDuplicate header present: ""super__init__)selfheadermessage	__class__s      r   r   DuplicateHeader.__init__#   s    /xq9!r   r
   r   r   r   r   r   r   r   __classcell__r   s   @r   r   r       s    ." "r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )InvalidHeadersLength(   z*Headers length is longer than the maximum.c                 <   > SU S[          3n[        TU ]	  U5        g )NzHeader length of  exceeded the maximum of )_MAX_HEADERS_LENGTHr   r   r   lengthr   r   s      r   r   InvalidHeadersLength.__init__+   s%    %fX-FGZF[\!r   r
   r    r"   s   @r   r$   r$   (       4" "r   r$   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )InvalidPayloadLength0   z*Payload length is longer than the maximum.c                 <   > SU S[          3n[        TU ]	  U5        g )NzPayload length of r'   )_MAX_PAYLOAD_LENGTHr   r   r)   s      r   r   InvalidPayloadLength.__init__3   s%    &vh.GH[G\]!r   r
   r    r"   s   @r   r.   r.   0   r,   r   r.   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )ChecksumMismatch8   z8Calculated checksum did not match the expected checksum.c                 8   > SUS SUS 3n[         TU ]  U5        g )NzChecksum mismatch: expected 0x08xz, calculated 0xr   )r   expected
calculatedr   r   s       r   r   ChecksumMismatch.__init__;   s)    28C.PZ[^O_`!r   r
   r    r"   s   @r   r4   r4   8   s    B" "r   r4   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )NoInitialResponseError@   zAn event of type initial-response was not received.

This exception is raised when the event stream produced no events or
the first event in the stream was not of the initial-response type.
c                 (   > Sn[         TU ]  U5        g )Nz0First event was not of the initial-response typer   )r   r   r   s     r   r   NoInitialResponseError.__init__G   s    D!r   r
   r    r"   s   @r   r<   r<   @   s    " "r   r<   c                      \ rS rSrSrSrSrSrSrSr	Sr
S	rS
r\\\S.r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\SS j5       r\SS j5       r\S 5       r\S 5       rSrg)DecodeUtilsL   zUnpacking utility functions used in the decoder.

All methods on this class take raw bytes and return  a tuple containing
the value parsed from the bytes and the number of bytes consumed to parse
that value.
z!Bz!Hz!Iz!bz!hz!iz!qz!III)         c                     g)zThis method consumes none of the provided bytes and returns True.

:type data: bytes
:param data: The bytes to parse from. This is ignored in this method.

:rtype: tuple
:rtype: (bool, int)
:returns: The tuple (True, 0)
)Tr   r
   datas    r   unpack_trueDecodeUtils.unpack_trued   s     r   c                     g)zThis method consumes none of the provided bytes and returns False.

:type data: bytes
:param data: The bytes to parse from. This is ignored in this method.

:rtype: tuple
:rtype: (bool, int)
:returns: The tuple (False, 0)
)Fr   r
   rG   s    r   unpack_falseDecodeUtils.unpack_falseq   s     r   c                 J    [        [        R                  U SS 5      S   nUS4$ )zParse an unsigned 8-bit integer from the bytes.

:type data: bytes
:param data: The bytes to parse from.

:rtype: (int, int)
:returns: A tuple containing the (parsed integer value, bytes consumed)
NrC   r   )r   rA   UINT8_BYTE_FORMATrH   values     r   unpack_uint8DecodeUtils.unpack_uint8~   s*     {44d2Ah?Baxr   c                 J    [        [        R                  U SS 5      S   nUS4$ )zParse an unsigned 32-bit integer from the bytes.

:type data: bytes
:param data: The bytes to parse from.

:rtype: (int, int)
:returns: A tuple containing the (parsed integer value, bytes consumed)
NrE   r   )r   rA   UINT32_BYTE_FORMATrP   s     r   unpack_uint32DecodeUtils.unpack_uint32   s*     {55tBQx@Caxr   c                 J    [        [        R                  U SS 5      S   nUS4$ )zParse a signed 8-bit integer from the bytes.

:type data: bytes
:param data: The bytes to parse from.

:rtype: (int, int)
:returns: A tuple containing the (parsed integer value, bytes consumed)
NrC   r   )r   rA   INT8_BYTE_FORMATrP   s     r   unpack_int8DecodeUtils.unpack_int8   s*     {33T"1X>qAaxr   c                 J    [        [        R                  U SS 5      S   nUS4$ )zParse a signed 16-bit integer from the bytes.

:type data: bytes
:param data: The bytes to parse from.

:rtype: tuple
:rtype: (int, int)
:returns: A tuple containing the (parsed integer value, bytes consumed)
NrD   r   )r   rA   INT16_BYTE_FORMATrP   s     r   unpack_int16DecodeUtils.unpack_int16   *     {44d2Ah?Baxr   c                 J    [        [        R                  U SS 5      S   nUS4$ )zParse a signed 32-bit integer from the bytes.

:type data: bytes
:param data: The bytes to parse from.

:rtype: tuple
:rtype: (int, int)
:returns: A tuple containing the (parsed integer value, bytes consumed)
NrE   r   )r   rA   INT32_BYTE_FORMATrP   s     r   unpack_int32DecodeUtils.unpack_int32   r`   r   c                 J    [        [        R                  U SS 5      S   nUS4$ )zParse a signed 64-bit integer from the bytes.

:type data: bytes
:param data: The bytes to parse from.

:rtype: tuple
:rtype: (int, int)
:returns: A tuple containing the (parsed integer value, bytes consumed)
N   r   )r   rA   INT64_BYTE_FORMATrP   s     r   unpack_int64DecodeUtils.unpack_int64   r`   r   c                 `    [         R                  U   n[        X SU 5      S   nX1-   nXU nXT4$ )aC  Parse a variable length byte array from the bytes.

The bytes are expected to be in the following format:
    [ length ][0 ... length bytes]
where length is an unsigned integer represented in the smallest number
of bytes to hold the maximum length of the array.

:type data: bytes
:param data: The bytes to parse from.

:type length_byte_size: int
:param length_byte_size: The byte size of the preceding integer that
represents the length of the array. Supported values are 1, 2, and 4.

:rtype: (bytes, int)
:returns: A tuple containing the (parsed byte array, bytes consumed).
Nr   )rA   UINT_BYTE_FORMATr   )rH   length_byte_sizeuint_byte_formatr*   	bytes_endarray_bytess         r   unpack_byte_arrayDecodeUtils.unpack_byte_array   sJ    & '778HI(/@0@*AB1E-	I6%%r   c                 V    [         R                  X5      u  p#UR                  S5      U4$ )ap  Parse a variable length utf-8 string from the bytes.

The bytes are expected to be in the following format:
    [ length ][0 ... length bytes]
where length is an unsigned integer represented in the smallest number
of bytes to hold the maximum length of the array and the following
bytes are a valid utf-8 string.

:type data: bytes
:param bytes: The bytes to parse from.

:type length_byte_size: int
:param length_byte_size: The byte size of the preceding integer that
represents the length of the array. Supported values are 1, 2, and 4.

:rtype: (str, int)
:returns: A tuple containing the (utf-8 string, bytes consumed).
zutf-8)rA   rp   decode)rH   rl   ro   consumeds       r   unpack_utf8_stringDecodeUtils.unpack_utf8_string   s2    ( !, = =!
 !!'*H44r   c                     U SS S4$ )zParse a 16-byte uuid from the bytes.

:type data: bytes
:param data: The bytes to parse from.

:rtype: (bytes, int)
:returns: A tuple containing the (uuid bytes, bytes consumed).
N   r
   rG   s    r   unpack_uuidDecodeUtils.unpack_uuid  s     CRy"}r   c                 B    [        [        R                  U 5      [        4$ )aM  Parse the prelude for an event stream message from the bytes.

The prelude for an event stream message has the following format:
    [total_length][header_length][prelude_crc]
where each field is an unsigned 32-bit integer.

:rtype: ((int, int, int), int)
:returns: A tuple of ((total_length, headers_length, prelude_crc),
consumed)
)r   rA   PRELUDE_BYTE_FORMAT_PRELUDE_LENGTHrG   s    r   unpack_preludeDecodeUtils.unpack_prelude  s     {66=OOr   r
   N)rD   )r   r   r   r   r   rO   UINT16_BYTE_FORMATrU   rY   r]   rb   rg   r|   rk   staticmethodrI   rL   rR   rV   rZ   r^   rc   rh   rp   ru   ry   r~   r   r
   r   r   rA   rA   L   s>       
 
 
 
 
 
 
 
 
 
       & &0 5 50 	 	 P Pr   rA   c                 @    [        X5      S-  nX:w  a  [        X5      eg )Nl    )r   r4   )rH   checksumcrccomputed_checksums       r   _validate_checksumr     s*     d(:5$x;; %r   c                   N    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	Sr
g)	MessagePreludei$  z2Represents the prelude of an event stream message.c                 (    Xl         X l        X0l        g N)total_lengthheaders_lengthr   )r   r   r   r   s       r   r   MessagePrelude.__init__'  s    (,r   c                 H    U R                   U R                  -
  [        -
  S-
  $ )zCalculates the total payload length.

The extra minus 4 bytes is for the message CRC.

:rtype: int
:returns: The total payload length.
rE   )r   r   r}   r   s    r   payload_lengthMessagePrelude.payload_length,  s%       4#6#66H1LLr   c                      U R                   S-
  $ )zCalculates the byte offset for the end of the message payload.

The extra minus 4 bytes is for the message CRC.

:rtype: int
:returns: The byte offset from the beginning of the event stream
message to the end of the payload.
rE   )r   r   s    r   payload_endMessagePrelude.payload_end7  s       1$$r   c                 (    [         U R                  -   $ )zCalculates the byte offset for the end of the message headers.

:rtype: int
:returns: The byte offset from the beginning of the event stream
message to the end of the headers.
)r}   r   r   s    r   headers_endMessagePrelude.headers_endC  s     !4!444r   )r   r   r   N)r   r   r   r   r   r   propertyr   r   r   r   r
   r   r   r   r   $  sH    <
 M M 	% 	% 5 5r   r   c                   (    \ rS rSrSrS rSS jrSrg)EventStreamMessageiN  z#Represents an event stream message.c                 4    Xl         X l        X0l        X@l        g r   )preludeheaderspayloadr   )r   r   r   r   r   s        r   r   EventStreamMessage.__init__Q  s    r   c                     U R                   R                  S5      nUS:X  d  US:X  a  SnUU R                   U R                  S.$ )Nz:message-typeerror	exceptioni  )status_coder   body)r   getr   )r   r   message_types      r   to_response_dict#EventStreamMessage.to_response_dictW  sE    ||''87"lk&AK&||LL
 	
r   )r   r   r   r   N)   )r   r   r   r   r   r   r   r   r
   r   r   r   r   N  s    -
r   r   c                   *   \ rS rSrSr\R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                  \R                  S.
rS rS rS rS rS rS	 rS
 rS rSrg)EventStreamHeaderParserib  zParses the event headers from an event stream message.

Expects all of the header data upfront and creates a dictionary of headers
to return. This object can be reused multiple times to parse the headers
from multiple event stream messages.
)
r   rC   rD      rE            rf   	   c                     S U l         g r   _datar   s    r   r    EventStreamHeaderParser.__init__  s	    
r   c                 .    Xl         U R                  5       $ )zParses the event stream headers from an event stream message.

:type data: bytes
:param data: The bytes that correspond to the headers section of an
event stream message.

:rtype: dict
:returns: A dictionary of header key, value pairs.
)r   _parse_headersr   rH   s     r   parseEventStreamHeaderParser.parse  s     
""$$r   c                     0 nU R                   (       a9  U R                  5       u  p#X!;   a  [        U5      eX1U'   U R                   (       a  M9  U$ r   )r   _parse_headerr   )r   r   namerQ   s       r   r   &EventStreamHeaderParser._parse_headers  sG    jj,,.KD%d++!DM	 jjj
 r   c                 H    U R                  5       nU R                  5       nX4$ r   )_parse_name_parse_value)r   r   rQ   s      r   r   %EventStreamHeaderParser._parse_header  s&    !!!#{r   c                 l    [         R                  U R                  S5      u  pU R                  U5        U$ )NrC   )rA   ru   r   _advance_data)r   r   rt   s      r   r   #EventStreamHeaderParser._parse_name  s-    $77

AF8$r   c                 j    [         R                  U R                  5      u  pU R                  U5        U$ r   )rA   rR   r   r   )r   typert   s      r   _parse_type#EventStreamHeaderParser._parse_type  s+    $11$**=8$r   c                     U R                  5       nU R                  U   nU" U R                  5      u  p4U R                  U5        U$ r   )r   _HEADER_TYPE_MAPr   r   )r   header_typevalue_unpackerrQ   rt   s        r   r   $EventStreamHeaderParser._parse_value  sC    &&(..{;(48$r   c                 ,    U R                   US  U l         g r   r   )r   rt   s     r   r   %EventStreamHeaderParser._advance_data  s    ZZ	*
r   r   N)r   r   r   r   r   rA   rI   rL   rZ   r^   rc   rh   rp   ru   ry   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   r   b  s     ""##""######(())##"").%


+r   r   c                   l    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rSrg)EventStreamBufferi  zStreaming based event stream buffer

A buffer class that wraps bytes from an event stream providing parsed
messages as they become available via an iterable interface.
c                 >    SU l         S U l        [        5       U l        g )Nr   )r   _preluder   _header_parserr   s    r   r   EventStreamBuffer.__init__  s    
57r   c                 .    U =R                   U-  sl         g)zoAdd data to the buffer.

:type data: bytes
:param data: The bytes to add to the buffer to be used when parsing
Nr   r   s     r   add_dataEventStreamBuffer.add_data  s     	

d
r   c                     UR                   [        :  a  [        UR                   5      eUR                  [        :  a  [        UR                  5      eg r   )r   r(   r$   r   r1   r.   )r   r   s     r   _validate_prelude#EventStreamBuffer._validate_prelude  sG    !!$77&w'='=>>!!$77&w'='=>> 8r   c                     U R                   S [         n[        R                  U5      u  p#[	        U6 n[        US [        S-
   UR                  5        U R                  U5        U$ NrE   )r   r}   rA   r~   r   r   r   r   )r   prelude_bytesraw_prelude_r   s        r   _parse_prelude EventStreamBuffer._parse_prelude  s]    

#3O4$33MB +.=)>?Q+>?Mw'r   c                     U R                   [        U R                  R                   nU R                  R                  U5      $ r   )r   r}   r   r   r   r   )r   header_bytess     r   r    EventStreamBuffer._parse_headers  s3    zz/DMM4M4MN""((66r   c                 d    U R                   nU R                  UR                  UR                   nU$ r   )r   r   r   r   )r   r   payload_bytess      r   _parse_payload EventStreamBuffer._parse_payload  s-    --

7#6#69L9LMr   c                     U R                   nU R                  UR                  UR                   n[        R                  U5      u  p4U$ r   )r   r   r   r   rA   rV   )r   r   	crc_bytesmessage_crcr   s        r   _parse_message_crc$EventStreamBuffer._parse_message_crc  s?    --JJw22W5I5IJ	$229=r   c                 Z    U R                   [        S-
  U R                  R                   nU$ r   )r   r}   r   r   )r   message_bytess     r   _parse_message_bytes&EventStreamBuffer._parse_message_bytes  s,    

a$--";";
 r   c                     U R                  5       nU R                  5       n[        X!U R                  R                  S9  U$ )N)r   )r   r   r   r   r   )r   r   r   s      r   _validate_message_crc'EventStreamBuffer._validate_message_crc  s8    --/113=4==;L;LMr   c                     U R                  5       nU R                  5       nU R                  5       n[        U R                  X#U5      nU R                  5         U$ r   )r   r   r   r   r   _prepare_for_next_message)r   r   r   r   r   s        r   _parse_message EventStreamBuffer._parse_message  sO    ((*%%'%%'$T]]GcJ&&(r   c                 b    U R                   U R                  R                  S  U l         S U l        g r   )r   r   r   r   s    r   r   +EventStreamBuffer._prepare_for_next_message  s&    ZZ : : <=
r   c                 "   [        U R                  5      [        :  a
  [        5       eU R                  c  U R                  5       U l        [        U R                  5      U R                  R                  :  a
  [        5       eU R                  5       $ )zProvides the next available message parsed from the stream

:rtype: EventStreamMessage
:returns: The next event stream message
)lenr   r}   StopIterationr   r   r   r   r   s    r   nextEventStreamBuffer.next  sg     tzz?_,/!==  //1DMtzz?T]]777/!""$$r   c                 "    U R                  5       $ r   )r   r   s    r   __next__EventStreamBuffer.__next__  s    yy{r   c                     U $ r   r
   r   s    r   __iter__EventStreamBuffer.__iter__  s    r   )r   r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r
   r   r   r   r     sM    8
?7

%"r   r   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)EventStreami  a<  Wrapper class for an event stream body.

This wraps the underlying streaming body, parsing it for individual events
and yielding them as they come available through the iterator interface.

The following example uses the S3 select API to get structured data out of
an object stored in S3 using an event stream.

**Example:**
::
    from botocore.session import Session

    s3 = Session().create_client('s3')
    response = s3.select_object_content(
        Bucket='bucketname',
        Key='keyname',
        ExpressionType='SQL',
        RequestProgress={'Enabled': True},
        Expression="SELECT * FROM S3Object s",
        InputSerialization={'CSV': {}},
        OutputSerialization={'CSV': {}},
    )
    # This is the event stream in the response
    event_stream = response['Payload']
    end_event_received = False
    with open('output', 'wb') as f:
        # Iterate over events in the event stream as they come
        for event in event_stream:
            # If we received a records event, write the data to a file
            if 'Records' in event:
                data = event['Records']['Payload']
                f.write(data)
            # If we received a progress event, print the details
            elif 'Progress' in event:
                print(event['Progress']['Details'])
            # End event indicates that the request finished successfully
            elif 'End' in event:
                print('Result is complete')
                end_event_received = True
    if not end_event_received:
        raise Exception("End event not received, request incomplete.")
c                 ^    Xl         X l        X@l        X0l        U R	                  5       U l        g r   )_raw_stream_output_shape_operation_name_parser_create_raw_event_generator_event_generator)r   
raw_streamoutput_shapeparseroperation_names        r   r   EventStream.__init__G  s*    %)- $ @ @ Br   c              #   n   #    U R                    H!  nU R                  U5      nU(       d  M  Uv   M#     g 7fr   )r  _parse_event)r   eventparsed_events      r   r  EventStream.__iter__N  s0     **E,,U3L|"" +s   &5	5c              #      #    [        5       nU R                  R                  5        H  nUR                  U5        U S h  vN   M      g  N	7fr   )r   r
  streamr   )r   event_stream_bufferchunks      r   r  'EventStream._create_raw_event_generatorT  sA     /1%%,,.E((/*** /*s   >A A

Ac                     UR                  5       nU R                  R                  X R                  5      nUS   S:X  a  U$ [	        X0R
                  5      e)Nr   r   )r   r  r   r  r   r  )r   r  response_dictparsed_responses       r   r  EventStream._parse_eventZ  sN    ..0,,,,]<N<NO'3."""?4H4HIIr   c                      [        U R                  5      nUR                  R                  S5      nUS:X  a  U$  [        5       e! [         a     [        5       ef = f)Nz:event-typezinitial-response)r   r  r   r   r   r<   )r   initial_event
event_types      r   get_initial_response EventStream.get_initial_responseb  si    	 !6!67M&..22=AJ//$$ 0 %&&  	$&&	s   7A 
AAc                 8    U R                   R                  5         g)z%Closes the underlying streaming body.N)r
  closer   s    r   r)  EventStream.closel  s     r   )r  r  r  r  r
  N)r   r   r   r   r   r   r  r  r  r&  r)  r   r
   r   r   r  r    s(    )VC#+J'!r   r  N)r   )r   binasciir   structr   botocore.exceptionsr   r}   r(   r1   	Exceptionr   r   r$   r.   r4   r<   rA   r   r   r   r   r   r  r
   r   r   <module>r/     s    #   0   " 	) 	"k ""; ""; ""{ "	"[ 	"MP MP`<'5 '5T
 
(Q+ Q+hb bJS! S!r   