o
    Q `!                     @   sR  d Z ddlmZ ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
 ddlmZmZ dd	lmZ dd
lmZ ejrmddlmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ej$dddZ%ej$dddZ&ej$ddddZ'dd Z(dd Z)G dd dee' ej*e' Z+G dd  d ee' ej*e' Z,dS )!a  Collection of useful `~fs.wrapfs.WrapFS` subclasses.

Here's an example that opens a filesystem then makes it *read only*::

    >>> from fs import open_fs
    >>> from fs.wrap import read_only
    >>> projects_fs = open_fs('~/projects')
    >>> read_only_projects_fs = read_only(projects_fs)
    >>> read_only_projects_fs.remove('__init__.py')
    Traceback (most recent call last):
      ...
    fs.errors.ResourceReadOnly: resource '__init__.py' is read only

    )print_function)unicode_literalsN   )WrapFS)abspathnormpathsplit)ResourceReadOnlyResourceNotFound)Info)check_writable)datetime)	AnyBinaryIO
CollectionDictIteratorIOOptionalTextTuple)FS)RawInfo)SubFS)Permissions_Wr   )bound_Tr   _FT)r   Z	covariantc                 C      t | S )zMake a read-only filesystem.

    Arguments:
        fs (FS): A filesystem instance.

    Returns:
        FS: A read only version of ``fs``

    )WrapReadOnlyZfs r"   )/usr/lib/python3/dist-packages/fs/wrap.py	read_only3   s   r$   c                 C   r   )a  Make a filesystem that caches directory information.

    Arguments:
        fs (FS): A filesystem instance.

    Returns:
        FS: A filesystem that caches results of `~FS.scandir`, `~FS.isdir`
        and other methods which read directory information.

    )WrapCachedDirr!   r"   r"   r#   cache_directoryA   s   r&   c                       sL   e Zd ZdZdZ fddZ		dddZddd	Zd
d Zdd Z	  Z
S )r%   a  Caches filesystem directory information.

    This filesystem caches directory information retrieved from a
    scandir call. This *may* speed up code that calls `~FS.isdir`,
    `~FS.isfile`, or `~FS.gettype` too frequently.

    Note:
        Using this wrap will prevent changes to directory information
        being visible to the filesystem object. Consequently it is best
        used only in a fairly limited scope where you don't expected
        anything on the filesystem to change.

    z
cached-dirc                    s   t t| | i | _d S N)superr%   __init___cache)selfZwrap_fs	__class__r"   r#   r)   a   s   
zWrapCachedDir.__init__Nc           	      C   sf   t t|}|t|pdf}|| jvr(| jj|||d}dd |D }|| j|< t| j|  }|S )Nr"   )
namespacespagec                 S   s   i | ]}|j |qS r"   )name).0infor"   r"   r#   
<dictcomp>q   s    z)WrapCachedDir.scandir.<locals>.<dictcomp>)r   r   	frozensetr*   _wrap_fsscandiritervalues)	r+   pathr.   r/   _path	cache_keyZ_scan_result_dirZgen_scandirr"   r"   r#   r6   f   s   

zWrapCachedDir.scandirc           	      C   s   t t|}|dkrtddddiS t|\}}|t|pdf}|| jvr-| j||d | j| }z|| }W |S  tyD   t|w )N/Zbasic T)r0   is_dirr"   )r.   )	r   r   r   r   r4   r*   r6   KeyErrorr
   )	r+   r9   r.   r:   Zdir_pathZresource_namer;   r<   r2   r"   r"   r#   getinfov   s   


zWrapCachedDir.getinfoc                 C   s   |  |jS r'   rA   r?   r+   r9   r"   r"   r#   isdir   s   zWrapCachedDir.isdirc                 C   s   |  |j S r'   rB   rC   r"   r"   r#   isfile   s   zWrapCachedDir.isfileNNr'   )__name__
__module____qualname____doc__	wrap_namer)   r6   rA   rD   rE   __classcell__r"   r"   r,   r#   r%   P   s    

r%   c                   @   s   e Zd ZdZdZdd Z			d-dd	Z		
d.ddZd/ddZd0ddZ	dd Z
dd Zdd Z			d-ddZd1ddZd/ddZd/dd Z		
d.d!d"Z						
d2d#d$Zd%d& Zd3d'd(Z			d4d)d*Zd+d, ZdS )5r    zMakes a Filesystem read-only.

    Any call that would would write data or modify the filesystem in any way
    will raise a `~fs.errors.ResourceReadOnly` exception.

    z	read-onlyc                 C      |    t|r'   checkr	   )r+   r9   datar"   r"   r#   appendbytes      zWrapReadOnly.appendbytesutf-8Nr>   c                 C   rM   r'   rN   )r+   r9   textencodingerrorsnewliner"   r"   r#   
appendtext      	zWrapReadOnly.appendtextFc                 C   rM   r'   rN   r+   r9   permissionsZrecreater"   r"   r#   makedir      zWrapReadOnly.makedirc                 C      |    t|r'   rN   r+   Zsrc_pathZdst_pathZ	overwriter"   r"   r#   move   rR   zWrapReadOnly.moverc                 K   s2   |    t|rt|| jj|f|dd|S )Nrb   )mode	buffering)rO   r   r	   r5   openbin)r+   r9   rc   rd   optionsr"   r"   r#   re      s   zWrapReadOnly.openbinc                 C   rM   r'   rN   rC   r"   r"   r#   remove   rR   zWrapReadOnly.removec                 C   rM   r'   rN   rC   r"   r"   r#   	removedir   rR   zWrapReadOnly.removedirc                 C   rM   r'   rN   )r+   r9   r2   r"   r"   r#   setinfo   rR   zWrapReadOnly.setinfoc                 C   rM   r'   rN   )r+   r9   contentsrU   rV   rW   r"   r"   r#   	writetext   rY   zWrapReadOnly.writetextc                 C   rM   r'   rN   )r+   r9   ZaccessedZmodifiedr"   r"   r#   settimes   rR   zWrapReadOnly.settimesc                 C   r^   r'   rN   r_   r"   r"   r#   copy   rR   zWrapReadOnly.copyc                 C   rM   r'   rN   )r+   r9   Zwiper"   r"   r#   create   rR   zWrapReadOnly.createc                 C   rM   r'   rN   rZ   r"   r"   r#   makedirs   r]   zWrapReadOnly.makedirsc           	   	   K   s:   |    t|rt|| jj|f||||||d|S )N)rc   rd   rU   rV   rW   line_buffering)rO   r   r	   r5   open)	r+   r9   rc   rd   rU   rV   rW   rp   rf   r"   r"   r#   rq      s   zWrapReadOnly.openc                 C   rM   r'   rN   )r+   r9   rj   r"   r"   r#   
writebytes  rR   zWrapReadOnly.writebytesc                 K   rM   r'   rN   )r+   r9   fileZ
chunk_sizerf   r"   r"   r#   upload  rR   zWrapReadOnly.uploadc                 C   rM   r'   rN   )r+   r9   rs   rU   rV   rW   r"   r"   r#   	writefile  rY   zWrapReadOnly.writefilec                 C   rM   r'   rN   rC   r"   r"   r#   touch(  rR   zWrapReadOnly.touch)rS   Nr>   )NF)F)ra   rb   rF   )ra   rb   NNr>   Fr'   )NNr>   )rG   rH   rI   rJ   rK   rQ   rX   r\   r`   re   rg   rh   ri   rk   rl   rm   rn   ro   rq   rr   rt   ru   rv   r"   r"   r"   r#   r       sP    	




	






	
r    )-rJ   Z
__future__r   r   typingZwrapfsr   r9   r   r   r   rV   r	   r
   r2   r   rc   r   ZTYPE_CHECKINGr   r   r   r   r   r   r   r   r   r   baser   r   Zsubfsr   r[   r   TypeVarr   r   r   r$   r&   ZGenericr%   r    r"   r"   r"   r#   <module>   s.    , C