U
    /e                     @  s~   U d dl mZ d dlZd dlmZ d dlmZ d dlmZmZm	Z	 erRd dl
mZ e	d Zded	< G d
d deeef ZdS )    )annotationsN)Iterator)MutableMapping)TYPE_CHECKINGAnyLiteral)	TypeAlias)rwxar   FileModec                      s   e Zd ZU dZded< ded< ded< d0dd	d
 fddZeddddZdddddZddddddZ	ddddZ
ddddd Zdddd!d"Zd#dd$d%Zddd&d'Zddd(d)Zd dd*d+Zd,dd-d.d/Z  ZS )1Zipa9  Mutable Mapping interface to a Zip file

    Keys must be strings, values must be bytes

    Parameters
    ----------
    filename: string
    mode: string, ('r', 'w', 'a'), defaults to 'a'

    Notes
    -----
    None of this class is thread-safe - not even normally trivial methods such as
    ``__len__ `` or ``__contains__``.

    Examples
    --------
    >>> z = Zip('myfile.zip')  # doctest: +SKIP
    >>> z['x'] = b'123'  # doctest: +SKIP
    >>> z['x']  # doctest: +SKIP
    b'123'
    >>> z.flush()  # flush and write metadata to disk  # doctest: +SKIP
    strfilenamezFileMode | Literal['closed']modezzipfile.ZipFile | None_filer   r   )r   r   c                   s    t    || _|| _d | _d S N)super__init__r   r   r   )selfr   r   	__class__ ,/tmp/pip-unpacked-wheel-z3s6s24u/zict/zip.pyr   +   s    
zZip.__init__zzipfile.ZipFile)returnc                 C  s:   | j dkrtd| jr | jjs4tj| j| j d| _| jS )NclosedzFile closed)r   )r   OSErrorr   fpzipfileZipFiler   r   r   r   r   file1   s
    
zZip.filebytes)keyr   c                 C  s   t |tst|| j|S r   )
isinstancer   KeyErrorr"   readr   r$   r   r   r   __getitem__9   s    
zZip.__getitem__zbytes | bytearray | memoryviewNone)r$   valuer   c                 C  sL   t |tst|t |tttfs*t||| kr:td| j|| d S NzNot supported by stdlib zipfile)	r%   r   	TypeErrorr#   	bytearray
memoryviewNotImplementedErrorr"   writestr)r   r$   r+   r   r   r   __setitem__>   s    
zZip.__setitem__zIterator[str]c                 C  s   dd | j jD S )Nc                 s  s   | ]}|j V  qd S r   )r   ).0Zzir   r   r   	<genexpr>H   s     zZip.__iter__.<locals>.<genexpr>)r"   filelistr!   r   r   r   __iter__G   s    zZip.__iter__objectboolc                 C  s<   t |tsdS z| j| W dS  tk
r6   Y dS X d S )NFT)r%   r   r"   getinfor&   r(   r   r   r   __contains__J   s    
zZip.__contains__c                 C  s   t dd S r,   )r0   r(   r   r   r   __delitem__S   s    zZip.__delitem__intc                 C  s   t | jjS r   )lenr"   r5   r!   r   r   r   __len__V   s    zZip.__len__c                 C  s.   | j r*| j jr| j j  | j   d | _ d S r   )r   r   flushcloser!   r   r   r   r?   Y   s
    
z	Zip.flushc                 C  s   |    d| _d S )Nr   )r?   r   r!   r   r   r   r@   `   s    z	Zip.closec                 C  s   | S r   r   r!   r   r   r   	__enter__d   s    zZip.__enter__r   )argsr   c                 G  s   |    d S r   )r@   )r   rB   r   r   r   __exit__g   s    zZip.__exit__)r   )__name__
__module____qualname____doc____annotations__r   propertyr"   r)   r2   r6   r:   r;   r>   r?   r@   rA   rC   __classcell__r   r   r   r   r      s"   
		r   )
__future__r   r   collections.abcr   typingr   r   r   r   Ztyping_extensionsr   r   rH   r   r#   r   r   r   r   r   <module>   s    