
    g!                         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 d dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZ d Zda G d d	e      Z ed
g d       G d dej,                               Z G d de      Zy)    N)suppress)cached_propertywraps)parse_qs)AbstractFileSystem)"get_package_version_without_importinfer_storage_optionsmirror_fromtokenizec                 .     t                fd       }|S )Nc                      	  | i |S # t         $ rP}|j                  s |j                  ^}} t        |t              rd|v rt	        t
        j                  |      | d }~ww xY w)Nzdoes not exist)OSErrorargs
isinstancestrFileNotFoundErrorerrnoENOENT)r   kwargs	exceptionmessagefuncs       Q/var/www/openai/venv/lib/python3.12/site-packages/fsspec/implementations/arrow.pywrapperz wrap_exceptions.<locals>.wrapper   sg    
	((( 	>>&^^NGd'3',<,G'g>IM	s    	A$AAA$)r   )r   r   s   ` r   wrap_exceptionsr      s     
4[  N    c                       e Zd ZdZdZ fdZed        Zed        Z	e
d        ZddZd Zd	 Zd
 Zed        Zed        Zed        Zedd       Zedd       Zedd       Zedd       Zed        Zed        Zd fd	Z fdZ xZS )ArrowFSWrapperzwFSSpec-compatible wrapper of pyarrow.fs.FileSystem.

    Parameters
    ----------
    fs : pyarrow.fs.FileSystem

    /c                 H    t        d      a|| _        t        |   di | y )Npyarrow )r   PYARROW_VERSIONfssuper__init__)selfr$   r   	__class__s      r   r&   zArrowFSWrapper.__init__3   s$    <YG"6"r   c                 .    | j                   j                  S N)r$   	type_namer'   s    r   protocolzArrowFSWrapper.protocol9   s    ww   r   c                 p    dt        | j                  j                  | j                  j                        z   S )Nhdfs_)r   r$   hostportr,   s    r   fsidzArrowFSWrapper.fsid=   s$    $'',,===r   c                 R    t        |      }|d   }|j                  d      r|dd  }|S )Npathz//   )r	   
startswith)clsr4   opss      r   _strip_protocolzArrowFSWrapper._strip_protocolA   s1    #D)6{??4 8Dr   c                     | j                  |      }ddlm} | j                  j	                   ||            D cg c]  }| j                  |       }}|r|S |D cg c]  }|d   	 c}S c c}w c c}w )Nr   )FileSelectorname)r9   
pyarrow.fsr;   r$   get_file_info_make_entry)r'   r4   detailr   r;   entryentriess          r   lszArrowFSWrapper.lsJ   s    ##D)+ ..|D/AB
B U#B 	 
 N/67weE&Mw77
 8s   A,A1c                     | j                  |      }| j                  j                  |g      \  }| j                  |      S r*   )r9   r$   r>   r?   )r'   r4   r   infos       r   rE   zArrowFSWrapper.infoW   s;    ##D)&&v.%%r   c                 h    | j                  |      }	 | j                  |       y# t        $ r Y yw xY w)NTF)r9   rE   r   r'   r4   s     r   existszArrowFSWrapper.exists\   s;    ##D)	IIdO  ! 		s   % 	11c                    ddl m} |j                  |j                  u rd}n{|j                  |j                  u rd}n`|j                  |j
                  u rFt        t        j                  t        j                  t        j                        |j                        d}|j                  |j                  ||j                  dS )Nr   )FileType	directoryfileother)r<   sizetypemtime)r=   rJ   rO   	DirectoryFileNotFoundr   r   r   osstrerrorr4   rN   rP   )r'   rE   rJ   kinds       r   r?   zArrowFSWrapper._make_entrye   s    '99***DYY(--'DYY(+++#ELL"++ell2KTYYWWD IIIIZZ	
 	
r   c                 V   | j                  |      j                  d      }| j                  |      j                  d      }| j                  |d      5 }| dt        j                  d       }	 | j                  |d      5 }t        j                  ||       d d d        | j                  j                  ||       	 d d d        y # 1 sw Y   /xY w# t        $ rA t        t              5  | j                  j                  |       d d d         # 1 sw Y    xY ww xY w# 1 sw Y   y xY w)Nr   rbz.tmp.   wb)r9   rstrip_opensecrets	token_hexopenshutilcopyfileobjr$   moveBaseExceptionr   r   delete_file)r'   path1path2r   lstream	tmp_fnamerstreams          r   cp_filezArrowFSWrapper.cp_filex   s    $$U+2237$$U+2237ZZt$ 'w'8'8';&<=IYYy$/7&&w8 0Y. %$ 0/ ! /0GG''	2 1 1 %$sN   D/CC$CC	CD*D	DDDDD(c                     | j                  |      j                  d      }| j                  |      j                  d      }| j                  j                  ||       y )Nr   )r9   r[   r$   rb   )r'   re   rf   r   s       r   mvzArrowFSWrapper.mv   sJ    $$U+2237$$U+2237UE"r   c                 \    | j                  |      }| j                  j                  |       y r*   )r9   r$   rd   rG   s     r   rm_filezArrowFSWrapper.rm_file   s$    ##D)D!r   c                     | j                  |      j                  d      }| j                  |      r)|r| j                  j	                  |       y t        d      | j                  j                  |       y )Nr   z0Can't delete directories without recursive=False)r9   r[   isdirr$   
delete_dir
ValueErrorrd   )r'   r4   	recursivemaxdepths       r   rmzArrowFSWrapper.rm   s\    ##D)005::d""4( !STTGG%r   c                    |dk(  r0|r| j                   j                  }n]| j                   j                  }nF|dk(  r| j                   j                  }n*|dk(  r| j                   j                  }nt        d|      i }|dk7  s|s)t        t        j                  d      d         dk\  rd |d<    ||fi |}t        | ||||fi |S )	NrX   rZ   abz'unsupported mode for Arrow filesystem: .r      compression)
r$   open_input_fileopen_input_streamopen_output_streamopen_append_streamrr   intr#   split	ArrowFile)	r'   r4   mode
block_sizeseekabler   method_kwargsstreams	            r   r\   zArrowFSWrapper._open   s    4<0022T\WW//FT\WW//FFthOPP4<x?((-a01Q6)-&((vtT:HHHr   c                     | j                  |      }|r| j                  |d       y | j                  j                  |d       y )NT)exist_okFrs   )r9   makedirsr$   
create_dir)r'   r4   create_parentsr   s       r   mkdirzArrowFSWrapper.mkdir   s<    ##D)MM$M.GGtu5r   c                 `    | j                  |      }| j                  j                  |d       y )NTr   )r9   r$   r   )r'   r4   r   s      r   r   zArrowFSWrapper.makedirs   s)    ##D)440r   c                 \    | j                  |      }| j                  j                  |       y r*   )r9   r$   rq   rG   s     r   rmdirzArrowFSWrapper.rmdir   s$    ##D)4 r   c                 n    | j                  |      }| j                  j                  |      j                  S r*   )r9   r$   r>   rP   rG   s     r   modifiedzArrowFSWrapper.modified   s-    ##D)ww$$T*000r   c                 8    |dv|d<   t        |   |fd d d|S )N)Nr   r   )startend)r%   cat_file)r'   r4   r   r   r   r(   s        r   r   zArrowFSWrapper.cat_file   s.    ")3zwEDdEfEEr   c                 2    d|d<   t        |   ||fi | y )NFr   )r%   get_file)r'   rpathlpathr   r(   s       r   r   zArrowFSWrapper.get_file   s!    "z00r   )F)FN)rX   NT)T)NN)__name__
__module____qualname____doc__root_markerr&   propertyr-   r   r2   classmethodr9   rC   rE   rH   r?   r   rj   rl   rn   ru   r\   r   r   r   r   r   r   __classcell__r(   s   @r   r   r   (   s*    K# ! ! > >  8&

&   # #
 " " & & I I, 6 6 1 1 ! ! 1 1F1 1r   r   r   )	readseektellwritereadablewritablecloserN   r   c                        e Zd ZddZd Zd Zy)r   Nc                 f    || _         || _        || _        || _        |x| _        | _        || _        y r*   )r4   r   r$   r   	blocksizer   r   )r'   r$   r   r4   r   r   r   s          r   r&   zArrowFile.__init__   s3    		+55r   c                     | S r*   r"   r,   s    r   	__enter__zArrowFile.__enter__   s    r   c                 "    | j                         S r*   )r   )r'   r   s     r   __exit__zArrowFile.__exit__   s    zz|r   r*   )r   r   r   r&   r   r   r"   r   r   r   r      s    r   r   c                   D     e Zd ZdZdZ	 	 	 	 	 	 d fd	Zed        Z xZS )HadoopFileSystemz]A wrapper on top of the pyarrow.fs.HadoopFileSystem
    to connect it's interface with fsspechdfsc                 P    ddl m}  |||||||      }	t        
|   dd|	i| y)a;  

        Parameters
        ----------
        host: str
            Hostname, IP or "default" to try to read from Hadoop config
        port: int
            Port to connect on, or default from Hadoop config if 0
        user: str or None
            If given, connect as this username
        kerb_ticket: str or None
            If given, use this ticket for authentication
        replication: int
            set replication factor of file for write operations. default value is 3.
        extra_conf: None or dict
            Passed on to HadoopFileSystem
        r   )r   )r0   r1   userkerb_ticketreplication
extra_confr$   Nr"   )r=   r   r%   r&   )r'   r0   r1   r   r   r   r   r   r   r$   r(   s             r   r&   zHadoopFileSystem.__init__   s;    6 	0##!
 	)B)&)r   c                 H   t        |       }i }|j                  dd       r|d   |d<   |j                  dd       r|d   |d<   |j                  dd       r|d   |d<   |j                  dd       r4t        |d         }|j                  dd       rt        |d   d         |d<   |S )Nr0   usernamer   r1   	url_queryr   r   )r	   getr   r   )r4   r8   outqueriess       r   _get_kwargs_from_urlsz&HadoopFileSystem._get_kwargs_from_urls"  s    #D)7764 f+CK77:t$j/CK7764 f+CK77;%s;/0G{{=$/%()?)B%CM"
r   )defaultr   NN   N)	r   r   r   r   r-   r&   staticmethodr   r   r   s   @r   r   r      s=    - H %*N  r   r   )r   iorT   r]   r`   
contextlibr   	functoolsr   r   urllib.parser   fsspec.specr   fsspec.utilsr   r	   r
   r   r   r#   r   IOBaser   r   r"   r   r   <module>r      s~     	 	    , ! * $ j1' j1Z 
		 $;~ ;r   