B
    -§,cÛ
  ã               @   sL   d Z ddlmZmZmZmZ ddlZddlZddlm	Z	 G dd„ de	ƒZ
dS )z£
gzipmrcfile
-----------

Module which exports the :class:`GzipMrcFile` class.

Classes:
    :class:`GzipMrcFile`: An object which represents a gzipped MRC file.

é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsNé   )ÚMrcFilec                   sR   e Zd ZdZdd„ Zdd„ Zdd„ Zd‡ fd	d
„	Zdd„ Zdd„ Z	dd„ Z
‡  ZS )ÚGzipMrcFilez•:class:`~mrcfile.mrcfile.MrcFile` subclass for handling gzipped files.
    
    Usage is the same as for :class:`~mrcfile.mrcfile.MrcFile`.
    
    c             C   s   d  | jj| j¡S )NzGzipMrcFile('{0}', mode='{1}'))ÚformatÚ_fileobjÚnameÚ_mode)Úself© r   ú2lib/python3.7/site-packages/mrcfile/gzipmrcfile.pyÚ__repr__!   s    
zGzipMrcFile.__repr__c             C   s(   t || jd ƒ| _tj| jdd| _dS )z9Override _open_file() to open both normal and gzip files.ÚbÚrb)ÚfileobjÚmodeN)Úopenr   r
   ÚgzipÚGzipFileÚ	_iostream)r   r   r   r   r   Ú
_open_file%   s    zGzipMrcFile._open_filec             C   s   | j  ¡  | j ¡  dS )z;Override _close_file() to close both normal and gzip files.N)r   Úcloser
   )r   r   r   r   Ú_close_file*   s    
zGzipMrcFile._close_fileFc                s   |   ¡  tt| ƒ |¡ dS )z5Override _read() to ensure gzip file is in read mode.N)Ú_ensure_readable_gzip_streamÚsuperr   Ú_read)r   Zheader_only)Ú	__class__r   r   r   /   s    zGzipMrcFile._readc             C   s:   | j jtjkr6| j  ¡  | j d¡ tj| jdd| _ dS )z6Make sure _iostream is a gzip stream that can be read.r   r   )r   r   N)r   r   r   ZREADr   r
   Úseekr   )r   r   r   r   r   4   s    
z(GzipMrcFile._ensure_readable_gzip_streamc             C   s8   |   ¡  | j ¡ }t| j ¡ ƒ}| j |tj¡ || S )z4Override _get_file_size() to avoid seeking from end.)r   r   ÚtellÚlenÚreadr    ÚosÚSEEK_SET)r   ÚposZextrar   r   r   Ú_get_file_size;   s
    
zGzipMrcFile._get_file_sizec             C   s|   | j sx| j ¡  | j d¡ tj| jdd| _| j | j 	¡ ¡ | j | j
 	¡ ¡ | j | j 	¡ ¡ | j ¡  | j ¡  dS )z|Override :meth:`~mrcfile.mrcinterpreter.MrcInterpreter.flush` since
        GzipFile objects need special handling.
        r   Úwb)r   r   N)Z
_read_onlyr   r   r
   r    r   r   ÚwriteÚheaderÚtobytesZextended_headerÚdataÚflushÚtruncate)r   r   r   r   r-   C   s    

zGzipMrcFile.flush)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r'   r-   Ú__classcell__r   r   )r   r   r      s   r   )r2   Z
__future__r   r   r   r   r   r$   Zmrcfiler   r   r   r   r   r   Ú<module>   s
   