U e5dS@sdZddlZddlZddlZddlZddlZddlZddlZddlZdddddgZ d\Z Z Z Z Zd \ZZd Zd Zd Zd edddfddZddZGdddZGdddeZGdddejZGdddejZefddddZddZddZe dkredS)zFunctions that read and write gzipped files. The user of the file doesn't have to worry about the compression, but random access is not allowed.N BadGzipFileGzipFileopencompress decompress))rrr rbcCsd|kr d|krPtd|fn0|dk r0td|dk r@td|dk rPtd|dd}t|tttjfr|t|||}n,t|d st|d rtd|||}nt d d|krt ||||S|SdS) a Open a gzip-compressed file in binary or text mode. 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 9. 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). tbzInvalid mode: %rNz0Argument 'encoding' not supported in binary modez.Argument 'errors' not supported in binary modez/Argument 'newline' not supported in binary modereadwritez1filename must be a str or bytes object, or a file) ValueErrorreplace isinstancestrbytesosPathLikerhasattr TypeErrorio TextIOWrapper)filenamemode compresslevelencodingerrorsnewlineZgz_modeZ binary_filer%/usr/lib64/python3.8/gzip.pyrs$ cCs|td|dS)Nz|jdkr||_n|jt|8_dSt|j|_d|_dSr-)r2r.r/r0)r3r5r%r%r&r5bs   z_PaddedFile.prependcCsd|_d|_|j|Sr7)r2r.r1seek)r3Zoffr%r%r&r9ksz_PaddedFile.seekcCsdSNTr%r3r%r%r&seekablepsz_PaddedFile.seekableN)r,)r,) __name__ __module__ __qualname____doc__r6rr5r9r<r%r%r%r&r+Js   r+c@seZdZdZdS)rz6Exception raised in some cases for invalid gzip files.N)r=r>r?r@r%r%r%r&rtsc@seZdZdZdZddeddfddZeddZeddZ d d Z d d Z d dZ ddZ d,ddZd-ddZddZeddZddZejfddZddZd d!Zd"d#Zd$d%Zd&d'Zejfd(d)Zd.d*d+ZdS)/ra The GzipFile 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. NcCs4|r"d|ksd|kr"td||r6d|kr6|d7}|dkrTt||pJd}|_|dkr|t|dd}t|ttfsd}n t |}|dkrt|d d}| d rt |_ t|}t||_||_nN| d rt|_ ||t|tjtj tjd |_||_ntd|||_|j tkr0||dS) aGConstructor for the GzipFile 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 9 controlling the level of compression; 1 is fastest and produces the least compression, and 9 is slowest and produces the most compression. 0 is no compression at all. The default is 9. 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. rUzInvalid mode: {!r}rNrnamerr r)waxr)rformatbuiltinsr myfileobjgetattrrrrrfspath startswithREADr _GzipReaderrBufferedReaderr.rBWRITE _init_writezlibZ compressobjZDEFLATED MAX_WBITSZ DEF_MEM_LEVELr _write_mtimefileobj_write_gzip_header)r3rr r!rUmtimerawr%r%r&r6s@#         zGzipFile.__init__cCsBddl}|dtd|jtkr<|jdddkr<|jdS|jS)Nrzuse the name attributer.gz)warningswarnDeprecationWarningr rPrB)r3r[r%r%r&rs  zGzipFile.filenamecCs |jjjS)z0Last modification time read from stream, or None)r.rX _last_mtimer;r%r%r&rWszGzipFile.mtimecCs.t|j}d|dddtt|dS)Nz)reprrUhexid)r3sr%r%r&__repr__s zGzipFile.__repr__cCs.||_td|_d|_g|_d|_d|_dSNr,r)rBrRcrc32crcr8Zwritebufbufsizeoffset)r3rr%r%r&rQs  zGzipFile._init_writecCs|jd|jdzs   zGzipFile.closecCs4||jtkr0|j|j||jdSr7)r~r rPrUrrr)r3Z zlib_moder%r%r&rQs zGzipFile.flushcCs |jS)zInvoke the underlying file object's fileno() method. This will raise AttributeError if the underlying file object doesn't support fileno(). )rUfilenor;r%r%r&rXszGzipFile.filenocCs"|jtkrtd|jddS)z[Return the uncompressed stream file position indicator to the beginning of the filezCan't rewind in write moderN)r rMrr.r9r;r%r%r&rewind`s zGzipFile.rewindcCs |jtkSr7)r rMr;r%r%r&readablegszGzipFile.readablecCs |jtkSr7)r rPr;r%r%r&writablejszGzipFile.writablecCsdSr:r%r;r%r%r&r<mszGzipFile.seekablecCs|jtkr|tjkr2|tjkr*|j|}ntd||jkrDtd||j}d}t|dD]}| |q^| d|dn |jt kr| |j ||S|jS)NzSeek from end not supportedzNegative seek in write modesrp)r rPrSEEK_SETSEEK_CURrkrrrangerrMr~r.r9)r3rkwhencecountchunkir%r%r&r9ps         z GzipFile.seekcCs||j|Sr7)r~r.readline)r3r8r%r%r&rszGzipFile.readline)r_)r_)r_)r=r>r?r@rIr{r6propertyrrWrfrQrVrrrrrrrRZ Z_SYNC_FLUSHrrrrrr<rrr9rr%r%r%r&rxs:  I      csZeZdZfddZddZddZddZdd d Zd d ZddZ fddZ Z S)rNcs,tjt|tjtj dd|_d|_dS)N)ZwbitsT)superr6r+rRZ decompressobjrS _new_memberr^)r3fp __class__r%r&r6s z_GzipReader.__init__cCstd|_d|_dSrg)rRrh_crc _stream_sizer;r%r%r& _init_reads z_GzipReader._init_readcCsF|j|}t||krB|j|t|}|s8td||7}q |S)zRead exactly *n* bytes from `self._fp` This method is required because self._fp may be unbuffered, i.e. return short reads. ACompressed file ended before the end-of-stream marker was reached)_fprr/EOFError)r3rrrr%r%r& _read_exacts   z_GzipReader._read_exactcCs|jd}|dkrdS|dkr,td|td|d\}}|_|dkrVtd|t@r|td |d\}|||t@r|jd }|r|d krqq|t @r|jd }|r|d krqq|t @r|dd S) Nrr,FrlzNot a gzipped file (%r)z|d|_|jf|j|_|jrf||s`|j |_ dSd|_|j t j}|j||}|jjdkr|j |jjn|jjdkr|j |jj|dkrq|dkrtdq|||j t|7_ |S)Nrr,TFr)readallZ _decompressoreof _read_eofrZ_decomp_factoryZ _decomp_argsrrZ_posZ_sizerrrrrZunconsumed_tailr5Z unused_datar_add_read_datar/)r3r8bufZ uncompressr%r%r&rs:    z_GzipReader.readcCs$t||j|_|jt||_dSr7)rRrhrrr/)r3rr%r%r&rsz_GzipReader._add_read_datacCstd|d\}}||jkrr?r6rrrrrrr __classcell__r%r%rr&rNs ! 3rN)rWc Cs6t}t|d||d}||W5QRX|S)zCompress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9. wb)rUr r!rW)rBytesIOrrgetvalue)rr!rWrr4r%r%r&rsc Cs0tt|d}|W5QRSQRXdS)zYDecompress a gzip compressed string in one shot. Return the decompressed string. rN)rrrr)rr4r%r%r&rsc Csddlm}|dd}|}|jdddd|jd dd d|jd d dd d|jdddgdd|}t}|jr|t}n |jrt }|j D]}|j r|dkrt ddt jjd}t jj}n>|dddkrt d|t|d}t|ddd}nB|dkr"t jj}t ddt jj|d}nt|d}t|dd}|d}|sPq^||q<|t jjk rt||t jjk r|qdS)Nr)ArgumentParserzeA simple command line interface for the gzip module: act like gzip, but do not delete the input file.)Z descriptionz--fast store_truezcompress faster)actionhelpz--bestzcompress betterz-dz --decompresszact like gunzip instead of gzipargs*-r1)nargsdefaultmetavarrr)rr rUrYrZzfilename doesn't end in .gz: r)rr rUr!r)argparserZadd_mutually_exclusive_group add_argument parse_args_COMPRESS_LEVEL_TRADEOFFZfastr|Zbestr{rrrsysstdinbufferstdoutexitrrHrrr) rparsergrouprr!argr4grr%r%r&main'sR          r__main__)!r@r'rryrrRrHrZ _compression__all__ZFTEXTrrrwrrMrPr|rr{rr*r+rrZ BaseStreamrZDecompressReaderrNrrrr=r%r%r%r&s:  ,* 0