U
    Z$dn
                     @   s,   d Z ddlZddlmZ G dd deZdS )a;  Read/Write NPZ files.

Backend: `Numpy <https://numpy.org/doc/stable/reference/generated/numpy.savez.html>`_

NPZ is a file format by numpy that provides storage of array data using gzip
compression. This imageio plugin supports data of any shape, and also supports
multiple images per file. However, the npz format does not provide streaming;
all data is read/written at once. Further, there is no support for meta data.

See the BSDF format for a similar (but more fully featured) format.

Parameters
----------
None

Notes
-----
This format is not available on Pypy.

    N   )Formatc                   @   sD   e Zd ZdZdd Zdd ZG dd dejZG dd	 d	ejZd
S )	NpzFormatzSee :mod:`imageio.plugins.npz`c                 C   s   |j | jkS N	extension
extensionsselfrequest r   7/tmp/pip-unpacked-wheel-zwww1ayp/imageio/plugins/npz.py	_can_read!   s    zNpzFormat._can_readc                 C   s   |j | jkS r   r   r	   r   r   r   
_can_write%   s    zNpzFormat._can_writec                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )zNpzFormat.Readerc                 C   sB   t | j | _t| jt jjjs(t	t
| jjdd d| _d S )Nc                 S   s   |  dd S )N_)split)xr   r   r   <lambda>1       z(NpzFormat.Reader._open.<locals>.<lambda>)key)nploadr   get_file_npz
isinstancelibZnpyioZNpzFileAssertionErrorsortedfiles_namesr
   r   r   r   _open,   s    zNpzFormat.Reader._openc                 C   s   | j   d S r   )r   closer!   r   r   r   _close3   s    zNpzFormat.Reader._closec                 C   s
   t | jS r   )lenr    r!   r   r   r   _get_length6   s    zNpzFormat.Reader._get_lengthc                 C   s6   |dk s|t | jkrtd| j| j|  }|i fS )Nr   z)Index out of range while reading from nzp)r%   r    
IndexErrorr   )r
   indeximr   r   r   	_get_data9   s    zNpzFormat.Reader._get_datac                 C   s   t dd S Nz*The npz format does not support meta data.RuntimeError)r
   r(   r   r   r   _get_meta_dataA   s    zNpzFormat.Reader._get_meta_dataN)__name__
__module____qualname__r"   r$   r&   r*   r.   r   r   r   r   Reader+   s
   r2   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
zNpzFormat.Writerc                 C   s
   g | _ d S r   )_imagesr!   r   r   r   r"   H   s    zNpzFormat.Writer._openc                 C   s   t j| j f| j  d S r   )r   Zsavez_compressedr   r   r3   r!   r   r   r   r$   M   s    zNpzFormat.Writer._closec                 C   s   | j | d S r   )r3   append)r
   r)   metar   r   r   _append_dataQ   s    zNpzFormat.Writer._append_datac                 C   s   t dd S r+   r,   )r
   r5   r   r   r   set_meta_dataT   s    zNpzFormat.Writer.set_meta_dataN)r/   r0   r1   r"   r$   r6   r7   r   r   r   r   WriterG   s   r8   N)	r/   r0   r1   __doc__r   r   r   r2   r8   r   r   r   r   r      s
   r   )r9   Znumpyr   corer   r   r   r   r   r   <module>   s   