U
    Hd                     @   sf   d dl Z d dlZd dlmZ d dlmZ d dlmZ G dd de jZ	G dd dZ
G d	d
 d
ZdS )    NGzipFile)Optional)to_bytesc                       sF   e Zd ZdZejf fdd	Zedd Ze	e
e dddZ  ZS )	GzipCompressionWrapperz1Wrapper for compressing file contents on the fly.c                    s,   t    || _tj|dd| _t | _d S )N   )levelwbits)super__init__rawzlibcompressobjcompress	bytearrayleftover)selfr   r   	__class__ 5/tmp/pip-unpacked-wheel-1m3lyvgh/storages/compress.pyr      s    
zGzipCompressionWrapper.__init__c                   C   s   dS )NTr   r   r   r   r   readable   s    zGzipCompressionWrapper.readable)bufreturnc                 C   s   t |}t | j|k rht| j|}|sP| jrh|  j| jtj7  _d | _qh|  j| j|7  _qt | jdkrzdS | jd | }t |}||d |< | j|d  | _|S )Nr   )	lenr   r   r   readr   flushr   Z_FINISH)r   r   sizechunkoutputr   r   r   readinto   s     zGzipCompressionWrapper.readinto)__name__
__module____qualname____doc__r   Z_BEST_COMPRESSIONr   staticmethodr   r   r   intr!   __classcell__r   r   r   r   r   	   s
   
r   c                   @   s   e Zd Zdd ZdS )CompressStorageMixinc                 C   s   t |S )zGzip a given string content.)r   )r   contentr   r   r   _compress_content*   s    z&CompressStorageMixin._compress_contentN)r"   r#   r$   r,   r   r   r   r   r*   )   s   r*   c                   @   s   e Zd ZdddZdS )CompressedFileMixin        c                 C   s   t |||dS )N)modefileobjmtimer   )r   r/   filer1   r   r   r   _decompress_file0   s    z$CompressedFileMixin._decompress_fileN)r.   )r"   r#   r$   r3   r   r   r   r   r-   /   s   r-   )ior   gzipr   typingr   Zstorages.utilsr   	RawIOBaser   r*   r-   r   r   r   r   <module>   s    