o
    uyi                     @   s   d dl Z d dlmZmZ d dl mZ d dlmZmZmZm	Z	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)DictListTupleUnion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   Y/home/ubuntu/maya3_transcribe/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_boolean0   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_int8   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>I   s    z*BinaryDecoder.read_ints.<locals>.<genexpr>)tupleranger   r   r   r   	read_intsG   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_dictK   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_floatR      &zBinaryDecoder.read_floatc                 C   r4   )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   )r6   r	   r   r
   r7   r   r   r   r   r   read_double[   r9   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   r0   d   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.
        )r0   decoder   r   r   r   r   	read_utf8i   s   zBinaryDecoder.read_utf8c                 C      |  d d S )Nr   r   r   r   r   r   skip_booleanq      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_intt   s   zBinaryDecoder.skip_intc                 C   r@   )Nr5   rA   r   r   r   r   
skip_floaty   rC   zBinaryDecoder.skip_floatc                 C   r@   )Nr:   rA   r   r   r   r   skip_double|   rC   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(   )rG   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.   r   r3   r6   r8   r;   r0   strr?   rB   rD   rE   rF   rG   rI   r   r   r   r   r   !   s,    		




r   c                   @   sj   e Zd ZU dZdZeed< deeef 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)
isinstancerO   ioBytesIOrS   )r   rT   r   r   r   __init__   s   

zStreamingBinaryDecoder.__init__c                 C   s
   | j  S )z#Return the current stream position.)rS   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 bytesr<   )
ValueErrorrS   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(   )rS   seekr   r   r   r   r   r      rH   zStreamingBinaryDecoder.skip)rJ   rK   rL   rM   	__slots__r   __annotations__r   rO   rX   rN   r   r   r   r   r   r   r   rR      s   
 rR   r$   r   c                 C   sD   zddl m} || W S  ty!   dd l}|d t|  Y S w )Nr   )CythonBinaryDecoderzGFalling back to pure Python Avro decoder, missing Cython implementation)pyiceberg.avro.decoder_fastre   ModuleNotFoundErrorwarningswarnrR   )r$   re   rh   r   r   r   new_decoder   s   

rj   )rV   abcr   r   r   typingr   r   r   r   r	   pyiceberg.avror
   r   pyiceberg.ior   pyiceberg.typedefr   r   rR   rO   rj   r   r   r   r   <module>   s   e+