HEX
Server: Apache
System: Linux vps35971 6.8.0-79-generic #79~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 15 16:54:53 UTC 2 x86_64
User: haitiopendh (6209306)
PHP: 8.1.32
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/botocore/__pycache__/eventstream.cpython-310.pyc
o

.&�a)P�@sdZddlmZddlmZddlmZdZdZdZ	Gdd	�d	e
�ZGd
d�de�ZGdd
�d
e�Z
Gdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Zd#dd�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGd d!�d!e�Zd"S)$zBinary Event Stream Decoding �)�crc32)�unpack)�EventStreamError�iic@seZdZdZdS)�ParserErrorz.Base binary flow encoding parsing exception.  N)�__name__�
__module__�__qualname__�__doc__�rr�6/usr/lib/python3/dist-packages/botocore/eventstream.pyrsrc� eZdZdZ�fdd�Z�ZS)�DuplicateHeaderz%Duplicate header found in the event. csd|}tt|��|�dS)NzDuplicate header present: "%s")�superr�__init__)�self�header�message��	__class__rrr szDuplicateHeader.__init__�rrr	r
r�
__classcell__rrrrr�rcr
)�InvalidHeadersLengthz+Headers length is longer than the maximum. c� d|tf}tt|��|�dS)Nz.Header length of %s exceeded the maximum of %s)�_MAX_HEADERS_LENGTHrrr�r�lengthrrrrr'��zInvalidHeadersLength.__init__rrrrrr%rrcr
)�InvalidPayloadLengthz+Payload length is longer than the maximum. cr)Nz/Payload length of %s exceeded the maximum of %s)�_MAX_PAYLOAD_LENGTHrrrrrrrr0rzInvalidPayloadLength.__init__rrrrrr.rrcr
)�ChecksumMismatchz9Calculated checksum did not match the expected checksum. cs d||f}tt|��|�dS)Nz5Checksum mismatch: expected 0x%08x, calculated 0x%08x)rr!r)r�expected�
calculatedrrrrr9rzChecksumMismatch.__init__rrrrrr!7rr!cr
)�NoInitialResponseErrorz�An 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.
    csd}tt|��|�dS)Nz0First event was not of the initial-response type)rr$r)rrrrrrFszNoInitialResponseError.__init__rrrrrr$@sr$c@s�eZdZdZdZdZdZdZdZdZ	dZ
d	Zeeed
�Ze
dd��Ze
d
d��Ze
dd��Ze
dd��Ze
dd��Ze
dd��Ze
dd��Ze
dd��Ze
d%dd��Ze
d%dd��Ze
d d!��Ze
d"d#��Zd$S)&�DecodeUtilsz�Unpacking 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)���cC�dS)aThis 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)
        )Trr��datarrr�unpack_truec�zDecodeUtils.unpack_truecCr))aThis 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)
        )Frrr*rrr�unpack_falsepr-zDecodeUtils.unpack_falsecC� ttj|dd��d}|dfS)z�Parse 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)
        Nr&r)rr%�UINT8_BYTE_FORMAT�r+�valuerrr�unpack_uint8}�
zDecodeUtils.unpack_uint8cCr/)z�Parse 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)
        Nr(r)rr%�UINT32_BYTE_FORMATr1rrr�
unpack_uint32�r4zDecodeUtils.unpack_uint32cCr/)z�Parse 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)
        Nr&r)rr%�INT8_BYTE_FORMATr1rrr�unpack_int8�r4zDecodeUtils.unpack_int8cCr/)aParse 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)
        Nr'r)rr%�INT16_BYTE_FORMATr1rrr�unpack_int16��zDecodeUtils.unpack_int16cCr/)aParse 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)
        Nr(r)rr%�INT32_BYTE_FORMATr1rrr�unpack_int32�r;zDecodeUtils.unpack_int32cCr/)aParse 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)rr%�INT64_BYTE_FORMATr1rrr�unpack_int64�r;zDecodeUtils.unpack_int64r'cCs<tj|}t||d|��d}||}|||�}||fS)a�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 preceeding 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)r%�UINT_BYTE_FORMATr)r+�length_byte_size�uint_byte_formatr�	bytes_end�array_bytesrrr�unpack_byte_array�s

zDecodeUtils.unpack_byte_arraycCst�||�\}}|�d�|fS)a�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 preceeding 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).
        zutf-8)r%rF�decode)r+rBrE�consumedrrr�unpack_utf8_string�s�zDecodeUtils.unpack_utf8_stringcCs|dd�dfS)z�Parse 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�rr*rrr�unpack_uuid�s
zDecodeUtils.unpack_uuidcCsttj|�tfS)a�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)
        )rr%�PRELUDE_BYTE_FORMAT�_PRELUDE_LENGTHr*rrr�unpack_preludeszDecodeUtils.unpack_preludeN)r')rrr	r
r0�UINT16_BYTE_FORMATr5r7r9r<r?rLrA�staticmethodr,r.r3r6r8r:r=r@rFrIrKrNrrrrr%KsL�









r%cCs$t||�d@}||krt||��dS)Nl��)rr!)r+�checksum�crc�computed_checksumrrr�_validate_checksums
�rTc@s<eZdZdZdd�Zedd��Zedd��Zedd	��Zd
S)�MessagePreludez3Represents the prelude of an event stream message. cCs||_||_||_dS�N)�total_length�headers_lengthrR)rrWrXrRrrrr$s
zMessagePrelude.__init__cCs|j|jtdS)z�Calculates the total payload length.

        The extra minus 4 bytes is for the message CRC.

        :rtype: int
        :returns: The total payload length.
        r()rWrXrM�rrrr�payload_length)s	zMessagePrelude.payload_lengthcCs
|jdS)a	Calculates 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.
        r()rWrYrrr�payload_end4s

zMessagePrelude.payload_endcCs
t|jS)z�Calculates 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.
        )rMrXrYrrr�headers_end@s
zMessagePrelude.headers_endN)	rrr	r
r�propertyrZr[r\rrrrrU"s


rUc@s"eZdZdZdd�Zddd�ZdS)	�EventStreamMessagez$Represents an event stream message. cCs||_||_||_||_dSrV)�prelude�headers�payloadrR)rr_r`rarRrrrrMs
zEventStreamMessage.__init__��cCs0|j�d�}|dks|dkrd}||j|jd�S)Nz
:message-type�error�	exceptioni�)�status_coder`�body)r`�getra)rre�message_typerrr�to_response_dictSs�z#EventStreamMessage.to_response_dictN)rb)rrr	r
rrirrrrr^Ksr^c@s~eZdZdZejejejejej	ej
ejejej
ej
d�
Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�ZdS)�EventStreamHeaderParsera Parses 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.
    )
rr&r'�r(���r>�	cCs
d|_dSrV��_datarYrrrrs
z EventStreamHeaderParser.__init__cCs||_|��S)aParses 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 dicionary of header key, value pairs.
        )rq�_parse_headers�rr+rrr�parse�s
zEventStreamHeaderParser.parsecCs8i}|jr|��\}}||vrt|��|||<|js|SrV)rq�
_parse_headerr)rr`�namer2rrrrr�s�z&EventStreamHeaderParser._parse_headerscCs|��}|��}||fSrV)�_parse_name�_parse_value)rrvr2rrrru�sz%EventStreamHeaderParser._parse_headercCs t�|jd�\}}|�|�|S)Nr&)r%rIrq�
_advance_data)rrvrHrrrrw�s
z#EventStreamHeaderParser._parse_namecCst�|j�\}}|�|�|SrV)r%r3rqry)r�typerHrrr�_parse_type�s
z#EventStreamHeaderParser._parse_typecCs.|��}|j|}||j�\}}|�|�|SrV)r{�_HEADER_TYPE_MAPrqry)r�header_type�value_unpackerr2rHrrrrx�s


z$EventStreamHeaderParser._parse_valuecCs|j|d�|_dSrVrp)rrHrrrry�sz%EventStreamHeaderParser._advance_dataN)rrr	r
r%r,r.r8r:r=r@rFrIrKr|rrtrrrurwr{rxryrrrrrj^s*�
	rjc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)�EventStreamBufferz�Streaming 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.
    cCsd|_d|_t�|_dS)N�)rq�_preluderj�_header_parserrYrrrr�szEventStreamBuffer.__init__cCs|j|7_dS)z�Add data to the buffer.

        :type data: bytes
        :param data: The bytes to add to the buffer to be used when parsing
        Nrprsrrr�add_data�szEventStreamBuffer.add_datacCs,|jtkr
t|j��|jtkrt|j��dSrV)rXrrrZr r)rr_rrr�_validate_prelude�s




�z#EventStreamBuffer._validate_preludecCsJ|jdt�}t�|�\}}t|�}|�|�t|dtd�|j�|S�Nr()rqrMr%rNrUr�rTrR)r�
prelude_bytes�raw_prelude�_r_rrr�_parse_prelude�s
z EventStreamBuffer._parse_preludecCs|jt|jj�}|j�|�SrV)rqrMr�r\r�rt)r�header_bytesrrrrr�sz EventStreamBuffer._parse_headerscCs|j}|j|j|j�}|SrV)r�rqr\r[)rr_�
payload_bytesrrr�_parse_payload�sz EventStreamBuffer._parse_payloadcCs*|j}|j|j|j�}t�|�\}}|SrV)r�rqr[rWr%r6)rr_�	crc_bytes�message_crcr�rrr�_parse_message_crc�sz$EventStreamBuffer._parse_message_crccCs|jtd|jj�}|Sr�)rqrMr�r[)r�
message_bytesrrr�_parse_message_bytes�sz&EventStreamBuffer._parse_message_bytescCs&|��}|��}t|||jjd�|S)N)rR)r�r�rTr�rR)rr�r�rrr�_validate_message_crc�sz'EventStreamBuffer._validate_message_crccCs4|��}|��}|��}t|j|||�}|��|SrV)r�rrr�r^r��_prepare_for_next_message)rrRr`rarrrr�_parse_message�sz EventStreamBuffer._parse_messagecCs|j|jjd�|_d|_dSrV)rqr�rWrYrrrr��s
z+EventStreamBuffer._prepare_for_next_messagecCsHt|j�tkr
t��|jdur|��|_t|j�|jjkr t��|��S)z�Provides the next available message parsed from the stream

        :rtype: EventStreamMessage
        :returns: The next event stream message
        N)�lenrqrM�
StopIterationr�r�rWr�rYrrr�next�s

zEventStreamBuffer.nextcCs|��SrV)r�rYrrr�__next__szEventStreamBuffer.__next__cCs|SrVrrYrrr�__iter__szEventStreamBuffer.__iter__N)rrr	r
rr�r�r�rrr�r�r�r�r�r�r�r�r�rrrrr�s 	rc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�EventStreama�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.")
    cCs&||_||_||_||_|��|_dSrV)�_raw_stream�
_output_shape�_operation_name�_parser�_create_raw_event_generator�_event_generator)r�
raw_stream�output_shape�parser�operation_namerrrr@s
zEventStream.__init__ccs&�|jD]}|�|�}|r|VqdSrV)r��_parse_event)r�event�parsed_eventrrrr�Gs�

��zEventStream.__iter__ccs6�t�}|j��D]}|�|�|D]}|Vqq	dSrV)rr��streamr�)r�event_stream_buffer�chunkr�rrrr�Ms�
��z'EventStream._create_raw_event_generatorcCs4|��}|j�||j�}|ddkr|St||j��)Nrerb)rir�rtr�rr�)rr��
response_dict�parsed_responserrrr�Ts
zEventStream._parse_eventcCsDzt|j�}|j�d�}|dkr|WSWt��ty!Yt��w)Nz:event-typezinitial-response)r�r�r`rgr�r$)r�
initial_event�
event_typerrr�get_initial_response\s
���z EventStream.get_initial_responsecCs|j��dS)z&Closes the underlying streaming body. N)r��closerYrrrr�fszEventStream.closeN)
rrr	r
rr�r�r�r�r�rrrrr�s*
r�N)r)r
�binasciir�structr�botocore.exceptionsrrMrr �	Exceptionrrrrr!r$�objectr%rTrUr^rjrr�rrrr�<module>s*			
P)Tc