o
    im                     @  s   d dl mZ d dlZ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 d dlmZmZ d dlmZ d dlmZ d	ZG d
d deZG dd deZe ZG dd deZG dd dZG dd dZG dd dZdS )    )annotationsN)ABCabstractmethod)Callable)	InputFileInputStream
OutputFile)TableMetadataTableMetadataUtil)UTF8)Configgzipc                   @  s6   e Zd ZedddZeddd	ZedddZdS )
Compressorlocationstrreturnc                 C  s   |  drt S tS )Nz.gz.metadata.json)endswithGzipCompressorNOOP_COMPRESSORr    r   S/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/pyiceberg/serializers.pyget_compressor!   s   zCompressor.get_compressorinpr   c                 C     dS )zReturn a stream decompressor.

        Args:
            inp: The input stream that needs decompressing.

        Returns:
            The wrapped stream
        Nr   selfr   r   r   r   stream_decompressor%       zCompressor.stream_decompressorCallable[[bytes], bytes]c                 C  r   )zzReturn a function to compress bytes.

        Returns:
            A function that can be used to compress bytes.
        Nr   r   r   r   r   bytes_compressor0   r   zCompressor.bytes_compressorN)r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname__staticmethodr   r   r   r!   r   r   r   r   r       s    
r   c                   @      e Zd Zd
ddZdddZd	S )NoopCompressorr   r   r   c                 C  s   |S Nr   r   r   r   r   r   :   s   z"NoopCompressor.stream_decompressorr   c                 C  s   dd S )Nc                 S  s   | S r*   r   )br   r   r   <lambda>>   r   z1NoopCompressor.bytes_compressor.<locals>.<lambda>r   r    r   r   r   r!   =   s   zNoopCompressor.bytes_compressorNr"   r#   r$   r%   r&   r   r!   r   r   r   r   r)   9       
r)   c                   @  r(   )r   r   r   r   c                 C  s
   t |S r*   )r   openr   r   r   r   r   E   s   
z"GzipCompressor.stream_decompressorr   c                 C  s   t jS r*   )r   compressr    r   r   r   r!   H   s   zGzipCompressor.bytes_compressorNr"   r#   r-   r   r   r   r   r   D   r.   r   c                   @  s$   e Zd ZdZeeefdd
dZdS )FromByteStreamzKA collection of methods that deserialize dictionaries into Iceberg objects.byte_streamr   encodingr   compressionr   r   r	   c                 C  sN   | | } t|}|| }| }W d   n1 sw   Y  t|S )a  Instantiate a TableMetadata object from a byte stream.

        Args:
            byte_stream: A file-like byte stream object.
            encoding (default "utf-8"): The byte encoder to use for the reader.
            compression: Optional compression method
        N)r   codecs	getreaderreadr
   	parse_raw)r2   r3   r4   reader
json_bytesmetadatar   r   r   table_metadataO   s   


zFromByteStream.table_metadataN)r2   r   r3   r   r4   r   r   r	   )r$   r%   r&   __doc__r'   r   r   r<   r   r   r   r   r1   L   s
    r1   c                   @  s"   e Zd ZdZeefddd	Zd
S )FromInputFilezIA collection of methods that deserialize InputFiles into Iceberg objects.
input_filer   r3   r   r   r	   c                 C  sF   |   }tj||tj| jddW  d   S 1 sw   Y  dS )aK  Create a TableMetadata instance from an input file.

        Args:
            input_file (InputFile): A custom implementation of the iceberg.io.file.InputFile abstract base class.
            encoding (str): Encoding to use when loading bytestream.

        Returns:
            TableMetadata: A table metadata instance.

        r   )r2   r3   r4   N)r/   r1   r<   r   r   r   )r?   r3   input_streamr   r   r   r<   e   s
   
$zFromInputFile.table_metadataN)r?   r   r3   r   r   r	   )r$   r%   r&   r=   r'   r   r<   r   r   r   r   r>   b   s    r>   c                   @  s    e Zd ZdZeddddZdS )ToOutputFilez_A collection of methods that serialize Iceberg objects into files given an OutputFile instance.Fr;   r	   output_filer   	overwriteboolr   Nonec                 C  sv   |j |d+}t drdnd}| j|dt}t|j	 |}|
| W d   dS 1 s4w   Y  dS )a#  Write a TableMetadata instance to an output file.

        Args:
            output_file (OutputFile): A custom implementation of the iceberg.io.file.OutputFile abstract base class.
            overwrite (bool): Where to overwrite the file if it already exists. Defaults to `False`.
        )rC   zlegacy-current-snapshot-idFT)exclude_noneN)creater   get_boolmodel_dump_jsonencoder   r   r   r   r!   write)r;   rB   rC   output_streamrF   r:   r   r   r   r<   z   s   "zToOutputFile.table_metadataN)F)r;   r	   rB   r   rC   rD   r   rE   )r$   r%   r&   r=   r'   r<   r   r   r   r   rA   w   s    rA   )
__future__r   r5   r   abcr   r   collections.abcr   pyiceberg.ior   r   r   pyiceberg.table.metadatar	   r
   pyiceberg.typedefr   pyiceberg.utils.configr   GZIPr   r)   r   r   r1   r>   rA   r   r   r   r   <module>   s"   