o
    }oi(                  
   @   s   d dl Z d dlZd dlZd dlZd dlmZmZ edZz
d dl	m
Z
 dZW n$ eyK Z zedeej de  eZ
dZW Y dZ[ndZ[ww G d	d
 d
ZG dd de
ZdS )    N)IOUnionNeMo)	BaseStoreTzICannot import zarr, support for zarr-based checkpoints is not available. z: Fc                   @   s   e Zd ZdZdeeejd f fddZdd Z	d(dd	Z
defd
dZedd Zedd Zedd Zedd Zdd Zdd Zdd Zdd Zdd Zdedee fdd Zd!d" Zd#d$ Zd%d& Zd'S ))TarPatha  
    A class that represents a path inside a TAR archive and behaves like pathlib.Path.

    Expected use is to create a TarPath for the root of the archive first, and then derive
    paths to other files or directories inside the archive like so:

    with TarPath('/path/to/archive.tar') as archive:
        myfile = archive / 'filename.txt'
        if myfile.exists():
            data = myfile.read()
            ...

    Only read and enumeration operations are supported.
    tarc                 G   s   d| _ d| _t|tr|j| _tjj|jg|R  | _d S t|tj	r3|| _|r1tjj| | _d S d S t|t
rOd| _ t|d| _|rMtjj| | _d S d S tdt|j )NF Trz&Unexpected argument type for TarPath: )_needs_to_close_relpath
isinstancer   _tarospathjointarfileTarFilestropen
ValueErrortype__name__)selfr   parts r   H/home/ubuntu/.local/lib/python3.10/site-packages/nemo/export/tarutils.py__init__2   s"   

zTarPath.__init__c                 C   s   | j r
| j  d S d S N)r
   r   closer   r   r   r   __del__D   s   zTarPath.__del__returnc                 C   s   t | jtj| j|S r   )r   r   r   r   r   r   r   keyr   r   r   __truediv__H   s   zTarPath.__truediv__c                 C   s   t j| jj| jS r   )r   r   r   r   namer   r   r   r   r   __str__K   s   zTarPath.__str__c                 C      | j S )z1
        Returns the wrapped tar object.
        )r   r   r   r   r   	tarobjectN      zTarPath.tarobjectc                 C   r'   )z8
        Returns the relative path of the path.
        )r   r   r   r   r   relpathU   r)   zTarPath.relpathc                 C   s   t j| jd S )z/
        Returns the name of the path.
           )r   r   splitr   r   r   r   r   r%   \   s   zTarPath.namec                 C   sB   | j }|d}d|  k rt|d k rn dS ||d S dS )z1
        Returns the suffix of the path.
        .r   r+   Nr   )r%   rfindlen)r   r%   ir   r   r   suffixc   s   
zTarPath.suffixc                 C   s   | j   | S r   )r   	__enter__r   r   r   r   r2   o   s   
zTarPath.__enter__c                 G   s   | j j| S r   )r   __exit__)r   argsr   r   r   r3   s   s   zTarPath.__exit__c                 C   s\   z
| j | j W dS  ty-   z| j tjd| j W Y dS  ty,   Y Y dS w w )z,
        Checks if the path exists.
        Tr-   F)r   	getmemberr   KeyErrorr   r   r   r   r   r   r   existsv   s   zTarPath.existsc                 C   d   z| j | j  W dS  ty1   z| j tjd| j  W Y dS  ty0   Y Y dS w w )z/
        Checks if the path is a file.
        Tr-   F)r   r5   r   isregr6   r   r   r   r   r   r   r   is_file      zTarPath.is_filec                 C   r8   )z4
        Checks if the path is a directory.
        Tr-   F)r   r5   r   isdirr6   r   r   r   r   r   r   r   is_dir   r;   zTarPath.is_dirmodec                 C   s   |dkr|dkrt  d}z	| j| j}W n" ty8   z| jtjd| j}W n
 ty5   t w Y nw |du r@t |S )z.
        Opens a file in the archive.
        r	   rbNr-   )	NotImplementedErrorr   extractfiler   r6   r   r   r   FileNotFoundError)r   r>   filer   r   r   r      s    zTarPath.openc                 c   s    | j  D ]<}|jdr|jdd n|j}| jr/|| jd s$q|t| jd d }t||rBt| j tj	
| j|V  qdS )z\
        Returns an iterator over the files in the directory, matching the pattern.
        ./   N/r+   )r   
getmembersr%   
startswithr   r/   fnmatchr   r   r   r   )r   patternmemberr%   r   r   r   glob   s    zTarPath.globc                 c   s    | j  D ]U}|jdr|jdd n|j}| jr/|| jd s$q|t| jd d }|d}tt|D ] }d||d }t		||rZt
| j tj| j|V   nq:qdS )z`
        Returns an iterator over the files in the directory, including subdirectories.
        rD   rE   NrF   r+   )r   rG   r%   rH   r   r/   r,   ranger   rI   r   r   r   )r   rJ   rK   r%   r   r0   subnamer   r   r   rglob   s     
zTarPath.rglobc                 C   s
   |  dS )zF
        Returns an iterator over the files in the directory.
        *)rL   r   r   r   r   iterdir      
zTarPath.iterdirN)r!   r   )r   
__module____qualname____doc__r   r   r   r   r   r    r$   r&   propertyr(   r*   r%   r1   r2   r3   r7   r:   r=   r   bytesr   rL   rO   rQ   r   r   r   r   r   "   s.    




r   c                   @   sV   e Zd ZdZdef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S )ZarrPathStorezt
    An implementation of read-only Store for zarr library
    that works with pathlib.Path or TarPath objects.
    tarpathc                 C   s"   t sJ d|| _d| _d| _d S )Nz<Package zarr>=2.18.2,<3.0.0 is required to use ZarrPathStoreF)	HAVE_ZARR_path	_writable	_erasable)r   rY   r   r   r   r      s   
zZarrPathStore.__init__c                 C   s<   | j | d}| W  d    S 1 sw   Y  d S )Nr?   )r[   r   read)r   r#   rC   r   r   r   __getitem__   s   $zZarrPathStore.__getitem__c                 C   s   | j |  S r   )r[   r:   r"   r   r   r   __contains__   s   zZarrPathStore.__contains__c                 C   s   |   S r   )keysr   r   r   r   __iter__   s   zZarrPathStore.__iter__c                 C   s   t dd |  D S )Nc                 s   s    | ]}d V  qdS )r+   Nr   ).0_r   r   r   	<genexpr>   s    z(ZarrPathStore.__len__.<locals>.<genexpr>)sumra   r   r   r   r   __len__   s   zZarrPathStore.__len__c                 C      t  r   r@   )r   r#   valuer   r   r   __setitem__      zZarrPathStore.__setitem__c                 C   rh   r   ri   r"   r   r   r   __delitem__  rl   zZarrPathStore.__delitem__c                 C   s
   | j  S )zA
        Returns an iterator over the keys in the store.
        )r[   rQ   r   r   r   r   ra     rR   zZarrPathStore.keysN)r   rS   rT   rU   r   r   r_   r`   rb   rg   rk   rm   ra   r   r   r   r   rX      s    rX   )rI   loggingr   r   typingr   r   	getLoggerLOGGERzarr.storager   rZ   	Exceptionewarningr   r   objectr   rX   r   r   r   r   <module>   s$   
 E