o
    DfX<                     @   s6  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZ ddlmZmZ ddlmZ g dZejZejZejZdZd	\ZZZZZejZejZej Z d
edddfddZ!G dd dej"Z#e#Z"eZ$efddde%dee de&fddZ'dd Z(dd Z)dd Z*e+dkre*  dS dS )zlSimilar to the stdlib gzip module. But using the Intel Storage Accelaration
Library to speed up its methods.    N)OptionalSupportsInt   )	igzip_lib	isal_zlib)_GzipReader)	IGzipFileopencompress
decompressBadGzipFileREAD_BUFFER_SIZEi   )r               rbc                 C   s   d|v rd|v rt d|f n|durt d|dur t d|dur(t d|dd}t| ttfs:t| d	rAt| ||}nt| d
sKt| drStd||| }ntdd|v rct	||||S |S )aA  Open a gzip-compressed file in binary or text mode. This uses the isa-l
    library for optimized speed.

    The filename argument can be an actual filename (a str or bytes object), or
    an existing file object to read from or write to.

    The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for
    binary mode, or "rt", "wt", "xt" or "at" for text mode. The default mode is
    "rb", and the default compresslevel is 2.

    For binary mode, this function is equivalent to the GzipFile constructor:
    GzipFile(filename, mode, compresslevel). In this case, the encoding, errors
    and newline arguments must not be provided.

    For text mode, a GzipFile object is created, and wrapped in an
    io.TextIOWrapper instance with the specified encoding, error handling
    behavior, and line ending(s).

    tbzInvalid mode: %rNz0Argument 'encoding' not supported in binary modez.Argument 'errors' not supported in binary modez/Argument 'newline' not supported in binary mode 
__fspath__readwritez1filename must be a str or bytes object, or a file)

ValueErrorreplace
isinstancestrbyteshasattrr   	TypeErrorioTextIOWrapper)filenamemodecompresslevelencodingerrorsnewlinegz_modebinary_file r*   S/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/isal/igzip.pyr	   >   s*   r	   c                       sN   e Zd ZdZddejddf fdd	Zdd Zef fdd	Z	d	d
 Z
  ZS )r   a
  The IGzipFile class simulates most of the methods of a file object with
    the exception of the truncate() method.

    This class only supports opening files in binary mode. If you need to open
    a compressed file in text mode, use the gzip.open() function.
    Nc                    s   t j|  krt jks!n d|vr!tdt j dt j d| dt ||||| | jtkr>t |t j	t j
 t jd| _| jtkrQt| jt}t|| _dS dS )aX  Constructor for the IGzipFile class.

        At least one of fileobj and filename must be given a
        non-trivial value.

        The new class instance is based on fileobj, which can be a regular
        file, an io.BytesIO object, or any other object which simulates a file.
        It defaults to None, in which case filename is opened to provide
        a file object.

        When fileobj is not None, the filename argument is only used to be
        included in the gzip file header, which may include the original
        filename of the uncompressed file.  It defaults to the filename of
        fileobj, if discernible; otherwise, it defaults to the empty string,
        and in this case the original filename is not included in the header.

        The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x',
        or 'xb' depending on whether the file will be read or written.
        The default is the mode of fileobj if discernible; otherwise, the
        default is 'rb'. A mode of 'r' is equivalent to one of 'rb', and
        similarly for 'w' and 'wb', 'a' and 'ab', and 'x' and 'xb'.

        The compresslevel argument is an integer from 0 to 3 controlling the
        level of compression; 0 is fastest and produces the least compression,
        and 3 is slowest and produces the most compression. Unlike
        gzip.GzipFile 0 is NOT no compression. The default is 2.

        The mtime argument is an optional numeric timestamp to be written
        to the last modification time field in the stream when compressing.
        If omitted or None, the current time is used.
        rz$Compression level should be between z and z, got .r   N)r   ISAL_BEST_SPEEDISAL_BEST_COMPRESSIONr   super__init__r#   WRITEcompressobjDEFLATED	MAX_WBITSDEF_MEM_LEVELr
   READr   fileobjr   r    BufferedReader_buffer)selfr"   r#   r$   r8   mtimeraw	__class__r*   r+   r1   u   s2   "

zIGzipFile.__init__c                 C   s.   t | j}d|dd  d tt|  d S )Nz<igzip r    >)reprr8   hexid)r;   sr*   r*   r+   __repr__   s   
$zIGzipFile.__repr__c                    s^   dt  jjjv r(ttdr(ttdr(|tkrt  tj d S t  tj d S t    d S )Nr$   _COMPRESS_LEVEL_FAST_COMPRESS_LEVEL_TRADEOFF)r0   _write_gzip_header__code__co_varnamesr   gziprH   rI   )r;   r$   r>   r*   r+   rJ      s   zIGzipFile._write_gzip_headerc                 C   s   |    | jtkrdd l}t|jd| jd u rtdt|t	r&t
|}nt|}|j}|dkrQ| j| j| |  j|7  _t|| j| _|  j|7  _|S )Nr   z%write() on read-only IGzipFile objectz"write() on closed IGzipFile object)_check_not_closedr#   r2   errnoOSErrorEBADFr8   r   r   r   len
memoryviewnbytesr   r
   sizer   crc32crcoffset)r;   datarO   lengthr*   r*   r+   r      s    



zIGzipFile.write)__name__
__module____qualname____doc__r   ISAL_DEFAULT_COMPRESSIONr1   rG   rI   rJ   r   __classcell__r*   r*   r>   r+   r   n   s    4r   )r<   r$   r<   returnc             
   C   sV   |du rt   }|tkrdnd}tdddddt||d}tj| |tjd	}|| S )
zCompress data in one shot and return the compressed string.
    Optional argument is the compression level, in range of 0-3.
    Nr   r   z<BBBBLBB      r      )flag)timerH   structpackintr   r
   ZCOMP_GZIP_NO_HDR)rY   r$   r<   xflheader
compressedr*   r*   r+   r
      s   r
   c                 C   s   t | }| S )a	  Decompress a gzip compressed string in one shot.
    Return the decompressed string.

    This function checks for extra gzip members. Using
    isal_zlib.decompress(data, wbits=31) is faster in cases where only one
    gzip member is guaranteed to be present.
    )r   readall)rY   readerr*   r*   r+   r      s   r   c                  C   s  t  } d| _| jddd |  }|jddddtd	d
 |jdddddd
 |jdddddd
 |jddddtdd
 |jdd |jddddddd
 |  }|jddddd |jd d!d"d# | jd$d%dd&d'd( | jd)d*dd+d | jd,d-tt	t j
d. | S )/NzFA simple command line interface for the igzip module. Acts like igzip.file?)nargsz-0z--faststore_constr$   z!use compression level 0 (fastest))actiondestconsthelpz-1r   zuse compression level 1z-2r   z!use compression level 2 (default)z-3z--bestzuse compression level 3 (best)T)r
   z-dz--decompressr
   Fz+Decompress the file instead of compressing.z-cz--stdout
store_truezwrite on standard output)rs   rv   z-oz--outputzWrite to this output file)rv   z-nz	--no-namereproduciblez6do not save or restore the original name and timestamp)rs   rt   rv   z-fz--forcez"Overwrite output without promptingz-bz--buffer-size)defaulttyperv   )argparseArgumentParserdescriptionadd_argumentadd_mutually_exclusive_grouprH   _COMPRESS_LEVEL_BESTset_defaultsr   ri   SUPPRESS)parserZcompress_groupZoutput_groupr*   r*   r+   _argument_parser  sf   


r   c            	      C   s6  t   } | jp	t}| jr| j}n1| jrd }n+| jd u rd }n#| jr(| jd }ntj	
| j\}}|dkrB| jsBtd| jd |d ur_| js_tj	|r_t| d}|dvr_td d }| jr| jd u rntjj}ntj| jdd}|d urt|d	}ntjj}| jrd
dd}nd|i}tdd	||d|}n#| jrt| jdd}ntdtjjd}|d urtj|d	d}ntjj}z1t||| j W |tjjur|  |tjjur|  |d ur|tjjur|  d S d S d S |tjjur|  |tjjur|  |d ur|tjjur|  w w w )Nz.gzzfilename doesn't end in .gz: z#. Cannot determine output filename.z0 already exists; do you wish to overwrite (y/n)?>   yYyesznot overwrittenr   )r#   wbr       )r<   r"   r"   )r#   r8   r$   )r#   r8   r*   )r   
parse_argsr$   rI   outputstdoutro   r
   ospathsplitextsysexitforceexistsinputstdinbufferbuiltinsr	   rx   r   shutilcopyfileobjbuffer_sizeclose)	argsr$   Zout_filepath	extensionZ	yes_or_noZ
out_bufferin_fileZgzip_file_kwargsout_filer*   r*   r+   main0  sp   






r   __main__),r^   r{   r   rM   r    r   r   rg   r   rf   typingr   r   r   r   r   r   __all__r.   rH   r_   rI   r/   r   r   FTEXTFHCRCFEXTRAFNAMEFCOMMENTr7   r2   r   r	   GzipFiler   Z_IGzipReaderri   r   r
   r   r   r   r[   r*   r*   r*   r+   <module>   sR   
0o

/@
