[^@s6dZddlZddlZddlZddlZddlZddlZddlZddddgZddd d d f\Z Z Z Z Z ddf\ZZd d dddddZddZGdddZGdddejZd ddZddZddZedkr2endS)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 cCs*d|kr1d|krtd|fqnQ|dk rLtdn|dk rgtdn|dk rtdn|jdd}t|ttfrt|||}nBt|d st|d rtd|||}n td d|kr"tj ||||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!/usr/lib/python3.4/gzip.pyrs$       cCs|jtjd|dS)Nz)rSrr'r,reprhexid)r.rSsr!r!r"__repr__s    zGzipFile.__repr__cCs|jrtdndS)zLRaises a ValueError if the underlying file object has been closed. zI/O operation on closed file.N)closedr)r.r!r!r" _check_closeds zGzipFile._check_closedcCs>||_tjdd@|_d|_g|_d|_dS)Nr(lr)r8rQcrc32crcr2Zwritebufbufsize)r.rr!r!r"rPs    zGzipFile._init_writec CsF|jjd|jjdy\tjj|j}t|tsY|jd}n|j dr{|dd }nWnt k rd}YnXd}|rt }n|jjt |jd|j }|dkrtj}nt|jt||jjd|jjd |rB|jj|d ndS) Nsszlatin-1s.gzrVr(rsssrW)rSrospathbasenamer8rrencodeendswithUnicodeEncodeErrorFNAMEchrrTtimer&int)r.ZfnameflagsrTr!r!r"rUs,     zGzipFile._write_gzip_headercCs#tjdd@|_d|_dS)Nr(lr)rQrerfr2)r.r!r!r" _init_readszGzipFile._init_readcCsj|jj|}xQt||kre|jj|t|}|sXtdn||7}qW|S)NzACompressed file ended before the end-of-stream marker was reached)rSrr*EOFError)r.ndatarr!r!r" _read_exactszGzipFile._read_exactcCs|jjd}|dkr"dS|dkr=tdntjd|jd\}}|_|dkrtdn|t@rtjd |jd\}|j|n|t@rx0|jjd }| s|d krPqqWn|t @r1x0|jjd }| s&|d krPqqWn|t @rK|jdn|jj }|r|j j |}|j |nd S) Nrr(FszNot a gzipped filez U      %   ,       c CsGtj}td|ddd|}|j|WdQX|jS)zCompress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9. rSrwbrN)rBytesIOrrgetvalue)rvrrr/r!r!r"rjs cCs/tdtj|}|jSWdQXdS)zYDecompress a gzip compressed string in one shot. Return the decompressed string. rSN)rrrr)rvr/r!r!r"rsscCstjdd}|o&|ddk}|rB|dd}n|sTdg}nxy|D]q}|r|dkrtddddd tjj}tjj}qa|ddd krtd t|q[nt|d}t j|ddd }na|dkr<tjj}tdddd d tjj}n%t j|d}t|d d }x*|j d}|s}Pn|j |qdW|tjjk r|j n|tjjk r[|j q[q[WdS)Nrrz-d-rrrr rSrVz.gzzfilename doesn't end in .gz:rirWrW) rargvrstdinbufferstdoutprintr^rrFrrr)argsrargr/grr!r!r"_test{s<   !  $ r__main__)r=r#rrprhrQrFr__all__ZFTEXTr|rzrnr{rIrOrr&r'BufferedIOBaserrrrr:r!r!r!r"s$0   $+ 8  &