[I@sdZddddddgZdZdd lZdd lZydd lmZWn"ek rtdd lmZYnXdd l m Z m Z dZ d Z d ZdZdZeZGdddejZddd d d ddZdddZddZd S)zInterface to the libbzip2 compression library. This module provides a file interface, classes for incremental (de)compression, and functions for one-shot (de)compression. BZ2File BZ2CompressorBZ2Decompressoropencompress decompressz%Nadeem Vawda N)RLock)rri c@sieZdZdZdddddZddZed d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZdddZdd d!Zd"d#d$Zd:d&d'Zd;d(d)Zd*d+Zd<d,d-Zd=d.d/Zd0d1Zd2d3Zd4d5Zd"d6d7Zd8d9ZdS)>ra@A file object providing transparent bzip2 (de)compression. A BZ2File can act as a wrapper for an existing file object, or refer directly to a named file on disk. Note that BZ2File provides a *binary* file interface - data read is returned as bytes, and data to be written should be given as bytes. rN cCst|_d|_d|_t|_d|_d|_|dk rXtj dt nd|koodknst dn|dkrd }t }t |_d |_d|_n|dkrd }t}t||_ng|dkrd}t}t||_n=|dkr:d}t}t||_nt d|ft|ttfrt|||_d|_||_n?t|dst|dr||_||_n tddS)a3Open a bzip2-compressed file. If filename is a str or bytes object, it gives the name of the file to be opened. Otherwise, it should be a file object, which will be used to read or write the compressed data. mode can be 'r' for reading (default), 'w' for (over)writing, 'x' for creating exclusively, or 'a' for appending. These can equivalently be given as 'rb', 'wb', 'xb', and 'ab'. buffering is ignored. Its use is deprecated. If mode is 'w', 'x' or 'a', compresslevel can be a number between 1 and 9 specifying the level of compression: 1 produces the least compression, and 9 (default) produces the most compression. If mode is 'r', the input file may be the concatenation of multiple compressed streams. NFrr z)Use of 'buffering' argument is deprecatedr z%compresslevel must be between 1 and 9r rbwwbxxbaabzInvalid mode: %rTreadwritez1filename must be a str or bytes object, or a file)rr zrb)rzwb)rr)rr)r_lock_fp_closefp _MODE_CLOSED_mode_pos_sizewarningswarnDeprecationWarning ValueError _MODE_READr _decompressor_buffer_buffer_offset _MODE_WRITEr _compressor isinstancestrbytes _builtin_openhasattr TypeError)selffilenamemode buffering compresslevelZ mode_coder6/usr/lib/python3.4/bz2.py__init__,sL                    zBZ2File.__init__cCs|j|jtkrdSzY|jttfkrAd|_n4|jtkru|jj|j j d|_ nWdz|j r|jj nWdd|_d|_ t|_d|_ d|_XXWdQXdS)zFlush and close the file. May be called more than once without error. Once the file is closed, any other operation on it will raise a ValueError. NFrr)rrrr%_MODE_READ_EOFr&r)rrr*flushrcloser'r()r1r6r6r7r;os"       z BZ2File.closecCs |jtkS)zTrue if this file is closed.)rr)r1r6r6r7closedszBZ2File.closedcCs|j|jjS)z3Return the file descriptor for the underlying file.)_check_not_closedrfileno)r1r6r6r7r>s zBZ2File.filenocCs|jo|jjS)z)Return whether the file supports seeking.)readablerseekable)r1r6r6r7r@szBZ2File.seekablecCs|j|jttfkS)z/Return whether the file was opened for reading.)r=rr%r9)r1r6r6r7r?s zBZ2File.readablecCs|j|jtkS)z/Return whether the file was opened for writing.)r=rr))r1r6r6r7writables zBZ2File.writablecCs|jrtdndS)NzI/O operation on closed file)r<r$)r1r6r6r7r=s zBZ2File._check_not_closedcCs5|jttfkr1|jtjdndS)NzFile not open for reading)rr%r9r=ioUnsupportedOperation)r1r6r6r7_check_can_reads zBZ2File._check_can_readcCs/|jtkr+|jtjdndS)NzFile not open for writing)rr)r=rBrC)r1r6r6r7_check_can_writes zBZ2File._check_can_writecCsV|jttfkr1|jtjdn|jjsRtjdndS)Nz3Seeking is only supported on files open for readingz3The underlying file object does not support seeking)rr%r9r=rBrCrr@)r1r6r6r7_check_can_seeks  zBZ2File._check_can_seekc Cs|jtkrdSx|jt|jkr |jjpI|jjt }|s|jj rwt|_|j |_ dSt dn|jj rt|_y|jj||_Wqtk rt|_|j |_ dSYqXn|jj||_d|_qWdS)NFzACompressed file ended before the end-of-stream marker was reachedrT)rr9r(lenr'r& unused_datarr _BUFFER_SIZEeofrr EOFErrorrrOSError)r1Zrawblockr6r6r7 _fill_buffers,           zBZ2File._fill_bufferTcCs|j|jd|_d|_g}xJ|jrt|rP|j|jn|jt|j7_d|_q+W|rdj|SdS)Nrr)r'r(rMappendrrGjoin)r1 return_datablocksr6r6r7 _read_alls  zBZ2File._read_allcCsC|j|}|t|jkrd|j|j|}||_|jt|7_|r`|SdS|j|jd|_d|_g}x|dkr+|jr+|t|jkr|jd|}||_n|j}d|_|r|j|n|jt|7_|t|8}qW|r?dj|SdS)Nrr)r(rGr'rrMrNrO)r1nrPenddatarQr6r6r7 _read_blocks*      zBZ2File._read_blockrc CsB|j3|j|js$dS|j|jdSWdQXdS)zReturn buffered data without advancing the file position. Always returns at least one byte of data, unless at EOF. The exact number of bytes returned is unspecified. rN)rrDrMr'r()r1rSr6r6r7peek s    z BZ2File.peekr c CsQ|jB|j|dkr$dS|dkr:|jS|j|SWdQXdS)zRead up to size uncompressed bytes from the file. If size is negative or omitted, read until EOF is reached. Returns b'' if the file is already at EOF. rrN)rrDrRrV)r1sizer6r6r7rs     z BZ2File.readc Cs|j|j|dksE|jt|jkrI|j rIdS|dkr|j|j|j|}|jt|7_n(|j|jd}d|_d|_|jt|7_|SWdQXdS)zRead up to size uncompressed bytes, while trying to avoid making multiple reads from the underlying stream. Returns b'' if the file is at EOF. rrN)rrDr(rGr'rMr)r1rXrUr6r6r7read1's   %    z BZ2File.read1c Cs'|jtjj||SWdQXdS)z_Read up to len(b) bytes into b. Returns the number of bytes read (0 for EOF). N)rrBBufferedIOBasereadinto)r1br6r6r7r[Bs zBZ2File.readintoc Cst|ts<t|ds-tdn|j}n|j|j|dkr|jjd|j d}|dkr|j|j |}||_ |j t |7_ |Snt j j||SWdQXdS)a Read a line of uncompressed bytes from the file. The terminating newline (if present) is retained. If size is non-negative, no more than size bytes will be read (in which case the line may be incomplete). Returns b'' if already at EOF. __index__zInteger argument expectedrs r N)r+intr/r0r]rrDr'findr(rrGrBrZreadline)r1rXrTliner6r6r7r`Js     zBZ2File.readlinec Csct|ts<t|ds-tdn|j}n|jtjj||SWdQXdS)zRead a list of lines of uncompressed bytes from the file. size can be specified to control the number of lines read: no further lines will be read once the total size of the lines read so far equals or exceeds size. r]zInteger argument expectedN) r+r^r/r0r]rrBrZ readlines)r1rXr6r6r7rbas  zBZ2File.readlinesc Cs_|jP|j|jj|}|jj||jt|7_t|SWdQXdS)zWrite a byte string to the file. Returns the number of uncompressed bytes written, which is always len(data). Note that due to buffering, the file on disk may not reflect the data written until close() is called. N)rrEr*rrrrrG)r1rUZ compressedr6r6r7ros   z BZ2File.writec Cs'|jtjj||SWdQXdS)zWrite a sequence of byte strings to the file. Returns the number of uncompressed bytes written. seq can be any iterable yielding byte strings. Line separators are not added between the written byte strings. N)rrBrZ writelines)r1seqr6r6r7rc}s zBZ2File.writelinescCsG|jjddt|_d|_t|_d|_d|_dS)Nrr) rseekr%rrrr&r'r()r1r6r6r7_rewinds     zBZ2File._rewindc Cs|j|j|dkr#nm|dkr?|j|}nQ|dkr}|jdkrm|jddn|j|}ntd|f||jkr|jn ||j8}|j|dd|jSWdQXdS)aChange the file position. The new position is specified by offset, relative to the position indicated by whence. Values for whence are: 0: start of stream (default); offset must not be negative 1: current stream position 2: end of stream; offset must not be positive Returns the new file position. Note that seeking is emulated, so depending on the parameters, this operation may be extremely slow. rr r rPFzInvalid value for whence: %sN)rrFrr rRr$rfrV)r1offsetwhencer6r6r7res        z BZ2File.seekcCs%|j|j|jSWdQXdS)z!Return the current file position.N)rr=r)r1r6r6r7tells  z BZ2File.tellrrrr)__name__ __module__ __qualname____doc__r8r;propertyr<r>r@r?rAr=rDrErFrMrRrVrWrrYr[r`rbrrcrfrerir6r6r6r7r!s4 C         %    *rr cCsd|kr1d|krtd|fqnQ|dk rLtdn|dk rgtdn|dk rtdn|jdd}t||d |}d|krtj||||S|SdS) a Open a bzip2-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 BZ2File constructor: BZ2File(filename, mode, compresslevel). In this case, the encoding, errors and newline arguments must not be provided. For text mode, a BZ2File object is created, and wrapped in an io.TextIOWrapper instance with the specified encoding, error handling behavior, and line ending(s). tr\zInvalid mode: %rNz0Argument 'encoding' not supported in binary modez.Argument 'errors' not supported in binary modez/Argument 'newline' not supported in binary moderr5)r$replacerrB TextIOWrapper)r2r3r5encodingerrorsnewlineZbz_modeZ binary_filer6r6r7rs      cCs#t|}|j||jS)zCompress a block of data. compresslevel, if given, must be a number between 1 and 9. For incremental compression, use a BZ2Compressor object instead. )rrr:)rUr5compr6r6r7rs c Csg}xv|r~t}y|j|}Wntk rL|rEPnYnX|j||jsrtdn|j}q Wdj|S)zjDecompress a block of data. For incremental decompression, use a BZ2Decompressor object instead. zACompressed data ended before the end-of-stream marker was reachedr)rrrLrNrJr$rHrO)rUZresultsZdecompZresr6r6r7rs      )rm__all__ __author__rBr!Z threadingr ImportErrorZdummy_threadingZ_bz2rrrr%r9r)rIrr.rZrrrr6r6r6r7s,    '