a
    yn„f  ã                   @   sd   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dœZ	e  
d¡ZG d	d
„ d
eƒZdS )é    N)ÚAbstractArchiveFileSystem)Úcompr)Úinfer_compressionÚfileÚ	directory)ó   0ó   5Útarc                       sH   e Zd ZdZdZdZdZd‡ fdd„	Zdd	„ Zd
d„ Z	ddd„Z
‡  ZS )ÚTarFileSystemzzCompressed Tar archives as a file-system (read-only)

    Supports the following formats:
    tar.gz, tar.bz2, tar.xz
    Ú r	   FNc           	   
      sN  t ƒ jf i |¤Ž |pi }t|tƒrFtj|fd|i|¤Ž| _| j ¡ }|d u rþd }zPt|dƒrf|j}n:t|dƒrx|j	}n(t|dƒrŠ|j
}nt|dƒr | ¡ d }W n4 tyÖ } zt d|› �¡ W Y d }~n
d }~0 0 |d urþt|ƒ}t d|› d|› �¡ |d u�rt| |ƒ}|| _|| _tj| jd	�| _d | _|| _d | _|  ¡  d S )
NÚprotocolÚoriginalÚpathÚnameÚinfoz:Unable to determine file name, not inferring compression: zInferred compression z from file name )Úfileobj)ÚsuperÚ__init__Ú
isinstanceÚstrÚfsspecÚopenZofÚhasattrr   r   r   r   Ú	ExceptionÚloggerÚwarningr   r   Z_fo_refÚfoÚtarfileÚTarFiler	   Ú	dir_cacheÚindex_storeÚindexÚ_index)	Úselfr   r    Ztarget_optionsZtarget_protocolÚcompressionÚkwargsr   Úex©Ú	__class__© ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/fsspec/implementations/tar.pyr      s@    	





ÿ
zTarFileSystem.__init__c                 C   sV   i }| j D ]@}| ¡ }t |d d¡|d< | ¡ d  d¡}||jf||< q
|| _d S )NÚtyper   r   ú/)r	   Úget_infoÚtypemapÚgetÚrstripÚoffset_datar!   )r#   ÚoutÚtir   r   r)   r)   r*   r"   [   s    
zTarFileSystem._indexc                 C   sz   | j d urd S dd„ |  | j ¡ ¡D ƒ| _ | j ¡ D ]@}| ¡ }|d  d¡|d< t |d d¡|d< || j |d < q4d S )Nc                 S   s   i | ]}||d ddœ“qS )r   r   )r   Úsizer+   r)   )Ú.0Údirnamer)   r)   r*   Ú
<dictcomp>l   s   ÿz+TarFileSystem._get_dirs.<locals>.<dictcomp>r   r,   r+   r   )	r   Z_all_dirnamesr	   ÚgetnamesÚ
getmembersr-   r0   r.   r/   )r#   Úmemberr   r)   r)   r*   Ú	_get_dirsg   s    
þzTarFileSystem._get_dirsÚrbc                 K   s>   |dkrt dƒ‚| j| \}}|d dkr2t dƒ‚| j |¡S )Nr<   z#Read-only filesystem implementationr+   r   zCan only handle regular files)Ú
ValueErrorr!   r	   Úextractfile)r#   r   Úmoder%   ÚdetailsÚoffsetr)   r)   r*   Ú_openv   s    zTarFileSystem._open)r   NNNN)r<   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zroot_markerr   Zcachabler   r"   r;   rB   Ú__classcell__r)   r)   r'   r*   r
      s        úBr
   )Úloggingr   r   Zfsspec.archiver   Zfsspec.compressionr   Zfsspec.utilsr   r.   Ú	getLoggerr   r
   r)   r)   r)   r*   Ú<module>   s   

