
    +gd                         d dl Z d dlmZ d dlZd dlmZ d dlmZ  G d de          Z G d de          Z	 G d	 d
e          Z
 G d de          Z G d de          Z G d de          ZdS )    N)Optional)AbstractArchiveFileSystem)DEFAULT_BLOCK_SIZEc                        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
Ze fd            Zd ZdefdZ	 	 	 	 ddedefdZ xZS )BaseCompressedFileFileSystemzFRead contents of compressed file as a filesystem with one file inside. Nprotocolcompression	extensionfotarget_protocoltarget_optionsc                     t                      j        | fi | t          j        |fd|| j        ddd|pi                     di           d|pi | _        t          j        	                    | j        j        
                    d          d                   | _        d	| j        v r'| 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.
        rbFT)requote_redirect_url	trust_envclient_kwargs)moder	   r
   r   z::r   .N)super__init__fsspecopenr
   popfileospathbasenamesplitcompressed_namerindexuncompressed_name	dir_cache)selfr   r   r   kwargs	__class__s        @lib/python3.11/site-packages/datasets/filesystems/compression.pyr   z%BaseCompressedFileFileSystem.__init__   s     	(((((K
$((-!  "'R,,_bAA
 
 #
 
	  "w//	0D0DT0J0J10MNN d***  !C4#7#>#>s#C#C!CDD% 	
     c                 l    t                                          |                              d          S )N/)r   _strip_protocollstrip)clsr   r&   s     r'   r+   z,BaseCompressedFileFileSystem._strip_protocol9   s+     ww&&t,,33C888r(   c                     | j         Ei | j        j                            | j        j                  d| j        i}|d         |i| _         d S d S )Nname)r#   r   fsinfor   r"   )r$   fs     r'   	_get_dirsz&BaseCompressedFileFileSystem._get_dirs>   sR    >!U49<$$TY^44Ufd>TUUAi^DNNN "!r(   r   c                 X    | j                                                                         S N)r   r   read)r$   r   s     r'   catz BaseCompressedFileFileSystem.catC   s     y~~$$&&&r(   r   Tr   c                     |                      |          }|dk    r t          d| d| j        j         d          | j                                        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%   s          r'   _openz"BaseCompressedFileFileSystem._openF   sX     ##D))4<<mmmty~mmmnnny~~r(   )r   NN)r   NTN)__name__
__module____qualname____doc__root_markerr	   str__annotations__r
   r   r   dictr   classmethodr+   r3   r7   r=   __classcell__r&   s   @r'   r   r   	   s<        PPK c    KIs ei$ $$-5c]$S[\`Sa$ $ $ $ $ $L 9 9 9 9 [9, , ,
' ' ' ' '                     r(   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r>   r?   r@   rA   r	   r
   r    r(   r'   rJ   rJ   U   #        IIHKIIIr(   rJ   c                       e Zd ZdZdZdZdZdS )GzipFileSystemz@Read contents of GZIP file as a filesystem with one file inside.gzipz.gzNrL   rM   r(   r'   rP   rP   ]   s#        JJHKIIIr(   rP   c                       e Zd ZdZdZdZdZdS )Lz4FileSystemz?Read contents of LZ4 file as a filesystem with one file inside.lz4z.lz4NrL   rM   r(   r'   rS   rS   e   rN   r(   rS   c                       e Zd ZdZdZdZdZdS )XzFileSystemzFRead contents of .xz (LZMA) file as a filesystem with one file inside.xzz.xzNrL   rM   r(   r'   rV   rV   m   s#        PPHKIIIr(   rV   c                   f     e Zd ZdZdZdZdZdddefdedede	e         d	e	e
         d
ef
 fdZ xZS )ZstdFileSystemz
    Read contents of zstd file as a filesystem with one file inside.

    Note that reading in binary mode with fsspec isn't supported yet:
    https://github.com/indygreg/python-zstandard/issues/136
    zstdz.zstr   Nr   r   r   r   r:   c           	         	  t                      j        d|||||d| | j        j        	 G d d          	fd}|| j        _        d S )N)r   r   r   r   r:   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS ),ZstdFileSystem.__init__.<locals>.WrappedFilec                     || _         d S r5   )_file)r$   file_s     r'   r   z5ZstdFileSystem.__init__.<locals>.WrappedFile.__init__   s    "


r(   c                 8    | j                                          | S r5   )r_   	__enter__r$   s    r'   rb   z6ZstdFileSystem.__init__.<locals>.WrappedFile.__enter__   s    
$$&&&r(   c                 *     | j         j        |i | d S r5   )r_   __exit__)r$   argsr%   s      r'   re   z5ZstdFileSystem.__init__.<locals>.WrappedFile.__exit__   s"    #
#T4V44444r(   c                 *    t          | j                  S r5   )iterr_   rc   s    r'   __iter__z5ZstdFileSystem.__init__.<locals>.WrappedFile.__iter__       DJ'''r(   c                 *    t          | j                  S r5   )nextr_   rc   s    r'   __next__z5ZstdFileSystem.__init__.<locals>.WrappedFile.__next__   rj   r(   c                 ,    t          | j        |          S r5   )getattrr_   )r$   attrs     r'   __getattr__z8ZstdFileSystem.__init__.<locals>.WrappedFile.__getattr__   s    tz4000r(   N)	r>   r?   r@   r   rb   re   ri   rm   rq   rM   r(   r'   WrappedFiler]      sn        # # #  5 5 5( ( (( ( (1 1 1 1 1r(   rr   c                  &      | i |          S r5   rM   )rf   r%   rr   _enters     r'   fixed_enterz,ZstdFileSystem.__init__.<locals>.fixed_enter   s"    ;vvt6v66777r(   rM   )r   r   r   rb   )r$   r   r   r   r   r:   r%   ru   rr   rt   r&   s           @@r'   r   zZstdFileSystem.__init__   s     	 	
+)!	
 	
 	
 	
 	
 $	1 	1 	1 	1 	1 	1 	1 	1(	8 	8 	8 	8 	8 	8 *	r(   )r>   r?   r@   rA   r	   r
   r   r   rC   r   rE   intr   rG   rH   s   @r'   rY   rY   u   s          HKI
 )-)-,1* 1*1* 1* "#	1*
 !1* 1* 1* 1* 1* 1* 1* 1* 1* 1* 1*r(   rY   )r   typingr   r   fsspec.archiver   fsspec.utilsr   r   rJ   rP   rS   rV   rY   rM   r(   r'   <module>rz      sb   				        4 4 4 4 4 4 + + + + + +I  I  I  I  I #< I  I  I X    0       1       0       /   =* =* =* =* =*1 =* =* =* =* =*r(   