o
    Q `+<                     @   s  d Z ddlm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	m
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 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 ddl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ej)rddlm*Z* ddlm+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddlm4Z4 ddl5m6Z6 ej7dddZ8g dZ9ej:rdd Z;ndd Z;G dd  d e(Z<ej=G d!d" d"e(Z>ej=G d#d deZ?e@d$krRdd%lAmBZB e<d&+ZCeDeCEd' eDeCEd( eDeCFd) eBeC eDeC eDeGeC W d   n	1 sw   Y  e<d*d+d,ZCeCHd- eCId.d/ eDeC eDeGeC W d   dS 1 sKw   Y  dS dS )0z(Manage the filesystem in a Tar archive.
    )print_function)unicode_literalsN)OrderedDict)castIO   )errors)FS)	write_tar)ResourceType)IllegalBackReferenceNoURL)Info)
RawWrapper)open_fs)Permissions)	url_quote)relpathbasenameisbasenormpathpartsfrombase)WrapFS)TarInfo)	AnyBinaryIO
CollectionDictListOptionalTextTupleUnion)RawInfo)SubFST	ReadTarFS)Zbound)TarFS
WriteTarFSr'   c                 C   s   |  |d S NZget_infomemberencoding r/   */usr/lib/python3/dist-packages/fs/tarfs.py_get_member_info4      r1   c                 C   s   |   S r*   r+   r,   r/   r/   r0   r1   ;   s   c                   @   sJ   e Zd ZdZddddZ					dd
dZejr#					dddZdS dS )r(   ap  Read and write tar files.

    There are two ways to open a TarFS for the use cases of reading
    a tar file, and creating a new one.

    If you open the TarFS with  ``write`` set to `False` (the
    default), then the filesystem will be a read only filesystem which
    maps to the files and directories within the tar file. Files are
    decompressed on the fly when you open them.

    Here's how you might extract and print a readme from a tar file::

        with TarFS('foo.tar.gz') as tar_fs:
            readme = tar_fs.readtext('readme.txt')

    If you open the TarFS with ``write`` set to `True`, then the TarFS
    will be a empty temporary filesystem. Any files / directories you
    create in the TarFS will be written in to a tar file when the TarFS
    is closed. The compression is set from the new file name but may be
    set manually with the ``compression`` argument.

    Here's how you might write a new tar file containing a readme.txt
    file::

        with TarFS('foo.tar.xz', write=True) as new_tar:
            new_tar.writetext(
                'readme.txt',
                'This tar file was written by PyFilesystem'
            )

    Arguments:
        file (str or io.IOBase): An OS filename, or an open file handle.
        write (bool): Set to `True` to write a new tar file, or
            use default (`False`) to read an existing tar file.
        compression (str, optional): Compression to use (one of the formats
            supported by `tarfile`: ``xz``, ``gz``, ``bz2``, or `None`).
        temp_fs (str): An FS URL for the temporary filesystem
            used to store data prior to tarring.

    )z.tar.xzz.txz)z.tar.bz2z.tbz)z.tar.gzz.tgz)xzbz2gzFNutf-8temp://__tartemp__c           	      C   s   t |tjtjfrtj|}|}nt|dd}|r4|d u r4d }t| j	D ]\}}|
|r3|} nq&|r>t||||dS t||dS )Nname )compressionr.   temp_fs)r.   )
isinstancesix	text_typebinary_typeospath
expandusergetattrZ	iteritems_compression_formatsendswithr)   r'   )	clsfilewriter:   r.   r;   filenamecomp
extensionsr/   r/   r0   __new__s   s    	
zTarFS.__new__c                 C      d S r*   r/   )selfrG   rH   r:   r.   r;   r/   r/   r0   __init__   s   	zTarFS.__init__)FNr6   r7   )	__name__
__module____qualname____doc__rD   rL   typingTYPE_CHECKINGrO   r/   r/   r/   r0   r(   B   s$    +

r(   c                       sd   e Zd ZdZ			d fdd	Zdd Zd	d
 Zdd Zdd Z fddZ				dddZ
  ZS )r)   zA writable tar file.
    Nr6   r7   c                    sH   || _ || _|| _|| _t|| _t| j | _t	t
| | j d S r*   )_filer:   r.   _temp_fs_urlr   _temp_fsdictZgetmeta_metasuperr)   rO   )rN   rG   r:   r.   r;   	__class__r/   r0   rO      s   
zWriteTarFS.__init__c                 C   s   d}| | j| j| j| jS )Nz?WriteTarFS({!r}, compression={!r}, encoding={!r}, temp_fs={!r}))formatrV   r:   r.   rW   )rN   tr/   r/   r0   __repr__   s   zWriteTarFS.__repr__c                 C      d | jS )Nz<TarFS-write '{}'>r^   rV   rN   r/   r/   r0   __str__   r2   zWriteTarFS.__str__c                 C   s
   | j |fS r*   rX   rN   rA   r/   r/   r0   delegate_path   s   
zWriteTarFS.delegate_pathc                 C   s   | j S r*   re   rc   r/   r/   r0   delegate_fs   s   zWriteTarFS.delegate_fsc                    s>   |   sz|   W | j  n| j  w tt|   d S r*   )isclosedr
   rX   closer[   r)   rc   r\   r/   r0   rj      s
   
zWriteTarFS.closec                 C   s4   |   st| j|p| j|p| j|p| jd dS dS )a)  Write tar to a file.

        Arguments:
            file (str or io.IOBase, optional): Destination file, may be
                a file name or an open file object.
            compression (str, optional): Compression to use (one of
                the constants defined in `tarfile` in the stdlib).
            encoding (str, optional): The character encoding to use
                (default uses the encoding defined in
                `~WriteTarFS.__init__`).

        Note:
            This is called automatically when the TarFS is closed.
        )r:   r.   N)ri   r
   rX   rV   r:   r.   )rN   rG   r:   r.   r/   r/   r0   r
      s   
zWriteTarFS.write_tar)Nr6   r7   )NNN)rP   rQ   rR   rS   rO   r`   rd   rg   rh   rj   r
   __classcell__r/   r/   r\   r0   r)      s    r)   c                       sB  e Zd ZdZddddddddZejejej	ej
ejejejejejejejejejejejejejeji	 ZZejjd0 fdd	Zedd	 Zd
d Zdd Ze j!r]dd Z"dd Z#ndd Z"dd Z#d1ddZ$dd Z%dd Z&dd Z'dd Z(		d2dd Z)d3d#d$Z*d%d& Z+d'd( Z, fd)d*Z-d+d, Z.d4d.d/Z/  Z0S )5r'   zA readable tar file.
    TF)Zcase_insensitiveZnetworkZ	read_onlyZsupports_renameZthread_safeZunicode_pathsZvirtualr6   c                    sX   t t|   || _|| _t|tjtjfrt	j
|dd| _nt	j
|dd| _d | _d S )Nrmode)Zfileobjrn   )r[   r'   rO   rV   r.   r<   r=   r>   r?   tarfileopen_tar_directory_cache)rN   rG   r.   r\   r/   r0   rO     s   
zReadTarFS.__init__c                    sB   | j du r| j  fdd| jD fdd}t| | _ | j S )zLazy directory cache.Nc                 3   s$    | ]} |j d |fV  qdS )/N)r8   strip).0info)_decoder/   r0   	<genexpr>  s    
z/ReadTarFS._directory_entries.<locals>.<genexpr>c               	   3   sB     D ]\} }zt | }W n	 ty   Y qw |r||fV  qd S r*   )r   r   )r8   rv   _name)_directory_entriesr/   r0   	_list_tar  s   
z/ReadTarFS._directory_entries.<locals>._list_tar)rr   rw   rq   r   )rN   r{   r/   )rw   rz   r0   rz     s   

zReadTarFS._directory_entriesc                 C   ra   )NzReadTarFS({!r})rb   rc   r/   r/   r0   r`   *  r2   zReadTarFS.__repr__c                 C   ra   )Nz<TarFS '{}'>rb   rc   r/   r/   r0   rd   .  r2   zReadTarFS.__str__c                 C      | | jS r*   )encoder.   rN   sr/   r/   r0   _encode4  r2   zReadTarFS._encodec                 C   r|   r*   )decoder.   r~   r/   r/   r0   rw   8  r2   zReadTarFS._decodec                 C      |S r*   r/   r~   r/   r/   r0   r   >     c                 C   r   r*   r/   r~   r/   r/   r0   rw   B  r   Nc                    sj  t | |}|p
d}i }|s'ddd|d< d|v r#dttji|d< t|S z	d}| j|  W n tyN   | |sAt	|d}t
| t
j _Y nw t|  j  d|d< d|v ry jt| j j d	|d< |sy j|d d
< d|v r|s j jt jd  j jd|d< d|v r|st | j|d< |d  fddt D  t|S )Nr/   r9   T)r8   is_dirZbasicZdetailstypeF)sizer   Zmodifiedaccessrm   )gidgrouppermissionsuidusertarc                    s,   i | ]}| d r|d dt | qS )isis_)
startswithreplacerC   )ru   kr-   r/   r0   
<dictcomp>s  s    z%ReadTarFS.getinfo.<locals>.<dictcomp>) r   validatepathintr   	directoryrz   KeyErrorisdirr   ResourceNotFoundro   r   DIRTYPEr   r   rw   r8   r   type_mapmtimer   gnamer   rn   dumpr   unamer1   r.   updatedirr   )rN   rA   Z
namespaces_pathZraw_infoZimplicitr/   r   r0   getinfoF  sT   +






zReadTarFS.getinfoc                    sJ   t | | z| j   W S  ty$   t fdd| jD  Y S w )Nc                 3   s    | ]}t  |V  qd S r*   )r   )ru   r8   r   r/   r0   rx     s    z"ReadTarFS.isdir.<locals>.<genexpr>)r   r   rz   r   r   anyrf   r/   r   r0   r   |  s   zReadTarFS.isdirc                 C   s4   t | |}z| j|  W S  ty   Y dS w NF)r   r   rz   isfiler   )rN   rA   r   r/   r/   r0   r     s   zReadTarFS.isfilec                 C      |    t|r*   checkr   ResourceReadOnly)rN   rA   rv   r/   r/   r0   setinfo     
zReadTarFS.setinfoc                    sX   t | | | |tjurt| fdd| jD }dd |D }tt	
|S )Nc                 3   s$    | ]}t  |rt |V  qd S r*   )r   r   )ru   nr   r/   r0   rx     s    

z$ReadTarFS.listdir.<locals>.<genexpr>c                 s   s$    | ]}t |rt|d  V  qdS )r   N)r   r   )ru   childr/   r/   r0   rx     s   " )r   r   Zgettyper   r   r   ZDirectoryExpectedrz   listr   fromkeys)rN   rA   childrenZcontentr/   r   r0   listdir  s   

zReadTarFS.listdirc                 C   r   r*   r   )rN   rA   r   Zrecreater/   r/   r0   makedir  s   
zReadTarFS.makedirrl   c           	      K   s   t | |}d|v sd|v sd|v rt|z| j| }W n ty1   tt|d  Y nw |	 s;t
|ttt| j|}tjrPdd }||_|S )Nw+ac                   S   rM   r*   r/   r/   r/   r/   r0   _flush  s   z!ReadTarFS.openbin.<locals>._flush)r   r   r   r   rz   r   r=   Z
raise_fromr   r   ZFileExpectedr   r   r   rq   ZextractfilePY2flush)	rN   rA   rn   	bufferingoptionsr   r-   rwr   r/   r/   r0   openbin  s   

zReadTarFS.openbinc                 C   r   r*   r   rf   r/   r/   r0   remove  r   zReadTarFS.removec                 C   r   r*   r   rf   r/   r/   r0   	removedir  r   zReadTarFS.removedirc                    s*   t t|   t| dr| j  d S d S )Nrq   )r[   r'   rj   hasattrrq   rc   r\   r/   r0   rj     s   
zReadTarFS.closec                 C   s   | j jS r*   )rq   closedrc   r/   r/   r0   ri     s   zReadTarFS.iscloseddownloadc                 C   s>   |dkrt | jtjrt| j}t|}d||S t||)NZfsztar://{}!/{})r<   rV   r=   Zstring_typesr   r^   r   )rN   rA   ZpurposeZquoted_fileZquoted_pathr/   r/   r0   geturl  s
   

zReadTarFS.geturl)r6   r*   r   )rl   r   )r   )1rP   rQ   rR   rS   rZ   ro   ZBLKTYPEr   Zblock_special_fileZCHRTYPE	characterr   r   ZFIFOTYPEZfifoZREGTYPErG   ZAREGTYPEZSYMTYPEsymlinkZCONTTYPEZLNKTYPEZ_typemapr   r   ZCreateFailedZ	catch_allrO   propertyrz   r`   rd   r=   r   r   rw   r   r   r   r   r   r   r   r   r   rj   ri   r   rk   r/   r/   r\   r0   r'      sZ    


6


__main__)renderz	tests.tarrs   z/tests/ztests/ttt/settings.iniz	TarFS.tarT)rH   zfoo/barzfoo/bar/baz.txtzHello, World)JrS   Z
__future__r   r   r@   ro   rT   collectionsr   r   r   r=   r9   r   baser	   compressr
   Zenumsr   r   r   rv   r   Ziotoolsr   Zopenerr   r   r   Z
_url_toolsr   rA   r   r   r   r   r   r   Zwrapfsr   rU   r   r   r   r   r   r   r    r!   r"   r#   r$   Zsubfsr%   TypeVarr&   __all__r   r1   r(   Zpython_2_unicode_compatibler)   r'   rP   Zfs.treer   Ztar_fsprintr   ZreadtextreprmakedirsZ	writetextr/   r/   r/   r0   <module>   sl     ,
\L 
s

$