a
    h(B                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlZd dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZmZ edZG dd	 d	eZd
d Zdd ZedddeedddZG dd dejZdS )    N)	lru_cache)AbstractFileSystem)compr)get_compression)
isfilelikestringify_pathzfsspec.localc                       s0  e Zd ZdZdZdZdZdB fdd	Zedd	 Z	dCd
dZ
dDddZdd ZdEddZdd Zdd Zdd Zdd Zdd ZdFddZdGdd ZdHed!d"d#Zd$d% Zd&d' Zed(d)d*Zd+d, ZdId-d.Zd/d0 ZdJd2d3ZdKd4d5Zd6d7 Zd8d9 Z e!d:d; Z"e!d<d= Z#d>d? Z$d@dA Z%  Z&S )LLocalFileSystema   Interface to files on local storage

    Parameters
    ----------
    auto_mkdir: bool
        Whether, when opening a file, the directory containing it should
        be created (if it doesn't already exist). This is assumed by pyarrow
        code.
    /)filelocalTFc                    s   t  jf i | || _d S N)super__init__
auto_mkdir)selfr   kwargs	__class__ J/var/www/auris/lib/python3.9/site-packages/fsspec/implementations/local.pyr   "   s    zLocalFileSystem.__init__c                 C   s   dS )Nr   r   r   r   r   r   fsid&   s    zLocalFileSystem.fsidc                 K   sF   |  |}| |rt||r0| j|dd ntj|fi | d S NTexist_ok)_strip_protocolexistsFileExistsErrormakedirsosmkdir)r   pathZcreate_parentsr   r   r   r   r    *   s    

zLocalFileSystem.mkdirc                 C   s   |  |}tj||d d S )Nr   )r   r   r   )r   r!   r   r   r   r   r   3   s    
zLocalFileSystem.makedirsc                 C   s   |  |}t| d S r   )r   r   rmdirr   r!   r   r   r   r"   7   s    
zLocalFileSystem.rmdirc           	   
   K   s   |  |}| |}g }|d dkrt|V}|D ]@}z(|rH| |n
|  |j}|| W q4 tyr   Y q40 q4W d    q1 s0    Y  n|r|gn|d g}|S )Ntype	directoryname)r   infor   scandirr!   appendFileNotFoundError)	r   r!   Zdetailr   Z	path_infoinfositfr'   r   r   r   ls;   s    

(zLocalFileSystem.lsc                 K   sH  t |tjr|jdd}| }|jddr2d}n|jddrDd}nd}|j}|rz|jdd}|j}W n ty~   d}Y n0 | 	|j
}nd| 	|}tj|dd}t|j}|rtj|dd}|j}t|jrd}nt|jrd}nd}t|d|j}|||||d	}	d
D ]}
t|d|
 |	|
< q|rDt||	d< |	S )NF)follow_symlinksr%   r
   otherTr   Zst_birthtime)r&   sizer$   createdislink)modeuidgidmtimeinoZnlinkZst_Zdestination)
isinstancer   DirEntrystat
is_symlinkis_diris_filest_sizeOSErrorr   r!   S_ISLNKst_modeS_ISDIRS_ISREGgetattrst_ctimereadlink)r   r!   r   outlinktr1   Zout2Zcreated_timeresultfieldr   r   r   r'   N   sN    


zLocalFileSystem.infoc                 K   s
   t |S r   )osplexists)r   r!   r   r   r   r   rN      s    zLocalFileSystem.lexistsc                 K   sl   |  |}|  |}| jr.| j| |dd | |rFt|| n"| |r`| j|dd nt	|d S r   )
r   r   r   _parentisfileshutilcopyfileisdirZmkdirsr*   )r   path1path2r   r   r   r   cp_file   s    



zLocalFileSystem.cp_filec                 C   s   |  |}tj|S r   )r   r   r!   rP   r#   r   r   r   rP      s    
zLocalFileSystem.isfilec                 C   s   |  |}tj|S r   )r   r   r!   rS   r#   r   r   r   rS      s    
zLocalFileSystem.isdirNc                 K   sX   t |r@t|d}t|| W d    qT1 s40    Y  n| j||fi |S d S )Nrb)r   openrQ   copyfileobjrV   )r   rT   rU   callbackr   r-   r   r   r   get_file   s    ,zLocalFileSystem.get_filec                 K   s   | j ||fi |S r   )rV   )r   rT   rU   rZ   r   r   r   r   put_file   s    zLocalFileSystem.put_file)	recursivec                 K   s$   |  |}|  |}t|| dS )zMove files/directories
        For the specific case of local, all ops on directories are recursive and
        the recursive= kwarg is ignored.
        N)r   rQ   move)r   rT   rU   r]   r   r   r   r   mv   s    

zLocalFileSystem.mvc                 K   s,   |  |}|  |}tj||fi | d S r   )r   r   rI   r   srcdstr   r   r   r   rI      s    

zLocalFileSystem.linkc                 K   s,   |  |}|  |}tj||fi | d S r   )r   r   symlinkr`   r   r   r   rc      s    

zLocalFileSystem.symlinkreturnc                 C   s   t j| |S r   )r   r!   r3   r   r#   r   r   r   r3      s    zLocalFileSystem.islinkc                 C   s   t | | d S r   )r   remover   r#   r   r   r   rm_file   s    zLocalFileSystem.rm_filec                 C   sn   t |ts|g}|D ]T}| |}| |r^|s8tdt|t krRtdt	
| qt| qd S )Nz+Cannot delete directory, set recursive=Truez'Cannot delete current working directory)r9   listr   rS   
ValueErrorrM   abspathr   getcwdrQ   rmtreerf   )r   r!   r]   Zmaxdepthpr   r   r   rm   s    


zLocalFileSystem.rmc                 C   s   |  |}d| S )Nfile://)r   )r   r&   r   r   r   unstrip_protocol   s    
z LocalFileSystem.unstrip_protocolrW   c                 K   sB   |  |}| jr,d|v r,| j| |dd t||fd| i|S )NwTr   fs)r   r   r   rO   LocalFileOpener)r   r!   r4   
block_sizer   r   r   r   _open   s    
zLocalFileSystem._openc                 K   s^   |  |}| jr$| j| |dd | |r<t|d  nt|d  |rZt	|d d S )NTr   ar   )
r   r   r   rO   r   r   utimerX   closetruncate)r   r!   ry   r   r   r   r   touch   s    

zLocalFileSystem.touchc                 C   s$   | j |d}tjj|d tjjdS )Nr!   r2   tzr'   datetimefromtimestamptimezoneutcr   r!   r'   r   r   r   r2      s    zLocalFileSystem.createdc                 C   s$   | j |d}tjj|d tjjdS )Nr{   r7   r|   r~   r   r   r   r   modified   s    zLocalFileSystem.modifiedc                 C   sh   |  |}tjdkr(|ddd p&dS |ddd }t|dkr`|dd dkr`|d d S |S d S )Nr	      r         ::/)r   r   seprsplitlen)clsr!   Zpath_r   r   r   rO      s    

zLocalFileSystem._parentc                 C   s@  t |}|dr |dd  }nF|dr8|dd  }n.|drP|dd  }n|drf|dd  }t|}tjd	kr*|d
d dkr|d d |dd   }}nj|d d dkr|d	d }dks|d	|d
  }dkr|d }}n|d | ||d   }}nd}|d	p | j}|| S |d	p:| jS d S )Nro      zfile:   zlocal://   zlocal:   r	   r   r   r   // )r   
startswithmake_path_posixr   r   findrstriproot_marker)r   r!   driveZindex1index2r   r   r   r      s0    



zLocalFileSystem._strip_protocolc                 C   s   dS NTr   r   r   r   r   _isfilestore   s    zLocalFileSystem._isfilestorec                 C   s   t |}t||S r   )r   r   chmod)r   r!   r4   r   r   r   r   &  s    zLocalFileSystem.chmod)F)T)F)F)N)N)T)FN)rW   N)T)'__name__
__module____qualname____doc__r   protocol
local_filer   propertyr   r    r   r"   r.   r'   rN   rV   rP   rS   r[   r\   boolr_   rI   rc   r3   rg   rn   rp   ru   rz   r2   r   classmethodrO   r   r   r   __classcell__r   r   r   r   r      sF   


	

3

	




&r   c                 C   s  t | tsRt | tttfr0t| dd | D S t| } t | tsRtd| dtj	dkr| 
drj| S | 
dr~t| S | 
dr| dd	 } n| d
krd} t  d|  S | dd dkr| dd dkr| dd	 } | dd dkrt| dkr| d d S | dd} | S | dd dkr<tt| S | 
dr`d| dd	 dd S | 
dr| dd} tt d  |  S | dd} | 
dr| dd	 } n| d
krd} tt  d|  S d	S )z-Make path generic and absolute for current OSc                 s   s   | ]}t |V  qd S r   )r   ).0rm   r   r   r   	<genexpr>/      z"make_path_posix.<locals>.<genexpr>zcould not convert z
 to stringr	   ~z./r   N.r   r   r   r   r   r   \)z\\r   r   )r   r	   )r9   strrh   settupler$   r   	TypeErrorr   r   r   rM   
expanduserrk   r   replacer   
splitdriver{   r   r   r   r   +  sH    






 
r   c                 C   s"   |  tjp tjduo |  tjS )zReturn True if the path ends with a path separator.

    A forward slash is always considered a path separator, even on Operating
    Systems that normally use a backslash.
    N)endswithr   r   altsepr{   r   r   r   trailing_sep^  s    	r   r   )maxsize  )maskre   c                 C   s   t | }t | |S )zGet the current umask.

    Follows https://stackoverflow.com/a/44130549 to get the umask.
    Temporarily sets the umask to the given value, and then resets it to the
    original value.
    )r   umask)r   valuer   r   r   	get_umaskj  s    

r   c                   @   s   e Zd Zd6ddZdd Zdd Zd	d
 Zdd Zdd Zdd Z	e
dddZe
dddZdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd7edd&d'Zed(d) Zd*d+ Zddd,d-Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZdS )8rs   TNc                 K   sJ   t d| || _|| _|| _d | _|| _t||| _t	j
| _|   d S )Nzopen file: %s)loggerdebugr!   r4   rr   r-   
autocommitr   compressionioDEFAULT_BUFFER_SIZE	blocksizeru   )r   r!   r4   r   rr   r   r   r   r   r   r   x  s    zLocalFileOpener.__init__c                 C   s   | j d u s| j jr| js"d| jvrXt| j| jd| _ | jrt| j }|| j | jd| _ n,t	 \}}t
| || _t|| jd| _ d| jvr| j dd| _| j d | j| j _d S )Nrq   r4   r   r   )r-   closedr   r4   rX   r!   r   r   tempfilemkstempr   rx   tempseekr1   )r   compressir&   r   r   r   ru     s    


zLocalFileOpener._openc                 C   s2   d| j vrt|   | j| | j|| S Nr)r4   ri   ru   r-   r   read)r   startendr   r   r   _fetch_range  s
    
zLocalFileOpener._fetch_rangec                 C   sH   d | _ |dd }| j| d|d v rDd | _ |   | j | d S )Nlocr   r4   )r-   pop__dict__updateru   r   )r   stater   r   r   r   __setstate__  s    zLocalFileOpener.__setstate__c                 C   sB   | j  }|d d| jv r.| j |d< n| jjs>td|S )Nr-   r   r   z+Cannot serialise open write-mode local file)r   copyr   r4   r-   tellr   ri   )r   dr   r   r   __getstate__  s    


zLocalFileOpener.__getstate__c              
   C   s   | j rtdzt| j| j W n6 tyX } ztj| jsD|W Y d }~n>d }~0 0 z d}t	| j|t
| @  W n ty   Y n0 d S )Nz0Can only commit if not already set to autocommitr   )r   RuntimeErrorrQ   r^   r   r!   PermissionErrorr   r   r   r   )r   er   r   r   r   commit  s    zLocalFileOpener.commitc                 C   s   | j rtdt| j d S )Nz#Cannot discard if set to autocommit)r   r   r   rf   r   r   r   r   r   discard  s    zLocalFileOpener.discardrd   c                 C   s   dS r   r   r   r   r   r   readable  s    zLocalFileOpener.readablec                 C   s
   d| j vS r   r   r   r   r   r   writable  s    zLocalFileOpener.writablec                 O   s   | j j|i |S r   )r-   r   r   argsr   r   r   r   r     s    zLocalFileOpener.readc                 O   s   | j j|i |S r   )r-   writer   r   r   r   r     s    zLocalFileOpener.writec                 O   s   | j j|i |S r   )r-   r   r   r   r   r   r     s    zLocalFileOpener.tellc                 O   s   | j j|i |S r   )r-   r   r   r   r   r   r     s    zLocalFileOpener.seekc                 O   s   | j j|i |S r   )r-   seekabler   r   r   r   r     s    zLocalFileOpener.seekablec                 O   s   | j j|i |S r   )r-   readliner   r   r   r   r     s    zLocalFileOpener.readlinec                 O   s   | j j|i |S r   )r-   	readlinesr   r   r   r   r     s    zLocalFileOpener.readlinesc                 C   s
   | j  S r   )r-   rx   r   r   r   r   rx     s    zLocalFileOpener.closec                 C   s   | j |S r   )r-   ry   )r   r1   r   r   r   ry     s    zLocalFileOpener.truncatec                 C   s   | j jS r   )r-   r   r   r   r   r   r     s    zLocalFileOpener.closedc                 C   s
   | j  S r   )rawfilenor   r   r   r   r     s    zLocalFileOpener.filenoc                 C   s   | j   d S r   )r-   flushr   r   r   r   r     s    zLocalFileOpener.flushc                 C   s
   | j  S r   )r-   __iter__r   r   r   r   r     s    zLocalFileOpener.__iter__c                 C   s   t | j|S r   )rE   r-   )r   itemr   r   r   __getattr__  s    zLocalFileOpener.__getattr__c                 C   s
   d| _ | S r   )
_incontextr   r   r   r   	__enter__  s    zLocalFileOpener.__enter__c                 C   s   d| _ | j||| d S )NF)r   r-   __exit__)r   exc_type	exc_value	tracebackr   r   r   r      s    zLocalFileOpener.__exit__)TNN)N)r   r   r   r   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rx   intry   r   r   r   r   r   r   r   r   r   r   r   r   rs   w  s6    
	

rs   )r   )r   r   loggingr   os.pathr!   rM   rQ   r;   r   	functoolsr   Zfsspecr   Zfsspec.compressionr   Zfsspec.corer   Zfsspec.utilsr   r   	getLoggerr   r   r   r   r   r   IOBasers   r   r   r   r   <module>   s*   
  3