o
    e$                     @   s   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ZG dd deZ	G dd deZ
dd
edede
fddZ			d dededededef
ddZd
edefddZd
ededeeeef fddZdedefddZdS )!    N)Callable)	Generator)Listc                   @   s>   e Zd ZdZdddZedefddZedefdd	Z	d
S )XarInfozWInformational class which holds the details about an archive member given by a XarFile.tarc                 C   s   || _ || _d S N)_infoarctype)selfZ_xarinfor	    r   TD:\Projects\ConvertPro\env\Lib\site-packages\visualdl/component/inference/xarfile.py__init__   s   
zXarInfo.__init__returnc                 C      | j dkr	| jjS | jjS Nr   )r	   r   namefilenamer
   r   r   r   r         
zXarInfo.namec                 C   r   r   )r	   r   size	file_sizer   r   r   r   r   %   r   zXarInfo.sizeNr   )
__name__
__module____qualname____doc__r   propertystrr   intr   r   r   r   r   r      s    
r   c                	   @   s   e Zd ZdZddededefddZdd	 Zd
d Zdd Z			d dedede	de
fddZdedefddZdefddZdee fddZdedee fddZdS )!XarFileac  
    The XarFile Class provides an interface to tar/rar/zip archives.

    Args:
        name(str) : file or directory name to be archived
        mode(str) : specifies the mode in which the file is opened, it must be:
            ========   ==============================================================================================
            Charater   Meaning
            --------   ----------------------------------------------------------------------------------------------
            'r'        open for reading
            'w'        open for writing, truncating the file first, file will be saved according to the arctype field
            'a'        open for writing, appending to the end of the file if it exists
            ========   ===============================================================================================
        arctype(str) : archive type, support ['tar' 'rar' 'zip' 'tar.gz' 'tar.bz2' 'tar.xz' 'tgz' 'txz'], if
                       the mode if 'w' or 'a', the default is 'tar', if the mode is 'r', it will be based on actual
                       archive type of file
    r   r   moder	   c                 K   s(  |dkr&|dv rd}d| _ nH|dkrd}d| _ n>|dv r"d}d| _ n4|| _ n0|d	krGt|r5d| _ d
}n!t|r>d| _ nt|rFd| _ n|dkrO|| _ ntd|| j dv rhtj	||fi || _
d S | j dkrztj||fi || _
d S | j dkrtj||fi || _
d S td| j )Nw)tar.gztgzzw:gzr   tar.bz2zw:bz2)tar.xztxzzw:xzrzr:*zipZrarazUnsupported mode {})r"   r$   r%   r   r#   r&   zUnsupported archive type {})r	   tarfile
is_tarfilezipfile
is_zipfilerarfile
is_rarfileRuntimeErrorformatopen_archive_fpZipFileZRarFile)r
   r   r    r	   kwargsr   r   r   r   ?   s@   





zXarFile.__init__c                 C   s   | j   d S r   )r3   closer   r   r   r   __del__g   s   zXarFile.__del__c                 C   s   | S r   r   r   r   r   r   	__enter__j   s   zXarFile.__enter__c                 C   s"   |r
t | ||| j  | S r   )printr3   r6   )r
   Zexit_exceptionZ
exit_valueZexit_tracebackr   r   r   __exit__m   s
   
zXarFile.__exit__NTarcname	recursiveexcludec           	         s   | j dkr| jj||||d dS | j| |rtj|s!dS g }t|D ]\ }}| fdd|D 7 }| fdd|D 7 }q(|D ]}|rO||sOqF| j| qFdS )a  
        Add the file `name' to the archive. `name' may be any type of file (directory, fifo, symbolic link, etc.).
        If given, `arcname' specifies an alternative name for the file in the archive. Directories are added
        recursively by default. This can be avoided by setting `recursive' to False. `exclude' is a function that
        should return True for each filename to be excluded.
        r   )filterNc                       g | ]	}t j |qS r   ospathjoin).0_file_dr   r   
<listcomp>       zXarFile.add.<locals>.<listcomp>c                    r?   r   r@   )rD   Z_sub_drF   r   r   rH      rI   )r	   r3   addwriterA   rB   isdirwalk)	r
   r   r;   r<   r=   itemsZ_sub_ds_filesitemr   rF   r   rJ   t   s   
zXarFile.addrB   c                 C   s   | j ||S )z6Extract a file from the archive to the specified path.)r3   extract)r
   r   rB   r   r   r   rQ      s   zXarFile.extractc                 C   s   | j |S )z9Extract all files from the archive to the specified path.)r3   
extractall)r
   rB   r   r   r   rR      s   zXarFile.extractallr   c                 C   s   | j dkr
| j S | j S )z+Return a list of file names in the archive.r   )r	   r3   getnamesnamelistr   r   r   r   rS      s   


zXarFile.getnamesc                 C   s2   | j dkrt| j|| j S t| j|| j S )z,Return the instance of XarInfo given 'name'.r   )r	   r   r3   	getmembergetinfo)r
   r   r   r   r   
getxarinfo   s   
zXarFile.getxarinfor   )NTN)r   r   r   r   r   r   r7   r8   r:   boolr   rJ   rQ   rR   r   rS   r   rW   r   r   r   r   r   ,   s,    (	
r   r!   r   r    r   c                 K   s   t | |fi |S )zh
    Open a xar archive for reading, writing or appending. Return
    an appropriate XarFile class.
    )r   )r   r    r5   r   r   r   r2      s   r2   Tr   r   r<   r=   r	   c                 C   sh   t jt j| d }d||}t|d|d}|j| ||d W d   |S 1 s-w   Y  |S )aK  
    Archive a file or directory

    Args:
        name(str) : file or directory path to be archived
        recursive(bool) : whether to recursively archive directories
        exclude(Callable) : function that should return True for each filename to be excluded
        arctype(str) : archive type, support ['tar' 'rar' 'zip' 'tar.gz' 'tar.bz2' 'tar.xz' 'tgz' 'txz']

    Returns:
        str: archived file path

    Examples:
        .. code-block:: python

            archive_path = '/PATH/TO/FILE'
            archive(archive_path, arcname='output.tar.gz', arctype='tar.gz')
    r   z{}.{}r!   )r    r	   )r<   r=   N)rA   rB   splitextbasenamer1   r2   rJ   )r   r<   r=   r	   rZ   Zsavenamefiler   r   r   archive   s   
r\   rB   c                 C   s<   t | dd}|| W d   dS 1 sw   Y  dS )a%  
    Unarchive a file

    Args:
        name(str) : file or directory name to be unarchived
        path(str) : storage name of archive file

    Examples:
        .. code-block:: python

            unarchive_path = '/PATH/TO/FILE'
            unarchive(unarchive_path, path='./output')
    r'   r    N)r2   rR   )r   rB   r[   r   r   r   	unarchive   s   "r^   c                 c   s    t | dd7}d }}| D ]
}|||j7 }q| D ]}||| |||j7 }|||fV  qW d   dS 1 sAw   Y  dS )a  
    Unarchive a file and return the unarchiving progress -> Generator[filename, extrace_size, total_size]

    Args:
        name(str) : file or directory name to be unarchived
        path(str) : storage name of archive file

    Examples:
        .. code-block:: python

            unarchive_path = 'test.tar.gz'
            for filename, extract_size, total_szie in unarchive_with_progress(unarchive_path, path='./output'):
                print(filename, extract_size, total_size)
    r'   r]   r   N)r2   rS   rW   r   rQ   )r   rB   r[   
total_sizeZextract_sizer   r   r   r   unarchive_with_progress   s   "r`   r[   c                 C   s,   t jtjtjg}|D ]	}|| r dS q
dS )z>Return True if xarfile supports specific file, otherwise FalseTF)r,   r-   r*   r+   r.   r/   )r[   Z_x_funcZ_fr   r   r   
is_xarfile   s   ra   )r!   )TNr   )rA   r*   r,   typingr   r   r   r.   objectr   r   r   r2   rX   r\   r^   r   r`   ra   r   r   r   r   <module>   s2   x	
 