a
    yn„f  ã                
   @   s"  d Z ddlmZ ddlZddlmZ dd„ ZdeiZd(dd	„Z	d)dd„Z
e	de
dƒ zddlmZ W n eyv   Y n0 e	dedƒ z&ddlmZ d*dd„Ze	dedƒ W n. eyØ   ddlmZ e	ddd„ dƒ Y n0 z(ddlmZ e	dedƒ e	dedƒ W n e�y   Y n0 z0ddlZe	dejddd� e	dejddd� W n e�y\   Y n0 G dd„ deƒZz"ddlZe d¡ e	d eg ƒ W n eeef�yª   Y n0 zddlZe	d!ejjd!ƒ W n e�yÜ   Y n0 z"ddlZd+d"d#„Z e	d$e d%ƒ W n e�y   Y n0 d&d'„ Z!dS ),z9Helper functions for a standard streaming compression APIé    )ÚZipFileN)ÚAbstractBufferedFilec                 K   s   | S ©N© )ÚfileÚmodeÚkwargsr   r   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/fsspec/compression.pyÚ	noop_file	   s    r
   Fc                 C   s|   t |tƒr|g}| tv r*|s*td| › �ƒ‚|D ]*}|tjjv r.|s.td|› d| › d�ƒ‚q.|t| < |D ]}| tjj|< qfdS )aA  Register an "inferable" file compression type.

    Registers transparent file compression type for use with fsspec.open.
    Compression can be specified by name in open, or "infer"-ed for any files
    ending with the given extensions.

    Args:
        name: (str) The compression type name. Eg. "gzip".
        callback: A callable of form (infile, mode, **kwargs) -> file-like.
            Accepts an input file-like object, the target mode and kwargs.
            Returns a wrapped file-like object.
        extensions: (str, Iterable[str]) A file extension, or list of file
            extensions for which to infer this compression scheme. Eg. "gz".
        force: (bool) Force re-registration of compression type or extensions.

    Raises:
        ValueError: If name or extensions already registered, and not force.

    z$Duplicate compression registration: z&Duplicate compression file extension: z (ú)N)Ú
isinstanceÚstrÚcomprÚ
ValueErrorÚfsspecÚutilsÚcompressions)ÚnameÚcallbackÚ
extensionsÚforceÚextr   r   r	   Úregister_compression   s    
r   Úrbc                    s|   d|vrJ|pd}t | fddi|¤Ž‰ ˆ j|dd�}|jf‡ fdd„	|_|S t | ƒ‰ |d u rfˆ  ¡ d }ˆ j|fddi|¤ŽS )	NÚrr   r   Úw)r   c                    s   | ƒ pˆ   ¡ S r   )Úclose)Úcloser©Úzr   r	   Ú<lambda><   ó    zunzip.<locals>.<lambda>r   )r   Úopenr   Únamelist)Úinfiler   Úfilenamer   Úfor   r   r	   Úunzip7   s    r'   Úzip)ÚBZ2FileÚbz2)Úigzipc                 K   s   t jf | |dœ|¤ŽS )N)Úfileobjr   )r+   Z	IGzipFile)r$   r   r   r   r   r	   ÚisalP   s    r-   ÚgzipÚgz©ÚGzipFilec                 K   s   t f d| i|¤ŽS )Nr,   r0   )Úfr   r   r   r	   r    X   r!   r    )ÚLZMAFileÚlzmaÚxzT)r   c                       s@   e Zd Z‡ fdd„Zddd„Zddd„Zd	d
„ Zdd„ Z‡  ZS )Ú
SnappyFilec                    sV   dd l }tƒ jf d d| d¡d ddœ|¤Ž || _d|v rH| ¡ | _n
| ¡ | _d S )Nr   ÚsnappyÚbiÿÉš;)ÚfsÚpathr   Úsizer   )r7   ÚsuperÚ__init__Ústripr$   ZStreamDecompressorÚcodecZStreamCompressor)Úselfr$   r   r   r7   ©Ú	__class__r   r	   r=   m   s    ÿÿzSnappyFile.__init__Fc                 C   s.   | j  d¡ | j | j  ¡ ¡}| j |¡ dS )Nr   T)ÚbufferÚseekr?   Z	add_chunkÚreadr$   Úwrite)r@   ÚfinalÚoutr   r   r	   Ú_upload_chunky   s    zSnappyFile._upload_chunkr   c                 C   s   t dƒ‚d S )NzSnappyFile is not seekable)ÚNotImplementedError)r@   ÚlocÚwhencer   r   r	   rD      s    zSnappyFile.seekc                 C   s   dS )NFr   )r@   r   r   r	   Úseekable‚   s    zSnappyFile.seekablec                 C   s   | j  || ¡}| j |¡S )z*Get the specified set of bytes from remote)r$   rE   r?   Ú
decompress)r@   ÚstartÚendÚdatar   r   r	   Ú_fetch_range…   s    zSnappyFile._fetch_range)F)r   )	Ú__name__Ú
__module__Ú__qualname__r=   rI   rD   rM   rR   Ú__classcell__r   r   rA   r	   r6   l   s
   

r6   r!   r7   Úlz4c                 C   s4   d|v rt  ¡ }| | ¡S t jdd�}| | ¡S d S )Nr   é
   )Úlevel)ÚzstdZZstdDecompressorÚstream_readerZZstdCompressorZstream_writer)r$   r   Zcctxr   r   r	   Úzstandard_file    s
    
r\   rZ   Zzstc                   C   s   t tƒS )z.Return a list of the implemented compressions.)Úlistr   r   r   r   r	   Úavailable_compressions­   s    r^   )F)r   N)r   )r   )"Ú__doc__Úzipfiler   Zfsspec.utilsr   Zfsspec.specr   r
   r   r   r'   r*   r)   ÚImportErrorr-   r+   r.   r1   r4   r3   Zlzmaffir6   r7   ÚcompressÚ	NameErrorÚAttributeErrorZ	lz4.framerW   Úframer"   Z	zstandardrZ   r\   r^   r   r   r   r	   Ú<module>   sj   
%


ÿ


