o
    ig                     @   s   d dl Z d dlmZmZ d dl mZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ G dd	 d	eZG d
d deZdedefddZdS )    N)ABCabstractmethod)SEEK_CUR)cast)STRUCT_DOUBLESTRUCT_FLOAT)InputStream)UTF8c                   @   s  e Zd ZdZedefddZededefddZededdfd	d
Z	de
fddZdefddZdedeedf fddZdedeeef ddfddZdefddZdefddZdefddZdefddZd)ddZd)dd Zd)d!d"Zd)d#d$Zd)d%d&Zd)d'd(ZdS )*BinaryDecoder.Decodes bytes into Python physical primitives.returnc                 C      dS )zReturn the current position.N selfr   r   T/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/pyiceberg/avro/decoder.pytell        zBinaryDecoder.tellnc                 C   r   )Read n bytes.Nr   r   r   r   r   r   read$   r   zBinaryDecoder.readNc                 C   r   )zSkip n bytes.Nr   r   r   r   r   skip(   r   zBinaryDecoder.skipc                 C   s   t | ddkS )zRead a value from the stream as a boolean.

        A boolean is written as a single byte
        whose value is either 0 (false) or 1 (true).
           ordr   r   r   r   r   read_boolean,   s   zBinaryDecoder.read_booleanc                 C   sn   t | d}|d@ }d}|d@ dkr,t | d}||d@ |> O }|d7 }|d@ dks|d? |d@  A }|S )zkRead an int/long value.

        int/long values are written using variable-length, zigzag coding.
        r            r   r   )r   br   shiftdatumr   r   r   read_int4   s   zBinaryDecoder.read_int.c                    s   t  fddt|D S )zRead a list of integers.c                 3   s    | ]}   V  qd S N)r#   ).0_r   r   r   	<genexpr>E   s    z*BinaryDecoder.read_ints.<locals>.<genexpr>)tupleranger   r   r   r   	read_intsC   s   zBinaryDecoder.read_intsdestc                 C   s*   t |D ]}|  }|  }|||< qdS )zZRead a dictionary of integers for keys and bytes for values into a destination dictionary.N)r)   r#   
read_bytes)r   r   r+   r&   kvr   r   r   read_int_bytes_dictG   s
   
z!BinaryDecoder.read_int_bytes_dictc                 C   &   t ttt df t| dd S )zRead a value from the stream as a float.

        A float is written as 4 bytes.
        The float is converted into a 32-bit integer using a method equivalent to
        Java's floatToIntBits and then encoded in little-endian format.
        .   r   )floatr   r(   r   unpackr   r   r   r   r   
read_floatN      &zBinaryDecoder.read_floatc                 C   r0   )zRead a value from the stream as a double.

        A double is written as 8 bytes.
        The double is converted into a 64-bit integer using a method equivalent to
        Java's doubleToLongBits and then encoded in little-endian format.
        .   r   )r2   r   r(   r   r3   r   r   r   r   r   read_doubleW   r5   zBinaryDecoder.read_doublec                 C   s   |   }|dkr| |S dS )z@Bytes are encoded as a long followed by that many bytes of data.r       )r#   r   )r   	num_bytesr   r   r   r,   `   s   zBinaryDecoder.read_bytesc                 C   s   |   tS )zRead an utf-8 encoded string from the stream.

        A string is encoded as a long followed by
        that many bytes of UTF-8 encoded character data.
        )r,   decoder	   r   r   r   r   	read_utf8e   s   zBinaryDecoder.read_utf8c                 C      |  d d S )Nr   r   r   r   r   r   skip_booleanm      zBinaryDecoder.skip_booleanc                 C   s<   t | d}|d@ dkrt | d}|d@ dksd S d S )Nr   r   r   r   )r   r    r   r   r   skip_intp   s   zBinaryDecoder.skip_intc                 C   r<   )Nr1   r=   r   r   r   r   
skip_floatu   r?   zBinaryDecoder.skip_floatc                 C   r<   )Nr6   r=   r   r   r   r   skip_doublex   r?   zBinaryDecoder.skip_doublec                 C   s   |  |   d S r$   )r   r#   r   r   r   r   
skip_bytes{      zBinaryDecoder.skip_bytesc                 C   s   |    d S r$   )rC   r   r   r   r   	skip_utf8~   s   zBinaryDecoder.skip_utf8)r   N)__name__
__module____qualname____doc__r   intr   bytesr   r   boolr   r#   r(   r*   dictr/   r2   r4   r7   r,   strr;   r>   r@   rA   rB   rC   rE   r   r   r   r   r
      s,    		




r
   c                   @   sf   e Zd ZU dZdZeed< deeB ddfddZde	fdd	Z
d
e	defddZd
e	ddfddZdS )StreamingBinaryDecoderr   _input_streaminput_streamr   Nc                 C   s$   t |trt|| _dS || _dS )zDReader is a Python object on which we can call read, seek, and tell.N)
isinstancerK   ioBytesIOrP   )r   rQ   r   r   r   __init__   s   

zStreamingBinaryDecoder.__init__c                 C   s
   | j  S )z#Return the current stream position.)rP   r   r   r   r   r   r      s   
zStreamingBinaryDecoder.tellr   c                 C   s   |dk rt d| dg }|}|dkr=| j|}t|}||kr$|S |dkr0td| d|| ||8 }|dksd|S )r   r   z
Requested z* bytes to read, expected positive integer.z
EOF: read z bytesr8   )
ValueErrorrP   r   lenEOFErrorappendjoin)r   r   datan_remaining	data_readread_lenr   r   r   r      s   

zStreamingBinaryDecoder.readc                 C   s   | j |t d S r$   )rP   seekr   r   r   r   r   r      rD   zStreamingBinaryDecoder.skip)rF   rG   rH   rI   	__slots__r   __annotations__rK   rU   rJ   r   r   r   r   r   r   r   rO      s   
 rO   r    r   c                 C   sH   zddl m} || W S  ty#   dd l}|jddd t|  Y S w )Nr   )CythonBinaryDecoderzGFalling back to pure Python Avro decoder, missing Cython implementation   )
stacklevel)pyiceberg.avro.decoder_fastrb   ModuleNotFoundErrorwarningswarnrO   )r    rb   rg   r   r   r   new_decoder   s   
ri   )rS   abcr   r   r   typingr   pyiceberg.avror   r   pyiceberg.ior   pyiceberg.typedefr	   r
   rO   rK   ri   r   r   r   r   <module>   s   e+