o
    ò.Ðiˆ  ã                   @   s˜   d dl Z d dlmZ d dlmZ d dlZd dlmZ G dd„ deƒZG dd„ deƒZ	G d	d
„ d
eƒZ
G dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N)Úpartial)ÚOptional)ÚAbstractArchiveFileSystemc                       s¦   e Zd ZU dZdZdZeed< dZeed< dZ	eed< 	ddede
e d	e
e f‡ fd
d„Ze‡ fdd„ƒZdd„ Zdefdd„Z				ddedefdd„Z‡  ZS )ÚBaseCompressedFileFileSystemzFRead contents of compressed file as a filesystem with one file inside.Ú NÚprotocolÚcompressionÚ	extensionÚfoÚtarget_protocolÚtarget_optionsc              
      s¶   t ƒ j| fi |¤Ž t|dƒr| ¡ n|| _ttj| jfd|| jdddœ|p'i  	di ¡¥dœ|p1i ¤Ž| _
tj | j d¡d	 ¡| _d
| jv rR| jd| j d
¡… n| j| _d| _dS )aÞ  
        The compressed file system can be instantiated from any compressed file.
        It reads the contents of compressed file as a filesystem with one file inside, as if it was an archive.

        The single file inside the filesystem is named after the compresssed file,
        without the compression extension at the end of the filename.

        Args:
            fo (:obj:``str``): Path to compressed file. Will fetch file using ``fsspec.open()``
            mode (:obj:``str``): Currently, only 'rb' accepted
            target_protocol(:obj:``str``, optional): To override the FS protocol inferred from a URL.
            target_options (:obj:``dict``, optional): Kwargs passed when instantiating the target FS.
        Ú
__fspath__ÚrbFT)Úrequote_redirect_urlÚ	trust_envÚclient_kwargs)Úmoder   r   r   z::r   Ú.N)ÚsuperÚ__init__Úhasattrr   r
   r   ÚfsspecÚopenr   ÚpopÚ_open_with_fsspecÚosÚpathÚbasenameÚsplitÚcompressed_nameÚrindexÚuncompressed_nameÚ	dir_cache)Úselfr
   r   r   Úkwargs©Ú	__class__© úT/home/ubuntu/.local/lib/python3.10/site-packages/datasets/filesystems/compression.pyr      s.   þþýúõ
ÿý
z%BaseCompressedFileFileSystem.__init__c                    s   t ƒ  |¡ d¡S )Nú/)r   Ú_strip_protocolÚlstrip)Úclsr   r%   r'   r(   r*   ;   s   z,BaseCompressedFileFileSystem._strip_protocolc                 C   s@   | j d u ri |  ¡ j | j¡¥d| ji¥}|d |i| _ d S d S )NÚname)r"   r   ÚfsÚinfor
   r!   )r#   Úfr'   r'   r(   Ú	_get_dirs@   s   
 þz&BaseCompressedFileFileSystem._get_dirsr   c                 C   s8   |   ¡  ¡ }| ¡ W  d   ƒ S 1 sw   Y  d S )N)r   r   Úread)r#   r   r0   r'   r'   r(   ÚcatE   s   $ÿz BaseCompressedFileFileSystem.catr   Tr   c                 K   s6   |   |¡}|dkrtd|› d| j› dƒ‚|  ¡  ¡ S )Nr   zTried to read with mode z	 on file z opened with mode 'rb')r*   Ú
ValueErrorr
   r   r   )r#   r   r   Ú
block_sizeÚ
autocommitÚcache_optionsr$   r'   r'   r(   Ú_openI   s   
	z"BaseCompressedFileFileSystem._open)r   NN)r   NTN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úroot_markerr   ÚstrÚ__annotations__r   r	   r   Údictr   Úclassmethodr*   r1   r3   r8   Ú__classcell__r'   r'   r%   r(   r   	   s8   
 
ÿÿÿÿÿ(úþýr   c                   @   ó   e Zd ZdZdZdZdZdS )ÚBz2FileSystemz?Read contents of BZ2 file as a filesystem with one file inside.Úbz2z.bz2N©r9   r:   r;   r<   r   r   r	   r'   r'   r'   r(   rD   X   ó
    rD   c                   @   rC   )ÚGzipFileSystemz@Read contents of GZIP file as a filesystem with one file inside.Úgzipz.gzNrF   r'   r'   r'   r(   rH   `   rG   rH   c                   @   rC   )ÚLz4FileSystemz?Read contents of LZ4 file as a filesystem with one file inside.Úlz4z.lz4NrF   r'   r'   r'   r(   rJ   h   rG   rJ   c                   @   rC   )ÚXzFileSystemzFRead contents of .xz (LZMA) file as a filesystem with one file inside.Úxzz.xzNrF   r'   r'   r'   r(   rL   p   rG   rL   c                   @   rC   )ÚZstdFileSystemzK
    Read contents of .zstd file as a filesystem with one file inside.
    Úzstdz.zstNrF   r'   r'   r'   r(   rN   x   s
    rN   )r   Ú	functoolsr   Útypingr   r   Úfsspec.archiver   r   rD   rH   rJ   rL   rN   r'   r'   r'   r(   Ú<module>   s    O