Yf$O@sXdZddlZddlZddlZddlZddlZddlZddlZddlZddddgZ ddd d d f\Z Z Z Z Zddf\ZZd d dddddZddZGdddZGdddejZGdddejZd ddZddZddZedkrTedS)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|krvtd|fnH|dk rFtd|dk r^td|dk rvtd|jdd}t|ttfrt|||}nBt|d st|d rtd|||}n td d|krtj ||||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 isinstancestrbytesrhasattr TypeErrorio TextIOWrapper)filenamemode compresslevelencodingerrorsnewlineZgz_modeZ binary_filer!)/opt/alt/python35/lib64/python3.5/gzip.pyrs$          cCs|jtjd|dS)Nz)rformatbuiltinsr myfileobjgetattrrrr startswithREADr _GzipReaderrBufferedReaderr)r:WRITE _init_writezlibZ compressobjZDEFLATED MAX_WBITSZ DEF_MEM_LEVELr _write_mtimefileobj_write_gzip_header)r.rrrrLmtimerawr!r!r"r1{s<#             zGzipFile.__init__cCsYddl}|jdtd|jtkrR|jdddkrR|jdS|jS)Nrzuse the name attributerz.gz)warningswarnDeprecationWarningrrGr:)r.rRr!r!r"rs  ( zGzipFile.filenamecCs |jjjS)z0Last modification time read from stream, or None)r)rO _last_mtime)r.r!r!r"rNszGzipFile.mtimecCs9t|j}d|dddtt|dS)Nz)reprrLhexid)r.sr!r!r"__repr__szGzipFile.__repr__cCsC||_tjd|_d|_g|_d|_d|_dS)Nr(r)r:rIcrc32crcr2Zwritebufbufsizeoffset)r.rr!r!r"rHs     zGzipFile._init_writec Cs7|jjd|jjdyVtjj|j}t|tsV|jd}|j dru|dd }Wnt k rd}YnXd}|rt }|jjt |jd|j }|dkrtj}t|jt||jjd|jjd |r3|jj|d dS) Nsszlatin-1s.gzrPr(rsssrQ)rLrospathbasenamer:rrencodeendswithUnicodeEncodeErrorFNAMEchrrKtimer&int)r.ZfnameflagsrNr!r!r"rMs,     zGzipFile._write_gzip_headercCs|j|jtkr7ddl}t|jd|jdkrRtdt|t rpt |}nt |}|j }|dkr|jj |jj||j|7_tj||j|_|j|7_|S)Nrz$write() on read-only GzipFile objectz!write() on closed GzipFile object)_check_not_closedrrGerrnoOSErrorEBADFrLrrrr* memoryviewnbytesrrr2rIr^r_ra)r.datarnZlengthr!r!r"rs       zGzipFile.writercCsG|j|jtkr7ddl}t|jd|jj|S)Nrz$read() on write-only GzipFile object)rmrrDrnrorpr)r)r.r2rnr!r!r"r s   z GzipFile.readcCs\|j|jtkr7ddl}t|jd|dkrLtj}|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) rmrrDrnrorprDEFAULT_BUFFER_SIZEr)read1)r.r2rnr!r!r"rus    zGzipFile.read1cCsG|j|jtkr7ddl}t|jd|jj|S)Nrz$peek() on write-only GzipFile object)rmrrDrnrorpr)peek)r.nrnr!r!r"rv!s   z GzipFile.peekcCs |jdkS)N)rL)r.r!r!r"closed(szGzipFile.closedc Cs|j}|dkrdSd|_zl|jtkrq|j|jjt||jt||jd@n|jt kr|j j Wd|j }|rd|_ |j XdS)Nl) rLrrGrrflushr&r_r2rDr)closerA)r.rLrAr!r!r"rz,s     zGzipFile.closecCsF|j|jtkrB|jj|jj||jjdS)N)rmrrGrLrrry)r.Z zlib_moder!r!r"ry?s zGzipFile.flushcCs |jjS)zInvoke the underlying file object's fileno() method. This will raise AttributeError if the underlying file object doesn't support fileno(). )rLfileno)r.r!r!r"r{FszGzipFile.filenocCs/|jtkrtd|jjddS)z[Return the uncompressed stream file position indicator to the beginning of the filezCan't rewind in write moderN)rrDror)r3)r.r!r!r"rewindNs zGzipFile.rewindcCs |jtkS)N)rrD)r.r!r!r"readableUszGzipFile.readablecCs |jtkS)N)rrG)r.r!r!r"writableXszGzipFile.writablecCsdS)NTr!)r.r!r!r"r4[szGzipFile.seekablecCs|jtkr|tjkrI|tjkr=|j|}n td||jkrdtd||j}td}x%t |dD]}|j |qW|j t|dn,|jt kr|j |j j||S|jS)NzSeek from end not supportedzNegative seek in write modei)rrGrSEEK_SETSEEK_CURrarrorrangerrDrmr)r3)r.rawhencecountchunkir!r!r"r3^s      z GzipFile.seekcCs|j|jj|S)N)rmr)readline)r.r2r!r!r"rrs zGzipFile.readlinerXrXrX)r5r6r7r8rAr1propertyrrNr]rHrMrrrurvrxrzrIZ Z_SYNC_FLUSHryr{r|r}r~r4rrr3rr!r!r!r"rns. F           cseZdZfddZddZddZddZdd d Zd d ZddZ fddZ S)rEcs<tjt|tjdtj d|_d|_dS)NZwbitsT)superr1r'rIZ decompressobjrJ _new_memberrU)r.fp) __class__r!r"r1xs  z_GzipReader.__init__cCstjd|_d|_dS)Nr(r)rIr^_crc _stream_size)r.r!r!r" _init_readsz_GzipReader._init_readcCsg|jj|}xNt||krb|jj|t|}|sUtd||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. zACompressed file ended before the end-of-stream marker was reached)_fprr*EOFError)r.rwrsrr!r!r" _read_exacts z_GzipReader._read_exactcCs;|jjd}|dkr"dS|dkr>td|tjd|jd\}}|_|dkr}td|t@rtjd |jd\}|j||t@rx*|jjd }| s|d krPqW|t @r x*|jjd }| s|d krPqW|t @r7|jdd S) Nrr(FszNot a gzipped file (%r)zs(0    $+ )   &