ó U!¶\c@sddZddlmZddlmZddlmZdZd%Zdd&Z d e fd „ƒYZ d e fd „ƒYZ de fd„ƒYZ de fd„ƒYZde fd„ƒYZde fd„ƒYZdefd„ƒYZdd„Zdefd„ƒYZdefd„ƒYZdefd„ƒYZd efd!„ƒYZd"efd#„ƒYZd$S('sBinary Event Stream Decoding iÿÿÿÿ(tcrc32(tunpack(tEventStreamErrori i€iiit ParserErrorcBseZdZRS(s.Base binary flow encoding parsing exception. (t__name__t __module__t__doc__(((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyRstDuplicateHeadercBseZdZd„ZRS(s%Duplicate header found in the event. cCs$d|}tt|ƒj|ƒdS(NsDuplicate header present: "%s"(tsuperRt__init__(tselftheadertmessage((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR s (RRRR (((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyRstInvalidHeadersLengthcBseZdZd„ZRS(s+Headers length is longer than the maximum. cCs*d|tf}tt|ƒj|ƒdS(Ns.Header length of %s exceeded the maximum of %s(t_MAX_HEADERS_LENGTHRR R (R tlengthR ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR 's (RRRR (((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR %stInvalidPayloadLengthcBseZdZd„ZRS(s+Payload length is longer than the maximum. cCs*d|tf}tt|ƒj|ƒdS(Ns/Payload length of %s exceeded the maximum of %s(t_MAX_PAYLOAD_LENGTHRRR (R RR ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR 0s (RRRR (((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR.stChecksumMismatchcBseZdZd„ZRS(s9Calculated checksum did not match the expected checksum. cCs*d||f}tt|ƒj|ƒdS(Ns5Checksum mismatch: expected 0x%08x, calculated 0x%08x(RRR (R texpectedt calculatedR ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR 9s (RRRR (((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR7stNoInitialResponseErrorcBseZdZd„ZRS(sÊ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. cCs d}tt|ƒj|ƒdS(Ns0First event was not of the initial-response type(RRR (R R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR Fs(RRRR (((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR@st DecodeUtilscBsþeZdZdZdZdZdZdZdZdZ ied6ed 6ed 6Z e d „ƒZ e d „ƒZ e d „ƒZe d„ƒZe d„ƒZe d„ƒZe d„ƒZe d d„ƒZe d d„ƒZe d„ƒZe d„ƒZRS(sà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. s!Bs!Hs!Is!hs!is!qs!IIIiiicCs tdfS(s 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) i(tTrue(tdata((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt unpack_truebs cCs tdfS(sThis 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) i(tFalse(R((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt unpack_falseos cCs$ttj|d ƒd}|dfS(sí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) ii(RRtUINT8_BYTE_FORMAT(Rtvalue((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt unpack_uint8|s cCs$ttj|d ƒd}|dfS(sî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) ii(RRtUINT32_BYTE_FORMAT(RR((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt unpack_uint32‰s cCs$ttj|d ƒd}|dfS(sParse 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) ii(RRtINT16_BYTE_FORMAT(RR((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt unpack_int16–s cCs$ttj|d ƒd}|dfS(sParse 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) ii(RRtINT32_BYTE_FORMAT(RR((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt unpack_int32¤s cCs$ttj|d ƒd}|dfS(sParse 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) ii(RRtINT64_BYTE_FORMAT(RR((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt unpack_int64²s cCsEtj|}t||| ƒd}||}|||!}||fS(s¤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). i(RtUINT_BYTE_FORMATR(Rtlength_byte_sizetuint_byte_formatRt bytes_endt array_bytes((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pytunpack_byte_arrayÀs    cCs+tj||ƒ\}}|jdƒ|fS(sÙ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). sutf-8(RR,tdecode(RR(R+tconsumed((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pytunpack_utf8_stringÙscCs|d dfS(sÛ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). i((R((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt unpack_uuidñs cCsttj|ƒtfS(s…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) (RRtPRELUDE_BYTE_FORMATt_PRELUDE_LENGTH(R((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pytunpack_preludeýs (RRRRtUINT16_BYTE_FORMATRR!R#R%R1R't staticmethodRRRR R"R$R&R,R/R0R3(((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyRKs2      icCs5t||ƒd@}||kr1t||ƒ‚ndS(NIÿÿÿÿ(RR(Rtchecksumtcrctcomputed_checksum((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt_validate_checksum s tMessagePreludecBsDeZdZd„Zed„ƒZed„ƒZed„ƒZRS(s3Represents the prelude of an event stream message. cCs||_||_||_dS(N(t total_lengththeaders_lengthR7(R R;R<R7((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR s  cCs|j|jtdS(s§Calculates the total payload length. The extra minus 4 bytes is for the message CRC. :rtype: int :returns: The total payload length. i(R;R<R2(R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pytpayload_lengths cCs |jdS(s 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. i(R;(R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt payload_end&s cCs t|jS(sÐ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. (R2R<(R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt headers_end2s(RRRR tpropertyR=R>R?(((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR:s    tEventStreamMessagecBs#eZdZd„Zdd„ZRS(s$Represents an event stream message. cCs(||_||_||_||_dS(N(tpreludetheaderstpayloadR7(R RBRCRDR7((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR ?s   iÈcCsR|jjdƒ}|dks*|dkr3d}ni|d6|jd6|jd6S(Ns :message-typeterrort exceptionit status_codeRCtbody(RCtgetRD(R RGt message_type((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pytto_response_dictEs  (RRRR RK(((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyRA=s tEventStreamHeaderParsercBsÀeZdZi ejd6ejd6ejd6ejd6ejd6ej d6ej d6ej d6ej d 6ej d 6Z d „Zd „Zd „Zd„Zd„Zd„Zd„Zd„ZRS(s 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. iiiiiiiiii cCs d|_dS(N(tNonet_data(R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR qscCs||_|jƒS(sParses 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. (RNt_parse_headers(R R((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pytparsets cCsQi}xD|jrL|jƒ\}}||kr?t|ƒ‚n|||(R RBt payload_bytes((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt_parse_payloadÌs cCs8|j}|j|j|j!}tj|ƒ\}}|S(N(R]RNR>R;RR (R RBt crc_bytest message_crcRc((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt_parse_message_crcÑs cCs|jtd|jj!}|S(Ni(RNR2R]R>(R t message_bytes((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt_parse_message_bytes×scCs5|jƒ}|jƒ}t||d|jjƒ|S(NR7(RjRlR9R]R7(R RiRk((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt_validate_message_crcÜs  cCsJ|jƒ}|jƒ}|jƒ}t|j|||ƒ}|jƒ|S(N(RmRORgRAR]t_prepare_for_next_message(R R7RCRDR ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt_parse_messageâs     cCs#|j|jj|_d|_dS(N(RNR]R;RM(R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyRnêscCsst|jƒtkr!tƒ‚n|jdkrB|jƒ|_nt|jƒ|jjkritƒ‚n|jƒS(s—Provides the next available message parsed from the stream :rtype: EventStreamMessage :returns: The next event stream message N( tlenRNR2t StopIterationR]RMRdR;Ro(R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pytnextïs  cCs |jƒS(N(Rr(R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt__next__scCs|S(N((R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt__iter__s(RRRR R_R`RdRORgRjRlRmRoRnRrRsRt(((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR[¤s            t EventStreamcBsDeZdZd„Zd„Zd„Zd„Zd„Zd„ZRS(sÐ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.") cCs7||_||_||_||_|jƒ|_dS(N(t _raw_streamt _output_shapet_operation_namet_parsert_create_raw_event_generatort_event_generator(R t raw_streamt output_shapetparsertoperation_name((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR 2s     ccs5x.|jD]#}|j|ƒ}|r |Vq q WdS(N(R{t _parse_event(R teventt parsed_event((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyRt9sccsJtƒ}x:|jjƒD])}|j|ƒx|D] }|Vq3WqWdS(N(R[RvtstreamR_(R tevent_stream_buffertchunkR((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyRz?s    cCsN|jƒ}|jj||jƒ}|ddkr8|St||jƒ‚dS(NRGiÈ(RKRyRPRwRRx(R Rt response_dicttparsed_response((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR€Fs  cCsVy5t|jƒ}|jjdƒ}|dkr4|SWntk rHnXtƒ‚dS(Ns :event-typesinitial-response(RrR{RCRIRqR(R t initial_eventt event_type((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pytget_initial_responseNs  cCs|jjƒdS(s&Closes the underlying streaming body. N(Rvtclose(R ((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyR‹Xs( RRRR RtRzR€RŠR‹(((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyRus*     Nii(RtbinasciiRtstructRtbotocore.exceptionsRR2RRt ExceptionRRR RRRtobjectRR9R:RARLR[Ru(((s8/tmp/pip-install-usGedi/botocore/botocore/eventstream.pyt s&     Á )Tc