o
    QDi-                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dl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 e	rLddlmZ dd	lmZ G d
d dZG dd dZdS )    )annotationsN)TYPE_CHECKINGAnyOptionalcast   )errorssettings)FrictionlessException)platform)types)Resourcec                   @  s   e Zd ZU dZdZded< 	 d<ddZd	d
 Zdd Ze	d=ddZ
e	d>ddZe	d?ddZe	d@ddZdd ZdAddZe	dBddZd?d d!Zd?d"d#ZdCd&d'ZdDd(d)ZdEd*d+ZdFd.d/Zd0d1 ZdGd5d6ZdHd7d8ZdId9d:Zd;S )JLoaderzRLoader representation

    Parameters:
        resource (Resource): resource

    Fboolremoteresourcer   c                 C  s   || _ d | _d | _d | _d S N)_Loader__resource_Loader__buffer_Loader__byte_stream_Loader__text_stream)selfr    r   S/var/www/Datamplify/venv/lib/python3.10/site-packages/frictionless/system/loader.py__init__)   s   
zLoader.__init__c                 C  s   | j r|   | S r   )closedopenr   r   r   r   	__enter__/   s   zLoader.__enter__c                 C  s   |    d S r   )close)r   typevalue	tracebackr   r   r   __exit__4      zLoader.__exit__returnc                 C  s   | j S )zD
        Returns:
            resource (Resource): resource
        )r   r   r   r   r   r   7      zLoader.resourcetypes.IBufferc                 C     | j du r	td| j S )z5
        Returns:
            Loader: buffer
        Nloader is not open)r   r
   r   r   r   r   buffer?   s   
zLoader.buffertypes.IByteStreamc                 C  r(   )zResource byte stream

        The stream is available after opening the loader

        Returns:
            io.ByteStream: resource byte stream
        Nr)   )r   r
   r   r   r   r   byte_streamI   s   
	zLoader.byte_streamtypes.ITextStreamc                 C  s$   | j rtd| js|  | _| jS )zResource text stream

        The stream is available after opening the loader

        Returns:
            io.TextStream: resource text stream
        r)   )r   r
   r   read_text_streamr   r   r   r   text_streamV   s
   	
zLoader.text_streamc                 C  s2   |    z|  | _| W S  ty   |     w )z!Open the loader as "io.open" does)r   read_byte_streamr   	Exceptionr   r   r   r   r   g   s   
zLoader.openNonec                 C  s   | j r| j   d| _ dS )z)Close the loader as "filelike.close" doesN)r   r   r   r   r   r   r   q   s   

zLoader.closec                 C  s
   | j du S )zSWhether the loader is closed

        Returns:
            bool: if closed
        N)r   r   r   r   r   r   w   s   
zLoader.closedc              
   C  s   z|   }| |}| |}| |}| | || _W |S  ttfy9 } ztj	t
|d}t||d}~w tjjtjjfyV } ztjt
|d}t|d}~w tym } ztjt
|d}t|d}~ww )z\Read bytes stream

        Returns:
            io.ByteStream: resource byte stream
        noteN)read_byte_stream_createread_byte_stream_processread_byte_stream_decompressread_byte_stream_bufferread_byte_stream_analyzer   LookupErrorUnicodeDecodeErrorr   EncodingErrorstrr
   r   zipfile
BadZipFilegzipBadGzipFileCompressionErrorIOErrorSchemeError)r   r,   r*   	exceptionerrorr   r   r   r0      s*   





zLoader.read_byte_streamc                 C     t  )z^Create bytes stream

        Returns:
            io.ByteStream: resource byte stream
        NotImplementedErrorr   r   r   r   r5      r&   zLoader.read_byte_stream_creater,   ByteStreamWithStatsHandlingc                 C  s   t || jdS )zProcess byte stream

        Parameters:
            byte_stream (io.ByteStream): resource byte stream

        Returns:
            io.ByteStream: resource byte stream
        )r   )rJ   r   r   r,   r   r   r   r6      s   zLoader.read_byte_stream_processc           	   	   C  s  | j js| j js
|S | j jdkr| jr(d| _t }t|| |d |}nd}|r4|	t
j}|s,|d tj|I}| j jpI| d }|sVtjdd}t|||}t }t|| |d W d   n1 suw   Y  |}|| j _W d   |S 1 sw   Y  |S | j jdkr| jsd}|r|	t
j}|s|d tj|}|S | j jd	kr| jsd}|r|	t
j}|s|d tj|}|S | j jd
kr| jsd}|r|	t
j}|s|d tj|}|S d| j j d}ttj|d)zDecompress byte stream

        Parameters:
            byte_stream (io.ByteStream): resource byte stream

        Returns:
            io.ByteStream: resource byte stream
        zipFr   Tzthe archive is emptyr3   Ngzbz2xzzcompression "z" is not supported)r   	multipartcompressionr   tempfileNamedTemporaryFileshutilcopyfileobjseekread1ioDEFAULT_BUFFER_SIZEr   r>   ZipFile	innerpathnamelistr   Errorr
   r   r@   rN   lzmarB   )	r   r,   targetbytesarchivenamerF   filer4   r   r   r   r7      sv   






z"Loader.read_byte_stream_decompressc                 C  s0   | | jjj}|d| jjj }|d |S )zBuffer byte stream

        Parameters:
            byte_stream (io.ByteStream): resource byte stream

        Returns:
            bytes: buffer
        Nr   )readr   detectorbuffer_sizerV   )r   r,   r*   r   r   r   r8     s   	
zLoader.read_byte_stream_bufferr*   r`   c                 C  s"   | j jj|| j dd| j _dS )zaDetect metadta using sample

        Parameters:
            buffer (bytes): byte buffer
        encoding)rg   N)r   re   detect_encodingget_definedrg   )r   r*   r   r   r   r9     s   zLoader.read_byte_stream_analyzec                 C  s*   | j jdkrdnd}tj| j| j j|dS )z[Read text stream

        Returns:
            io.TextStream: resource text stream
        csv N)newline)r   formatrX   TextIOWrapperr,   rg   )r   rl   r   r   r   r.     s   zLoader.read_text_streampathr=   r   c                 C  s   |  |}| |}|S )zWrite from a temporary file

        Parameters:
            path (str): path to a temporary file

        Returns:
            any: result of writing e.g. resulting path
        )write_byte_stream_createwrite_byte_stream_save)r   ro   r,   resultr   r   r   write_byte_stream)  s   
	
zLoader.write_byte_streamc                 C  s   t tj| t|d}|S )zCreate byte stream for writing

        Parameters:
            path (str): path to a temporary file

        Returns:
            io.ByteStream: byte stream
        rb)atexitregisterosremover   )r   ro   rc   r   r   r   rp   6  s   	
zLoader.write_byte_stream_createc                 C  rG   )zStore byte streamrH   rK   r   r   r   rq   C  s   zLoader.write_byte_stream_saveN)r   r   )r%   r   )r%   r'   )r%   r+   )r%   r-   )r%   r2   )r%   r   )r,   r+   r%   rJ   )r,   r+   r%   r+   )r,   r+   )r*   r`   )ro   r=   r%   r   )ro   r=   r%   r+   )r,   r+   r%   r   )__name__
__module____qualname____doc__r   __annotations__r   r   r#   propertyr   r*   r,   r/   r   r   r   r0   r5   r6   r7   r8   r9   r.   rs   rp   rq   r   r   r   r   r      s:   
 
	







S



r   c                   @  s@   e Zd ZdddZdd	d
Zdd Zedd ZddddZdS )rJ   r,   r+   r   r   c                C  s.   || _ || _td| _td| _d| _d S )Nmd5sha256r   ))_ByteStreamWithStatsHandling__byte_stream&_ByteStreamWithStatsHandling__resourcehashlibnew!_ByteStreamWithStatsHandling__md5$_ByteStreamWithStatsHandling__sha256#_ByteStreamWithStatsHandling__bytes)r   r,   r   r   r   r   r   R  s
   
z$ByteStreamWithStatsHandling.__init__rb   r=   c                 C  s   t | j|S r   )getattrr   )r   rb   r   r   r   __getattr__Y  r$   z'ByteStreamWithStatsHandling.__getattr__c                 c  s,    	 |  tj}|sd S |jddE d H  q)NT)keepends)rW   r	   rY   
splitlines)r   r`   r   r   r   __iter__\  s   z$ByteStreamWithStatsHandling.__iter__c                 C  s   | j jS r   )r   r   r   r   r   r   r   c  s   z"ByteStreamWithStatsHandling.closedsizeOptional[int]c                 C  s   |d u rdn|}t t| j|}| j| | j| |  jt|7  _|dks,|s<| j	 | j
j_| j	 | j
j_| j| j
j_|S )Nr   )r   r`   r   rW   r   updater   r   len	hexdigestr   statsr   r   )r   r   chunkr   r   r   rW   g  s   z!ByteStreamWithStatsHandling.read1N)r,   r+   r   r   )rb   r=   )r   )r   r   )	ry   rz   r{   r   r   r   r~   r   rW   r   r   r   r   rJ   Q  s    


rJ   )
__future__r   ru   r   rX   rw   rT   rR   typingr   r   r   r   rk   r   r	   rE   r
   r   r   r   r   r   rJ   r   r   r   r   <module>   s$      7