a
    ij6"                     @   s   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d ZdaG d	d
 d
eZedg dG dd dejZG dd deZdS )    N)suppress)cached_propertywraps)parse_qs)AbstractFileSystem)"get_package_version_without_importinfer_storage_optionsmirror_fromtokenizec                    s   t   fdd}|S )Nc               
      sp   z | i |W S  t yj } zB|js( |j^}} t|trTd|v rTttj||n W Y d }~n
d }~0 0 d S )Nzdoes not exist)OSErrorargs
isinstancestrFileNotFoundErrorerrnoENOENT)r   kwargs	exceptionmessagefunc Z/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/fsspec/implementations/arrow.pywrapper   s    
z wrap_exceptions.<locals>.wrapper)r   )r   r   r   r   r   wrap_exceptions   s    r   c                       s   e Zd ZdZdZ fddZedd Zedd Z	e
d	d
 Zd-ddZdd Zdd Zdd Zedd Zedd Zedd Zed.ddZed/dd Zed0d!d"Zed1d#d$Zed%d& Zed'd( Zd2 fd)d*	Z fd+d,Z  ZS )3ArrowFSWrapperzwFSSpec-compatible wrapper of pyarrow.fs.FileSystem.

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

    /c                    s$   t da|| _t jf i | d S )NZpyarrow)r   PYARROW_VERSIONfssuper__init__)selfr   r   	__class__r   r   r    3   s    zArrowFSWrapper.__init__c                 C   s   | j jS N)r   	type_namer!   r   r   r   protocol9   s    zArrowFSWrapper.protocolc                 C   s   dt | jj| jj S )NZhdfs_)r
   r   hostportr&   r   r   r   fsid=   s    zArrowFSWrapper.fsidc                 C   s*   t |}|d }|dr&|dd  }|S )Npathz//   )r   
startswith)clsr+   opsr   r   r   _strip_protocolA   s
    
zArrowFSWrapper._strip_protocolFc              	      s~     |}ddlm} z" fdd j||D }W n* ttfyb    j|fi |g}Y n0 |rl|S dd |D S d S )Nr   )FileSelectorc                    s   g | ]}  |qS r   )_make_entry.0entryr&   r   r   
<listcomp>O   s   z%ArrowFSWrapper.ls.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )namer   r3   r   r   r   r6   X       )r0   
pyarrow.fsr1   r   get_file_infor   NotADirectoryErrorinfo)r!   r+   detailr   r1   entriesr   r&   r   lsJ   s    


zArrowFSWrapper.lsc                 K   s$   |  |}| j|g\}| |S r$   )r0   r   r:   r2   )r!   r+   r   r<   r   r   r   r<   Z   s    
zArrowFSWrapper.infoc                 C   s6   |  |}z| | W n ty,   Y dS 0 dS d S )NFT)r0   r<   r   r!   r+   r   r   r   exists_   s    
zArrowFSWrapper.existsc                 C   sn   ddl m} |j|ju rd}n<|j|ju r0d}n*|j|ju rVttjt	
tj|jnd}|j|j||jdS )Nr   )FileType	directoryfileother)r7   sizetypemtime)r9   rB   rG   Z	DirectoryFileZNotFoundr   r   r   osstrerrorr+   rF   rH   )r!   r<   rB   kindr   r   r   r2   h   s    zArrowFSWrapper._make_entryc                 K   s   |  |d}|  |d}| |d}| dtd }zJ| |d}t|| W d    n1 sr0    Y  | j	|| W nH t
y   tt | j| W d    n1 s0    Y   Y n0 W d    n1 s0    Y  d S )Nr   rbz.tmp.   wb)r0   rstrip_opensecretsZ	token_hexopenshutilcopyfileobjr   moveBaseExceptionr   r   delete_file)r!   path1path2r   ZlstreamZ	tmp_fnameZrstreamr   r   r   cp_file{   s    *
*zArrowFSWrapper.cp_filec                 K   s2   |  |d}|  |d}| j|| d S )Nr   )r0   rP   r   rV   )r!   rY   rZ   r   r   r   r   mv   s    zArrowFSWrapper.mvc                 C   s   |  |}| j| d S r$   )r0   r   rX   r@   r   r   r   rm_file   s    
zArrowFSWrapper.rm_fileNc                 C   sF   |  |d}| |r6|r,| j| qBtdn| j| d S )Nr   z0Can't delete directories without recursive=False)r0   rP   isdirr   
delete_dir
ValueErrorrX   )r!   r+   	recursiveZmaxdepthr   r   r   rm   s    

zArrowFSWrapper.rmrM   Tc           	      K   s   |dkr |r| j j}qR| j j}n2|dkr2| j j}n |dkrD| j j}ntd|i }|dksb|sttdd dkrd |d< ||fi |}t	| ||||fi |S )	NrM   rO   abz'unsupported mode for Arrow filesystem: .r      compression)
r   Zopen_input_fileZopen_input_streamZopen_output_streamZopen_append_streamr`   intr   split	ArrowFile)	r!   r+   mode
block_sizeseekabler   method_kwargsstreamr   r   r   rQ      s    



zArrowFSWrapper._openc                 K   s2   |  |}|r| j|dd n| jj|dd d S )NT)exist_okFra   )r0   makedirsr   
create_dir)r!   r+   Zcreate_parentsr   r   r   r   mkdir   s    
zArrowFSWrapper.mkdirc                 C   s   |  |}| jj|dd d S )NTrq   )r0   r   rs   )r!   r+   rp   r   r   r   rr      s    
zArrowFSWrapper.makedirsc                 C   s   |  |}| j| d S r$   )r0   r   r_   r@   r   r   r   rmdir   s    
zArrowFSWrapper.rmdirc                 C   s   |  |}| j|jS r$   )r0   r   r:   rH   r@   r   r   r   modified   s    
zArrowFSWrapper.modifiedc                    s&   |dv|d< t  j|fd d d|S )N)Nr   rl   )startend)r   cat_file)r!   r+   rw   rx   r   r"   r   r   ry      s    zArrowFSWrapper.cat_filec                    s"   d|d< t  j||fi | d S )NFrl   )r   get_file)r!   ZrpathZlpathr   r"   r   r   rz      s    zArrowFSWrapper.get_file)F)FN)rM   NT)T)F)NN)__name__
__module____qualname____doc__Zroot_markerr    propertyr'   r   r*   classmethodr0   r?   r<   rA   r2   r   r[   r\   r]   rb   rQ   rt   rr   ru   rv   ry   rz   __classcell__r   r   r"   r   r   (   sB   



	





r   ro   )	readseektellwritereadablewritablecloserF   rl   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	ri   Nc                 K   s.   || _ || _|| _|| _| | _| _|| _d S r$   )r+   rj   r   ro   	blocksizerk   r   )r!   r   ro   r+   rj   rk   r   r   r   r   r       s    zArrowFile.__init__c                 C   s   | S r$   r   r&   r   r   r   	__enter__   s    zArrowFile.__enter__c                 G   s   |   S r$   )r   )r!   r   r   r   r   __exit__   s    zArrowFile.__exit__)N)r{   r|   r}   r    r   r   r   r   r   r   ri      s   

ri   c                       s2   e Zd ZdZdZd fdd	Zed	d
 Z  ZS )HadoopFileSystemz]A wrapper on top of the pyarrow.fs.HadoopFileSystem
    to connect it's interface with fsspecZhdfsdefaultr   N   c           
         s:   ddl m} |||||||d}	t jf d|	i| dS )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   )r(   r)   userkerb_ticketreplication
extra_confr   N)r9   r   r   r    )
r!   r(   r)   r   r   r   r   r   r   r   r"   r   r   r       s    zHadoopFileSystem.__init__c                 C   s   t | }i }|dd r$|d |d< |dd r<|d |d< |dd rT|d |d< |dd rt|d }|dd rt|d d |d< |S )Nr(   usernamer   r)   Z	url_queryr   r   )r   getr   rg   )r+   r/   outZqueriesr   r   r   _get_kwargs_from_urls%  s    z&HadoopFileSystem._get_kwargs_from_urls)r   r   NNr   N)	r{   r|   r}   r~   r'   r    staticmethodr   r   r   r   r"   r   r      s         'r   )r   iorJ   rR   rT   
contextlibr   	functoolsr   r   urllib.parser   Zfsspec.specr   Zfsspec.utilsr   r   r	   r
   r   r   r   IOBaseri   r   r   r   r   r   <module>   s&    1