B
    u9az0                 @   s   d dddddgZ dZddlmZ dd	lZdd	lZdd	lZdd	lZdd
l	m
Z
 ddlmZmZ dZdZdZG dd  d ejZdddZdddZdd Zd	S )BZ2FileBZ2CompressorBZ2Decompressoropencompress
decompressz%Nadeem Vawda <nadeem.vawda@gmail.com>    )r   N)RLock)r   r         c               @   s   e Zd Zd(ddZdd Zedd	 Zd
d Zdd Zdd Z	dd Z
d)ddZd*ddZd+ddZdd Zd,ddZd-ddZd d! Zd"d# Zejfd$d%Zd&d' ZdS ).r   rN	   c             C   sP  t  | _d | _d| _t| _|d k	r.tdt d|  krBdksLn t	d|dkr^d}t
}nb|dkrzd	}t}t|| _nF|d
krd}t}t|| _n*|dkrd}t}t|| _nt	d|f t|tttjfrt||| _d| _|| _n.t|dst|dr|| _|| _ntd| jt
krFtj| jttd}t|| _nd| _d S )NFz)Use of 'buffering' argument is deprecatedr	   r   z%compresslevel must be between 1 and 9) r   rbr   )wwbr   )xxbr   )aabr   zInvalid mode: %rTreadwritez6filename must be a str, bytes, file or PathLike object)Ztrailing_errorr   )r   _lock_fp_closefp_MODE_CLOSED_modewarningswarnDeprecationWarning
ValueError
_MODE_READ_MODE_WRITEr   _compressor
isinstancestrbytesosPathLike_builtin_openhasattr	TypeError_compressionZDecompressReaderr   OSErrorioBufferedReader_buffer_pos)selffilenamemode	bufferingcompresslevelZ	mode_coderaw r7   /usr/lib/python3.7/bz2.py__init__'   sL    
zBZ2File.__init__c             C   s   | j  | jtkrd S z<| jtkr.| j  n"| jtkrP| j| j	
  d | _	W d z| jrf| j  W d d | _d| _t| _d | _X X W d Q R X d S )NF)r   r   r   r    r/   closer!   r   r   r"   flushr   )r1   r7   r7   r8   r:   l   s     



zBZ2File.closec             C   s
   | j tkS )N)r   r   )r1   r7   r7   r8   closed   s    zBZ2File.closedc             C   s   |    | j S )N)_check_not_closedr   fileno)r1   r7   r7   r8   r>      s    zBZ2File.filenoc             C   s   |   o| j S )N)readabler/   seekable)r1   r7   r7   r8   r@      s    zBZ2File.seekablec             C   s   |    | jtkS )N)r=   r   r    )r1   r7   r7   r8   r?      s    zBZ2File.readablec             C   s   |    | jtkS )N)r=   r   r!   )r1   r7   r7   r8   writable   s    zBZ2File.writabler   c          	   C   s&   | j  |   | j|S Q R X d S )N)r   _check_can_readr/   peek)r1   nr7   r7   r8   rC      s    zBZ2File.peekc          	   C   s&   | j  |   | j|S Q R X d S )N)r   rB   r/   r   )r1   sizer7   r7   r8   r      s    zBZ2File.readc          	   C   s4   | j $ |   |dk rtj}| j|S Q R X d S )Nr   )r   rB   r-   DEFAULT_BUFFER_SIZEr/   read1)r1   rF   r7   r7   r8   rH      s
    zBZ2File.read1c          	   C   s&   | j  |   | j|S Q R X d S )N)r   rB   r/   readinto)r1   br7   r7   r8   rI      s    zBZ2File.readintoc          	   C   sJ   t |ts$t|dstd| }| j |   | j|S Q R X d S )N	__index__zInteger argument expected)	r#   intr)   r*   rK   r   rB   r/   readline)r1   rF   r7   r7   r8   rM      s    

zBZ2File.readlinec          	   C   sJ   t |ts$t|dstd| }| j |   | j|S Q R X d S )NrK   zInteger argument expected)	r#   rL   r)   r*   rK   r   rB   r/   	readlines)r1   rF   r7   r7   r8   rN      s    

zBZ2File.readlinesc          	   C   sL   | j < |   | j|}| j| |  jt|7  _t|S Q R X d S )N)r   Z_check_can_writer"   r   r   r   r0   len)r1   dataZ
compressedr7   r7   r8   r      s    zBZ2File.writec          	   C   s    | j  tj| |S Q R X d S )N)r   r+   
BaseStream
writelines)r1   seqr7   r7   r8   rR      s    zBZ2File.writelinesc          	   C   s(   | j  |   | j||S Q R X d S )N)r   Z_check_can_seekr/   seek)r1   offsetwhencer7   r7   r8   rT     s    zBZ2File.seekc          	   C   s4   | j $ |   | jtkr$| j S | jS Q R X d S )N)r   r=   r   r    r/   tellr0   )r1   r7   r7   r8   rW     s
    

zBZ2File.tell)r   Nr   )r   )rE   )rE   )rE   )rE   )__name__
__module____qualname__r9   r:   propertyr<   r>   r@   r?   rA   rC   r   rH   rI   rM   rN   r   rR   r-   SEEK_SETrT   rW   r7   r7   r7   r8   r      s"   
E



	

r   r   c             C   s   d|kr d|krPt d|f n0|d k	r0t d|d k	r@t d|d k	rPt d|dd}t| ||d}d|krt||||S |S d S )	NtrJ   zInvalid mode: %rz0Argument 'encoding' not supported in binary modez.Argument 'errors' not supported in binary modez/Argument 'newline' not supported in binary moder   )r5   )r   replacer   r-   TextIOWrapper)r2   r3   r5   encodingerrorsnewlineZbz_modeZbinary_filer7   r7   r8   r     s    c             C   s   t |}|| |  S )N)r   r   r;   )rP   r5   compr7   r7   r8   r   F  s    c             C   sj   g }xZ| r^t  }y|| }W n tk
r<   |r6P n Y nX || |jsVtd|j} qW d|S )NzACompressed data ended before the end-of-stream marker was reached    )r   r   r,   appendeofr   Zunused_datajoin)rP   resultsZdecompZresr7   r7   r8   r   Q  s    

)r   r   NNN)r   )__all__
__author__builtinsr   r(   r-   r&   r   r+   Z	threadingr   Z_bz2r   r   r   r    r!   rQ   r   r   r   r7   r7   r7   r8   <module>   s&   
   
(
