a DOg0@sdZgdZdZddlmZddlZddlZddlZddl m Z ddl m Z m Z dZdZd ZGd d d ejZdddZdddZddZdS)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 )rN)RLock)rrc@seZdZdZd*ddddZddZed d Zd d Zd dZ ddZ ddZ d+ddZ d,ddZ d-ddZddZd.ddZd/dd Zd!d"Zd#d$Zejfd%d&Zd'd(Zd)S)0ra@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. r  compresslevelcCs8t|_d|_d|_t|_d|kr.dks8ntd|dvrJd}t}nb|dvrfd }t}t ||_ nF|d vrd }t}t ||_ n*|d vrd }t}t ||_ ntd|ft |t t tjfrt|||_d|_||_n*t|dst|dr||_||_ntd|jtkr.tj|jttd}t||_nd|_dS)aOpen a bzip2-compressed file. If filename is a str, bytes, or PathLike 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'. 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. NFr r z%compresslevel must be between 1 and 9)r rbr)wwbr)xxbr)aabrInvalid mode: %rTreadwritez6filename must be a str, bytes, file or PathLike object)Ztrailing_errorr)r_lock_fp_closefp _MODE_CLOSED_mode ValueError _MODE_READ _MODE_WRITEr _compressor isinstancestrbytesosPathLike _builtin_openhasattr TypeError _compressionZDecompressReaderrOSErrorioBufferedReader_buffer_pos)selffilenamemoderZ mode_coderawr5(/opt/alt/python39/lib64/python3.9/bz2.py__init__&sH     zBZ2File.__init__cCs|j|jtkr"WddSz|jtkr:|jn"|jtkr\|j|j d|_ Wz,|j rp|jWd|_d|_ t|_d|_qd|_d|_ t|_d|_0nJz,|j r|jWd|_d|_ t|_d|_nd|_d|_ t|_d|_00Wdn1s0YdS)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. NF) rrrr r/closer!rrr"flushrr1r5r5r6r8es>       z BZ2File.closecCs |jtkS)zTrue if this file is closed.)rrr:r5r5r6closed~szBZ2File.closedcCs||jS)z3Return the file descriptor for the underlying file.)_check_not_closedrfilenor:r5r5r6r=szBZ2File.filenocCs|o|jS)z)Return whether the file supports seeking.)readabler/seekabler:r5r5r6r?szBZ2File.seekablecCs||jtkS)z/Return whether the file was opened for reading.)r<rr r:r5r5r6r>szBZ2File.readablecCs||jtkS)z/Return whether the file was opened for writing.)r<rr!r:r5r5r6writableszBZ2File.writablercCs>|j$||j|WdS1s00YdS)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. N)r_check_can_readr/peek)r1nr5r5r6rBsz BZ2File.peekcCs>|j$||j|WdS1s00YdS)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. N)rrAr/rr1sizer5r5r6rsz BZ2File.readcCsL|j2||dkrtj}|j|WdS1s>0YdS)zRead up to size uncompressed bytes, while trying to avoid making multiple reads from the underlying stream. Reads up to a buffer's worth of data if size is negative. Returns b'' if the file is at EOF. rN)rrAr-DEFAULT_BUFFER_SIZEr/read1rEr5r5r6rHs z BZ2File.read1cCs>|j$||j|WdS1s00YdS)zRRead bytes into b. Returns the number of bytes read (0 for EOF). N)rrAr/readinto)r1br5r5r6rIszBZ2File.readintocCsbt|ts$t|dstd|}|j$||j|WdS1sT0YdS)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__Integer argument expectedN) r#intr)r*rKrrAr/readlinerEr5r5r6rNs  zBZ2File.readlinecCsbt|ts$t|dstd|}|j$||j|WdS1sT0YdS)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. rKrLN) r#rMr)r*rKrrAr/ readlinesrEr5r5r6rOs  zBZ2File.readlinescCs|jh|t|ttfr(t|}nt|}|j}|j |}|j ||j |7_ |WdS1st0YdS)aWrite a byte string to the file. Returns the number of uncompressed bytes written, which is always the length of data in bytes. Note that due to buffering, the file on disk may not reflect the data written until close() is called. N) rZ_check_can_writer#r% bytearraylen memoryviewnbytesr"rrrr0)r1dataZlengthZ compressedr5r5r6rs   z BZ2File.writecCs8|jtj||WdS1s*0YdS)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)rr+ BaseStream writelines)r1seqr5r5r6rVszBZ2File.writelinescCs@|j&||j||WdS1s20YdS)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. N)rZ_check_can_seekr/seek)r1offsetwhencer5r5r6rXsz BZ2File.seekcCsZ|j@||jtkr2|jWdS|jWdS1sL0YdS)z!Return the current file position.N)rr<rr r/tellr0r:r5r5r6r[s  z BZ2File.tellN)r )r)rD)rD)rD)rD)__name__ __module__ __qualname____doc__r7r8propertyr;r=r?r>r@rBrrHrIrNrOrrVr-SEEK_SETrXr[r5r5r5r6rs& ?     rrr cCsd|vr d|vrPtd|fn0|dur0td|dur@td|durPtd|dd}t|||d }d|vrt||||S|SdS) aOpen a bzip2-compressed file in binary or text mode. The filename argument can be an actual filename (a str, bytes, or PathLike 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). trJrNz0Argument 'encoding' not supported in binary modez.Argument 'errors' not supported in binary modez/Argument 'newline' not supported in binary moderr )rreplacerr- TextIOWrapper)r2r3rencodingerrorsnewlineZbz_modeZ binary_filer5r5r6rs rcCst|}|||S)zCompress a block of data. compresslevel, if given, must be a number between 1 and 9. For incremental compression, use a BZ2Compressor object instead. )rrr9)rTrcompr5r5r6rGsrcCsfg}|r\t}z||}Wnty:|r4Yq\nYn0|||jsTtd|j}qd|S)zjDecompress a block of data. For incremental decompression, use a BZ2Decompressor object instead. zACompressed data ended before the end-of-stream marker was reached)rrr,appendeofrZ unused_datajoin)rTZresultsZdecompresr5r5r6rRs  r)rr NNN)r )r___all__ __author__builtinsrr(r-r&r+Z threadingrZ_bz2rrrr r!rUrrrr5r5r5r6s&   )