
    [e|                        d dl m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 dl
mZmZ  G d deeef                   ZdS )    )annotationsN)Iterator)quoteunquote)ZictBaselockedc                       e Zd ZU dZded<   ded<   ded<   ded	<   dd fdZd dZed d            Zd!dZ	e	Z
ed"d            Zed#d            Zd$dZd%dZed&d            Zd'dZ xZS )(Filea  Mutable Mapping interface to a directory

    Keys must be strings, values must be buffers

    Note this shouldn't be used for interprocess persistence, as keys
    are cached in memory.

    Parameters
    ----------
    directory: str
        Directory to write to. If it already exists, existing files will be imported as
        mapping elements. If it doesn't exists, it will be created.
    memmap: bool (optional)
        If True, use `mmap` for reading. Defaults to False.

    Notes
    -----
    If you call methods of this class from multiple threads, access will be fast as long
    as atomic disk access such as ``open``, ``os.fstat``, and ``os.remove`` is fast.
    This is not always the case, e.g. in case of slow network mounts or spun-down
    magnetic drives.
    Bytes read/write in the files is not protected by locks; this could cause failures
    on Windows, NFS, and in general whenever it's not OK to delete a file while there
    are file descriptors open on it.

    Examples
    --------
    >>> z = File('myfile')  # doctest: +SKIP
    >>> z['x'] = b'123'  # doctest: +SKIP
    >>> z['x']  # doctest: +SKIP
    b'123'

    Also supports writing lists of bytes objects

    >>> z['y'] = [b'123', b'4567']  # doctest: +SKIP
    >>> z['y']  # doctest: +SKIP
    b'1234567'

    Or anything that can be used with file.write, like a memoryview

    >>> z['data'] = np.ones(5).data  # doctest: +SKIP
    str	directoryboolmemmapzdict[str, str]	filenamesint_incFstr | pathlib.Pathc                   t                                                       t          |          | _        || _        i | _        d| _        t          j        	                    | j                  st          j
        | j        d           d S t          j        | j                  D ]/}|| j        |                     |          <   | xj        dz  c_        0d S )Nr   T)exist_ok   )super__init__r   r   r   r   r   ospathexistsmakedirslistdir_unsafe_key)selfr   r   fn	__class__s       )lib/python3.11/site-packages/zict/file.pyr   zFile.__init__=   s    Y	w~~dn-- 	K666666j00  79t//334		Q			     keyreturnc                ^    t          |d          d| j         z   }| xj        dz  c_        |S )a8  Escape key so that it is usable on all filesystems.

        Append to the filenames a unique suffix that changes every time this method is
        called. This prevents race conditions when another thread accesses the same
        key, e.g. ``__setitem__`` on one thread and ``__getitem__`` on another.
         )safe#r   )r   r   r   r#   s     r!   	_safe_keyzFile._safe_keyK   s7     Cb!!!O	OO3		Q		
r"   c                V    |                      d          d         } t          |           S )z%Undo the escaping done by _safe_key()r(   r   )splitr   )r#   s    r!   r   zFile._unsafe_keyW   s$     iinnQs||r"   c                8    d| j          dt          |            dS )Nz<File: z, z
 elements>)r   lenr   s    r!   __str__zFile.__str__]   s#    @@@3t99@@@@r"   bytearray | memoryviewc                   t           j                            | j        | j        |                   }| j        r^t          |d          5 }t          t          j        |	                                d                    cd d d            S # 1 swxY w Y   d S t          |d          5 }t          j
        |	                                          j        }t          |          }|                                 5  |                    |          }d d d            n# 1 swxY w Y   ||k    sJ |cd d d            S # 1 swxY w Y   d S )Nzr+br   rb)r   r   joinr   r   r   open
memoryviewmmapfilenofstatst_size	bytearrayunlockreadinto)r   r#   r   fhsizebufnreads          r!   __getitem__zFile.__getitem__b   s   W\\$.$.*=>> ; 
	b% =B!$)BIIKK";";<<= = = = = = = = = = = = = = = = = = b$ 2x		,,4oo[[]] - -KK,,E- - - - - - - - - - - - - - -}}}}                 sI   4B		BB&AD85DD8D	D8D	D88D<?D<valuerbytes | bytearray | memoryview | list[bytes | bytearray | memoryview] | tuple[bytes | bytearray | memoryview, ...]Nonec                n   |                      |           |                     |          }t          t          j                            | j        |          d          5 }|                                 5  t          |t          t          f          r|                    |           n|                    |           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   || j        v r9t          j        t          j                            | j        |                     d S || j        |<   d S )Nwb)discardr*   r5   r   r   r4   r   r<   
isinstancetuplelist
writelineswriter   remove)r   r#   rC   r   r>   s        r!   __setitem__zFile.__setitem__y   s    	S^^C  "',,t~r22D99 	 R 	  	 %%//  e$$$$		  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  $.   Ibgll4>26677777"$DN3s7   C3AC;CC	CC	CC"%C"objectc                    || j         v S N)r   r)   s     r!   __contains__zFile.__contains__   s    dn$$r"   Iterator[str]c                *    t          | j                  S rR   )iterr   r/   s    r!   __iter__zFile.__iter__   s    DN###r"   c                    | j                             |          }t          j        t          j                            | j        |                     d S rR   )r   popr   rN   r   r4   r   )r   r#   r   s      r!   __delitem__zFile.__delitem__   s?    ^$$
	"',,t~r2233333r"   c                *    t          | j                  S rR   )r.   r   r/   s    r!   __len__zFile.__len__   s    4>"""r"   )F)r   r   r   r   )r#   r   r$   r   )r$   r   )r#   r   r$   r1   )r#   r   rC   rD   r$   rE   )r#   rP   r$   r   )r$   rT   )r#   r   r$   rE   )r$   r   )__name__
__module____qualname____doc____annotations__r   r*   staticmethodr   r0   __repr__r   rB   rO   rS   rW   rZ   r\   __classcell__)r    s   @r!   r
   r
      se        ) )V NNNLLLIII      
 
 
 
    \
A A A A H   V, % % % V%0% % % %$ $ $ $ 4 4 4 V4# # # # # # # #r"   r
   )
__future__r   r7   r   pathlibcollections.abcr   urllib.parser   r   zict.commonr   r   r   bytesr
    r"   r!   <module>rl      s    " " " " " "  				  $ $ $ $ $ $ ' ' ' ' ' ' ' ' ( ( ( ( ( ( ( (R# R# R# R# R#8CJ R# R# R# R# R#r"   