o
    pf<                     @   s  d Z ddlmZmZ ddlZddlmZ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 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m Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ej)rddlm*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 ddl	m3Z3 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rNd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 sGw   Y  dS dS )/z(Manage the filesystem in a Tar archive.
    )print_functionunicode_literalsN)IOcast)OrderedDict   )errors)	url_quote)FS)	write_tar)ResourceType)IllegalBackReferenceNoURL)Info)
RawWrapper)open_fs)basenamefrombaseisbasenormpathpartsrelpath)Permissions)WrapFS)	AnyBinaryIO
CollectionDictListOptionalTextTupleUnion)TarInfo)RawInfo)SubFST	ReadTarFS)bound)TarFS
WriteTarFSr'   c                 C   s   |  |d S Nget_infomemberencoding r1   A/home/ertert/spirit/venv/lib/python3.10/site-packages/fs/tarfs.py_get_member_info5      r3   c                 C   s   |   S r+   r,   r.   r1   r1   r2   r3   ;   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)   a  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 or an FS instance to use to store
            data prior to tarring. Defaults to creating a new
            `~fs.tempfs.TempFS`.

    )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 )compressionr0   temp_fs)r0   )
isinstancesix	text_typebinary_typeospath
expandusergetattr	iteritems_compression_formatsendswithr*   r'   )	clsfilewriter<   r0   r=   filenamecomp
extensionsr1   r1   r2   __new__t   s    	
zTarFS.__new__c                 C      d S r+   r1   )selfrJ   rK   r<   r0   r=   r1   r1   r2   __init__   s   	zTarFS.__init__)FNr8   r9   )	__name__
__module____qualname____doc__rG   rO   typingTYPE_CHECKINGrR   r1   r1   r1   r2   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.Nr8   r9   c                    sH   || _ || _|| _|| _t|| _t| j | _t	t
| | j d S r+   )_filer<   r0   _temp_fs_urlr   _temp_fsdictgetmeta_metasuperr*   rR   )rQ   rJ   r<   r0   r=   	__class__r1   r2   rR      s   
zWriteTarFS.__init__c                 C   s   d}| | j| j| j| jS )Nz?WriteTarFS({!r}, compression={!r}, encoding={!r}, temp_fs={!r}))formatrY   r<   r0   rZ   )rQ   tr1   r1   r2   __repr__   s   zWriteTarFS.__repr__c                 C      d | jS )Nz<TarFS-write '{}'>rb   rY   rQ   r1   r1   r2   __str__   r4   zWriteTarFS.__str__c                 C   s
   | j |fS r+   r[   rQ   rC   r1   r1   r2   delegate_path   s   
zWriteTarFS.delegate_pathc                 C   s   | j S r+   ri   rg   r1   r1   r2   delegate_fs   s   zWriteTarFS.delegate_fsc                    s>   |   sz|   W | j  n| j  w tt|   d S r+   )isclosedr   r[   closer_   r*   rg   r`   r1   r2   rn      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<   r0   N)rm   r   r[   rY   r<   r0   )rQ   rJ   r<   r0   r1   r1   r2   r      s   
zWriteTarFS.write_tar)Nr8   r9   )NNN)rS   rT   rU   rV   rR   rd   rh   rk   rl   rn   r   __classcell__r1   r1   r`   r2   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)case_insensitivenetwork	read_onlysupports_renamethread_safeunicode_pathsvirtualr8   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)fileobjry   )r_   r'   rR   rY   r0   r>   r?   r@   rA   tarfileopen_tar_directory_cache)rQ   rJ   r0   r`   r1   r2   rR     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)r:   strip).0info)_decoder1   r2   	<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   )r:   r   _name)_directory_entriesr1   r2   	_list_tar  s   
z/ReadTarFS._directory_entries.<locals>._list_tar)r~   r   r}   r   )rQ   r   r1   )r   r   r2   r     s   

zReadTarFS._directory_entriesc                 C   re   )NzReadTarFS({!r})rf   rg   r1   r1   r2   rd   *  r4   zReadTarFS.__repr__c                 C   re   )Nz<TarFS '{}'>rf   rg   r1   r1   r2   rh   .  r4   zReadTarFS.__str__c                 C      | | jS r+   )encoder0   rQ   sr1   r1   r2   _encode4  r4   zReadTarFS._encodec                 C   r   r+   )decoder0   r   r1   r1   r2   r   8  r4   zReadTarFS._decodec                 C      |S r+   r1   r   r1   r1   r2   r   >     c                 C   r   r+   r1   r   r1   r1   r2   r   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 )Nr1   r;   T)r:   is_dirbasicdetailstypeF)sizer   modifiedaccessrx   )gidgrouppermissionsuidusertarc                    s,   i | ]}| d r|d dt | qS )isis_)
startswithreplacerE   )r   kr/   r1   r2   
<dictcomp>s  s    z%ReadTarFS.getinfo.<locals>.<dictcomp>) r   validatepathintr   	directoryr   KeyErrorisdirr   ResourceNotFoundr{   r#   DIRTYPEr   r   r   r:   r   type_mapmtimer   gnamer   ry   dumpr   unamer3   r0   updatedirr   )rQ   rC   
namespaces_pathraw_infoimplicitr1   r   r2   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   )r   r:   r   r1   r2   r     s    z"ReadTarFS.isdir.<locals>.<genexpr>)r   r   r   r   r   anyrj   r1   r   r2   r   |  s   zReadTarFS.isdirc                 C   s4   t | |}z| j|  W S  ty   Y dS w NF)r   r   r   isfiler   )rQ   rC   r   r1   r1   r2   r     s   zReadTarFS.isfilec                 C      |    t|r+   checkr   ResourceReadOnly)rQ   rC   r   r1   r1   r2   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   )r   nr   r1   r2   r     s    

z$ReadTarFS.listdir.<locals>.<genexpr>c                 s   s$    | ]}t |rt|d  V  qdS )r   N)r   r   )r   childr1   r1   r2   r     s   " )r   r   gettyper   r   r   DirectoryExpectedr   listr   fromkeys)rQ   rC   childrencontentr1   r   r2   listdir  s   

zReadTarFS.listdirc                 C   r   r+   r   )rQ   rC   r   recreater1   r1   r2   makedir  s   
zReadTarFS.makedirrw   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   rP   r+   r1   r1   r1   r1   r2   _flush  s   z!ReadTarFS.openbin.<locals>._flush)r   r   r   r   r   r   r?   
raise_fromr   r   FileExpectedr   r   r   r}   extractfilePY2flush)	rQ   rC   ry   	bufferingoptionsr   r/   rwr   r1   r1   r2   openbin  s   

zReadTarFS.openbinc                 C   r   r+   r   rj   r1   r1   r2   remove  r   zReadTarFS.removec                 C   r   r+   r   rj   r1   r1   r2   	removedir  r   zReadTarFS.removedirc                    s*   t t|   t| dr| j  d S d S )Nr}   )r_   r'   rn   hasattrr}   rg   r`   r1   r2   rn     s   
zReadTarFS.closec                 C   s   | j jS r+   )r}   closedrg   r1   r1   r2   rm     s   zReadTarFS.iscloseddownloadc                 C   s>   |dkrt | jtjrt| j}t|}d||S t||)Nfsztar://{}!/{})r>   rY   r?   string_typesr	   rb   r   )rQ   rC   purposequoted_filequoted_pathr1   r1   r2   geturl  s
   

zReadTarFS.geturl)r8   r+   r   )rw   r   )r   )1rS   rT   rU   rV   r^   r{   BLKTYPEr   block_special_fileCHRTYPE	characterr   r   FIFOTYPEfifoREGTYPErJ   AREGTYPESYMTYPEsymlinkCONTTYPELNKTYPE_typemapr   r   CreateFailed	catch_allrR   propertyr   rd   rh   r?   r   r   r   r   r   r   r   r   r   r   r   r   rn   rm   r   ro   r1   r1   r`   r2   r'      sZ    


6


__main__)renderz	tests.tarr   z/tests/ztests/ttt/settings.iniz	TarFS.tarT)rK   zfoo/barzfoo/bar/baz.txtzHello, World)JrV   
__future__r   r   rW   r   r   rB   r?   r{   collectionsr   r;   r   
_url_toolsr	   baser
   compressr   enumsr   r   r   r   r   iotoolsr   openerr   rC   r   r   r   r   r   r   r   r   wrapfsr   rX   r   r   r   r   r   r   r    r!   r"   r#   r$   subfsr%   TypeVarr&   __all__r   r3   r)   python_2_unicode_compatibler*   r'   rS   fs.treer   tar_fsprintr   readtextreprmakedirs	writetextr1   r1   r1   r2   <module>   sj     ,
]L 
r

$