3 ^nOc@sdZddlZddlZddlZddlZddlZddlZddlZddlZddddgZ d\Z Z Z Z Zd\ZZdddZddZGdddZGdddejZGdddejZdddZddZddZedkredS)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.NGzipFileopencompress decompressrb cCsd|kr d|krPtd|fn0|dk r0td|dk r@td|dk rPtd|jdd}t|tttjfr|t|||}n,t|d st|d rtd|||}nt d d|krt j ||||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) ValueErrorZreplace isinstancestrbytesosZPathLikerZhasattrZ TypeErrorioZ TextIOWrapper)filenamemode compresslevelZencodingZerrorsZnewlineZgz_modeZ binary_filer/usr/lib64/python3.6/gzip.pyrs$ cCs|jtjd|dS)Nz|jdkr||_n|jt|8_dSt|j|_d|_dSr!)r&r"r#r$)r'r)rrrr)]s   z_PaddedFile.prependcCsd|_d|_|jj|Sr+)r&r"r%seek)r'Zoffrrrr-fsz_PaddedFile.seekcCdSNTrr'rrrseekablekz_PaddedFile.seekableNr r3) __name__ __module__ __qualname____doc__r*rr)r-r1rrrrrEs   rc@seZdZdZdZd-ddZeddZedd Zd d Z d d Z ddZ ddZ d/ddZ d1ddZddZeddZddZejfddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zejfd)d*Zd3d+d,ZdS)4ra 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. Nr cCs2|r"d|ksd|kr"tdj||r6d|kr6|d7}|dkrTtj||pJd}|_|dkr|t|dd}t|ttfsd}n t j |}|dkrt|d d}|j d rt |_ t|}tj||_||_nN|j drt|_ |j|tj|tjtj tjd|_||_ntdj|||_|j tkr.|jdS)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. r ZUzInvalid mode: {!r}r Nr namerrZrwaxr)r9r:r;)rZformatbuiltinsr myfileobjZgetattrrrrrZfspathZ startswithREADr _GzipReaderrZBufferedReaderr"r8WRITE _init_writezlibZ compressobjZDEFLATED MAX_WBITSZ DEF_MEM_LEVELr _write_mtimefileobj_write_gzip_header)r'rrrrEmtimerawrrrr*{s>#         zGzipFile.__init__cCsBddl}|jdtd|jtkr<|jdddkr<|jdS|jS)Nrzuse the name attributer.gz)warningsZwarnZDeprecationWarningrr@r8)r'rLrrrrs  zGzipFile.filenamecCs |jjjS)z0Last modification time read from stream, or None)r"rH _last_mtimer0rrrrGrzGzipFile.mtimecCs.t|j}d|dddtt|dS)Nz)reprrEhexZid)r'srrr__repr__ zGzipFile.__repr__cCs.||_tjd|_d|_g|_d|_d|_dSNr r)r8rBcrc32crcr,ZwritebufZbufsizeoffset)r'rrrrrAs  zGzipFile._init_writec Cs|jjd|jjdyr]r^r_r"rr'r,r]rrrr   z GzipFile.readcCs@|j|jtkr&ddl}t|jd|dkr4tj}|jj |S)zdImplements BufferedIOBase.read1() Reads up to a buffer's worth of data is size is negative.rNz%read1() on write-only GzipFile object) r\rr>r]r^r_rDEFAULT_BUFFER_SIZEr"read1rbrrrres  zGzipFile.read1cCra)Nrz$peek() on write-only GzipFile object)r\rr>r]r^r_r"peek)r'nr]rrrrf#rcz GzipFile.peekcCs |jdkSr+rEr0rrrclosed*szGzipFile.closedc Cs|j}|dkrdSd|_zP|jtkrR|j|jjt||jt||jd@n|jt krf|j j Wd|j }|rd|_ |j XdS)N) rErr@rrflushrrVr,r>r"closer=)r'rEr=rrrrl.s   zGzipFile.closecCs4|j|jtkr0|jj|jj||jjdSr+)r\rr@rErrrk)r'Z zlib_moderrrrkAs zGzipFile.flushcCs |jjS)zInvoke the underlying file object's fileno() method. This will raise AttributeError if the underlying file object doesn't support fileno(). )rEfilenor0rrrrmHszGzipFile.filenocCs"|jtkrtd|jjddS)z[Return the uncompressed stream file position indicator to the beginning of the filezCan't rewind in write moderN)rr>r^r"r-r0rrrrewindPs zGzipFile.rewindcC |jtkSr+)rr>r0rrrreadableWr2zGzipFile.readablecCror+)rr@r0rrrwritableZr2zGzipFile.writablecCr.r/rr0rrrr1]r2zGzipFile.seekablecCs|jtkr|tjkr2|tjkr*|j|}ntd||jkrDtd||j}dd}xt|dD]}|j |qdW|j d|dn |jt kr|j |j j ||S|jS)NzSeek from end not supportedzNegative seek in write moderY)rr@rSEEK_SETZSEEK_CURrWrr^Zrangerr>r\r"r-)r'rWZwhenceZcountchunkZirrrr-`s        z GzipFile.seekcCs|j|jj|Sr+)r\r"readline)r'r,rrrrutszGzipFile.readline)NNr NNrNrNrNrvrNrv)r4r5r6r7r=r*ZpropertyrrGrRrArFrrrerfrirlrBZ Z_SYNC_FLUSHrkrmrnrprqr1rrsr-rurrrrrns. H     csZeZdZfddZddZddZddZdd d Zd d ZddZ fddZ Z S)r?cs,tjt|tjtj dd|_d|_dS)N)ZwbitsT)superr*rrBZ decompressobjrC _new_memberrM)r'ZfpZ __class__rrr*zs z_GzipReader.__init__cCstjd|_d|_dSrT)rBrU_crc _stream_sizer0rrr _init_reads z_GzipReader._init_readcCsJ|jj|}x8t||krD|jj|t|}|s:td||7}qW|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)r'rgr`r rrr _read_exacts  z_GzipReader._read_exactcCs|jjd}|dkrdS|dkr,td|tjd|jd\}}|_|dkrVtd|t@r|tjd |jd\}|j||t@rx |jjd }| s|d krPqW|t @rx |jjd }| s|d krPqW|t @r|jdd S) Nrr FrXzNot a gzipped file (%r)ztjdd}|o|ddk}|r.|dd}|s8dg}x|D]}|r|dkrltddtjjd}tjj}q|d dd krtd t|q@t|d}t j|ddd }n>|dkrtjj}tdd tjjd}nt j|d}t|d d }x |j d }|sP|j |qW|tjjk r"|j |tjjk r@|j q@WdS)Nrrz-dz-rr )rrrErIrJzfilename doesn't end in .gz:rrrrKrK) sysZargvrZstdinZbufferZstdoutZprintrOrr<rrrl)ZargsrZargr(Zgrtrrr_tests<       rZ__main__)rrrrr )rr)r r NNN)r )r7rrr[rrBr<rZ _compressionZ__all__ZFTEXTrrrZrr>r@rrrZ BaseStreamrZDecompressReaderr?rrrr4rrrrZs,   +)  &